Problem when executing this code
-
Hi all. New here to coding kind of. Check out the following code.
#include "iostream.h" #include "cstdio.h" #include "cstring.h" int main() { char str[80]; cout << "Enter a sentence: "; gets (str) cout << "\nYou entered: " << str return 0; }
What it does is nothing, but prompts you to enter the string BEFORE it couts "Enter a sentence: ". Why would this be, cout is clearly supposed to be executed before the statement gets (str);. How the heck does it get that out of what I coded. Wrong version of VC or something ? using MS VC 6.0. How can this be. I am banging my head against the wall. :mad: lol Thank you much people. -
Hi all. New here to coding kind of. Check out the following code.
#include "iostream.h" #include "cstdio.h" #include "cstring.h" int main() { char str[80]; cout << "Enter a sentence: "; gets (str) cout << "\nYou entered: " << str return 0; }
What it does is nothing, but prompts you to enter the string BEFORE it couts "Enter a sentence: ". Why would this be, cout is clearly supposed to be executed before the statement gets (str);. How the heck does it get that out of what I coded. Wrong version of VC or something ? using MS VC 6.0. How can this be. I am banging my head against the wall. :mad: lol Thank you much people.I don't know, how about a carriage return after "Enter a sentence\n" ?
-
Hi all. New here to coding kind of. Check out the following code.
#include "iostream.h" #include "cstdio.h" #include "cstring.h" int main() { char str[80]; cout << "Enter a sentence: "; gets (str) cout << "\nYou entered: " << str return 0; }
What it does is nothing, but prompts you to enter the string BEFORE it couts "Enter a sentence: ". Why would this be, cout is clearly supposed to be executed before the statement gets (str);. How the heck does it get that out of what I coded. Wrong version of VC or something ? using MS VC 6.0. How can this be. I am banging my head against the wall. :mad: lol Thank you much people. -
Hi all. New here to coding kind of. Check out the following code.
#include "iostream.h" #include "cstdio.h" #include "cstring.h" int main() { char str[80]; cout << "Enter a sentence: "; gets (str) cout << "\nYou entered: " << str return 0; }
What it does is nothing, but prompts you to enter the string BEFORE it couts "Enter a sentence: ". Why would this be, cout is clearly supposed to be executed before the statement gets (str);. How the heck does it get that out of what I coded. Wrong version of VC or something ? using MS VC 6.0. How can this be. I am banging my head against the wall. :mad: lol Thank you much people.You're mixing iostreams (cout) and stdio (gets), so the streams are not synchronized. This should work: #include "iostream.h" #include "stdio.h" #include "string.h" int main() { char str[80]; cout << "Enter a sentence: "; cout.flush(); gets (str); cout << "\nYou entered: " << str; return 0; } As should this: #include #include int main() { std::string str; std::cout << "Enter a sentence: "; std::getline(std::cin, str); std::cout << "\nYou entered: " << str; return 0; } (But, for some reason I don't understand, the latter requires you to hit enter twice.) -------- There are 10 types of people in this world. Those who know binary and those who don't.
-
You're mixing iostreams (cout) and stdio (gets), so the streams are not synchronized. This should work: #include "iostream.h" #include "stdio.h" #include "string.h" int main() { char str[80]; cout << "Enter a sentence: "; cout.flush(); gets (str); cout << "\nYou entered: " << str; return 0; } As should this: #include #include int main() { std::string str; std::cout << "Enter a sentence: "; std::getline(std::cin, str); std::cout << "\nYou entered: " << str; return 0; } (But, for some reason I don't understand, the latter requires you to hit enter twice.) -------- There are 10 types of people in this world. Those who know binary and those who don't.
So, if that is true, then why would Herb Schildt produce this code in his book(C++ from the Ground Up, 3rd edition):
#include "iostream.h" #include "cstdio.h" // using namespace std brings up an error using MS VC 6.0. This is my own note // This code is from page 88. int main() { char str[80]; cout << "Enter a string: "; gets(str); cout << "Here is your string: "; cout << str; return 0; }
It might make sense that the two streams are not synchronized which is the only thing I could think of, however Mr. Schildt has produced otherwise. Possibly a different compilor configuration? Mr. Schildt shows this in his book:Enter a string: This is a test Here is your string: This is a test
-
So, if that is true, then why would Herb Schildt produce this code in his book(C++ from the Ground Up, 3rd edition):
#include "iostream.h" #include "cstdio.h" // using namespace std brings up an error using MS VC 6.0. This is my own note // This code is from page 88. int main() { char str[80]; cout << "Enter a string: "; gets(str); cout << "Here is your string: "; cout << str; return 0; }
It might make sense that the two streams are not synchronized which is the only thing I could think of, however Mr. Schildt has produced otherwise. Possibly a different compilor configuration? Mr. Schildt shows this in his book:Enter a string: This is a test Here is your string: This is a test
#include "iostream.h"
This is the old way of including the C++ standard headers, which indicates that the book may be a bit outdated. The new headers don't have an extension, e.g.:#include <iostream> #include <cstdio>
It is up to the iostreams implementation to decide how to handle buffering, and some older implementations may have synchronization between iostreams and stdio turned on by default. - Mike -
#include "iostream.h"
This is the old way of including the C++ standard headers, which indicates that the book may be a bit outdated. The new headers don't have an extension, e.g.:#include <iostream> #include <cstdio>
It is up to the iostreams implementation to decide how to handle buffering, and some older implementations may have synchronization between iostreams and stdio turned on by default. - MikeOkay, however it is due to my mistake that the includes are listed as so. I had actually changed the to "iostream.h" because the web forum didn't display that till I figured out that feature. Which leads me to my main point, which is how am I supposed to learn this book of the stuff doesn't compile. I paid alot of money for this book, and I don't think it's old either.
-
Okay, however it is due to my mistake that the includes are listed as so. I had actually changed the to "iostream.h" because the web forum didn't display that till I figured out that feature. Which leads me to my main point, which is how am I supposed to learn this book of the stuff doesn't compile. I paid alot of money for this book, and I don't think it's old either.
I haven't read any of the C++ books by Herb Schildt, but I've heard both good and bad things about them. The edition you mentioned was published in March 2003, which is indeed fairly recent, so I would expect it to follow more modern conventions. The code snippet you posted earlier should work fine (it compiles fine in VC6 for me, with the
using namespace std;
line and the includes changed to their modern versions) since all the input is coming from one type of stream and all the output is going to another type of stream. The problem arises when you read in data from two different types of streams or write data to two different types of streams. Stylistically, I would argue that mixingcstdio
andiostream
is generally a bad idea, even in a case like this. Also,gets()
is inherently unsafe, since the program would break if a line longer than 80 characters was entered. Hopefully, this was noted in the book. If not... eep. :omg: shawnsch wrote: how am I supposed to learn this book of the stuff doesn't compile. What was the compiler error? I'm curious, since, as I mentioned, I just tried it in VC6 and it compiled and ran. Here is the version I tried:#include <iostream> #include <cstdio> using namespace std; int main() { char str[80]; cout << "Enter a string: "; gets(str); cout << "Here is your string: "; cout << str; return 0; }
- Mike