Function not working properly?

Hello every1, i have been learning c++ by reading books, and i was confused when i came into this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <stdio>
using namespace std;

int main()
{
    char str[80];
    
    cout << "Please enter a new string: ";
    gets(str);
    cout << "Here is your string: "
    cout << str;

    return 0;
}




Now according to the book, if i input "This is a test" without the "", it only outputs: Here is your string: This


Which is really wierd considering that i am using gets() instead of cin just to anticipate this problem. Can somebody explain to me why the program malfunctions even though i have copied the code from the book correctly?

Thanx in advance.
Ferrovial
Works for me. What compiler are you using? The header should be cstdio, not simply stdio and you are missing a semicolon on line 11.
Sorry it is correct in my computer, the code is written correctly, not missing punctuation or letters (it is cstdio, i just rewrote the code here, and i made a mistake) and the compiler i use is gnu-gcc on dev cpp 5.1 on windows vista.
You may also have to include the string library to be able to use cout for char arrays??? (Not sure on that.)
Regardless try using printf("%s", str) to print the string instead of cout << str.
ok i will thanx. also can you explain to me what do you mean by saying include the string library? Because i have never heard about the string library. is it like <stdlib> or something?
meh i got it, you must be talking about <cstring>. Silly me, i hadn't noticed it earlier it was 1 page later in my book, from where i was.
<string> would be the current C++ library and <cstring> or <string.h> would be the old library C library.
Also if you would like to hunt through library documentation check out: http://cplusplus.com/reference/
You can find out how to use the string libraries from that page.
kevinkjt2000 wrote:
You may also have to include the string library to be able to use cout for char arrays???

No, the << operator is overloaded to work properyl with char*.

@ferrovial: if you want to use std::string, and in C++ you should, you have to #include <string>.

Also, gets() is very unsafe and you should never ever use it. If your book presents gets() but doesn't mention this, you might want to consider a better book. You could do this instead:

1
2
string str;
getline(cin, str);
@filipe is it because it doesn't limit check (or how it is supposed to be translated from greek?) I mean it doesn't check the array for overflow problems once you write a string with a size more than 80 characters?
Topic archived. No new replies allowed.