Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem when executing this code

Problem when executing this code

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionannouncement
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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.

    P T J 3 Replies Last reply
    0
    • L Lost User

      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.

      P Offline
      P Offline
      Paul Belikian
      wrote on last edited by
      #2

      I don't know, how about a carriage return after "Enter a sentence\n" ?

      1 Reply Last reply
      0
      • L Lost User

        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.

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        If I remember, add a "endl" to the end. i.e. count << "Enter a sentence: " << endl; An endl not only adds a CR/LF, it also flushes the output buffer. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        1 Reply Last reply
        0
        • L Lost User

          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.

          J Offline
          J Offline
          JT Anderson
          wrote on last edited by
          #4

          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.

          S 1 Reply Last reply
          0
          • J JT Anderson

            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.

            S Offline
            S Offline
            shawnsch
            wrote on last edited by
            #5

            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

            Z 1 Reply Last reply
            0
            • S shawnsch

              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

              Z Offline
              Z Offline
              ZoogieZork
              wrote on last edited by
              #6

              #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

              S 1 Reply Last reply
              0
              • Z ZoogieZork

                #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

                S Offline
                S Offline
                shawnsch
                wrote on last edited by
                #7

                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.

                Z 1 Reply Last reply
                0
                • S shawnsch

                  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.

                  Z Offline
                  Z Offline
                  ZoogieZork
                  wrote on last edited by
                  #8

                  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 mixing cstdio and iostream 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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups