views:

154

answers:

3

Why does the following code not work?

#include <iostream>
#include <string>
int main(){
    char filename[20];
    cout << "Type in the filename: ";
    cin >> filename;
    strcat(filename, '.txt');
    cout << filename;
}

It should concatenate ".txt" on the end of whatever filename is inputted

Also, when I try to compile it (with g++) this is the error message

alt text

+15  A: 

Use double quotes instead of single quotes.

strcat(filename, ".txt"); 

In C++, single quotes indicate a single character, double quotes indicate a sequence of characters (a string). Appending an L before the literal indicates that it uses the wide character set:

".txt"  // <--- ordinary string literal, type "array of const chars"
L".txt" // <--- wide string literal, type "array of const wchar_ts"
'a'     // <--- single ordinary character, type "char"
L'a'    // <--- single wide character, type "wchar_t"

The ordinary string literal is usually ASCII, while the wide string literal is usually some form of Unicode encoding (although the C++ language doesn't guarantee this - check your compiler documentation).

The compiler warning mentions int because the C++ standard (2.13.2/1) says that character literals that contain more than one char actually has type int, which has an implementation defined value.

If you're using C++ though, you're better off using std::string instead, as Mark B has suggested:

#include <iostream> 
#include <string> 
int main(){ 
    std::string filename;
    std::cout << "Type in the filename: "; 
    std::cin >> filename; 
    filename += ".txt"; 
    std::cout << filename; 
} 
In silico
Wow...So simple...
Mark
Rule number 1: Compile Cleanly at High Warning Levels (http://www.gotw.ca/publications/c++cs.htm). Good catch; it's an easy error to make (and miss).
gregg
Also note that string are terminated by '\0' and as such are 1 character longer than they look.
Martin York
`+1` for mentioning `std::string`. (I was at the verge of down-voting before I saw it.)
sbi
John Dibling
@John: Thanks for your comment. It of course follows from the mandate to compile cleanly that we must understand what the warning is telling us. The book I linked in my first comment explains it much better than I can. I highly recommend it for all C++ programmers (uncompensated endorsement). The error may be simple for some to identify, through hard experience, but after getting stuck the OP did good to seek help noting both the warning and the bad behavior.
gregg
+7  A: 

" and ' mean different things in C++. The single quote means a character, while the double quote means a C-string. You should use ".txt".

Given that this is C++ however, don't use C-style char[] at all: Use std::string instead:

#include <iostream>
#include <string>
int main(){
    std::string filename;
    cout << "Type in the filename: ";
    cin >> filename;
    filename += ".txt";
    cout << filename;
}
Mark B
Ahh, I'm used to PHP... Started c++ a few days ago
Mark
@Mark: Forget you know any languages. You need to get [a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start from the beginning.
GMan
If the material you are reading is suggesting using a character array and `strcat` over using `std::string` , you're probably either reading a C tutorial or a poorly written C++ tutorial.
Brian
Not reading anything, playing around :)
Mark
@Mark: That's fun, but be aware results and practices shouldn't be remembered; you'll get bad habits and false information that way. If you really want to *learn* it, you have to get a book.
GMan
@GMan: +1Indeed, I'd say a book is essential for learning a programming language, especially in a language such as C++ which is quite versatile, for better or worse. Also, a book is much nicer to read than long tutorials on screen.
gablin
+2  A: 

strcat second argument uses a string (double quotes). You are using single quotes (character == integer )

Ahmed

Elf King