1

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

Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
  • 4
    A lot of people are saying to use `std::strings` instead of character arrays. In addition to the fact that `std::strings` are easier to use due to build in support for stuff like concatenation, they are also safer to use, as it is easy to introduce (exploitable) bugs when using character arrays. For example, what if the user enters in a filename that is longer than 19 (1 character is used for null terminator) characters? – Brian Aug 24 '10 at 18:28

3 Answers3

15

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; 
} 
Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
  • 2
    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 Aug 24 '10 at 18:22
  • 1
    Also note that string are terminated by '\0' and as such are 1 character longer than they look. – Martin York Aug 24 '10 at 18:37
  • `+1` for mentioning `std::string`. (I was at the verge of down-voting before I saw it.) – sbi Aug 24 '10 at 20:37
  • @gregg: If Rule #1 is compile clean, Rule #2 should be "read & understand the compiler errors." In this case, the compiler told OP exactly why the code wouldn't compile. – John Dibling Aug 24 '10 at 21:09
  • @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 Aug 25 '10 at 13:41
8

" 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
  • 95,107
  • 10
  • 109
  • 188
  • Ahh, I'm used to PHP... Started c++ a few days ago – Mark Lalor Aug 24 '10 at 18:19
  • @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. – GManNickG Aug 24 '10 at 18:33
  • 2
    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 Aug 24 '10 at 18:33
  • 4
    @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. – GManNickG Aug 24 '10 at 18:54
  • 1
    @GMan: +1 Indeed, 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 Aug 24 '10 at 20:05
2

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

Ahmed

Elf King
  • 1,189
  • 5
  • 7