Quick Question

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 1
Reputation: The Leprechaun is an unknown quantity at this point 
Solved Threads: 0
The Leprechaun The Leprechaun is offline Offline
Newbie Poster

Quick Question

 
0
  #1
Oct 19th, 2006
ok well i started out with making my own source code then compiling and running with Dev-C++ and when i run it, its working fine until the last line, here let me past my code

  1. // just a test
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. string mystr;
  10. cout << "Hello, whats your name? ";
  11. getline (cin, mystr);
  12. cout << "Hello " << mystr << ".\n";
  13. cout << "What grade are you in" << mystr << "? ";
  14. getline (cin, mystr);
  15. cout << "Thats cool, I used to be in " << mystr << "too.\n";
  16. cout << "Well i'm going to go now, goodbye.";
  17. return 0;
  18. }
i know its stupid, i just started and wanted to do something, but ok when i get to the part "what grade are you in << mystr << "? "; and when i enter my grade or w/e the box just closes, is there any way to fix this, would i have to put something like Press "q" to quit or something like that? Any help would be nice, thankyou
Last edited by The Leprechaun; Oct 19th, 2006 at 1:43 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Quick Question

 
0
  #2
Oct 19th, 2006
try putting cin.get(); before your return statement.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 21
Reputation: easy is an unknown quantity at this point 
Solved Threads: 0
easy's Avatar
easy easy is offline Offline
Newbie Poster

Re: Quick Question

 
0
  #3
Oct 22nd, 2006
run the in a command prompt then ull be able to see all the output before it quits. or try placing system("PAUSE") before the return statement, but i think thats bad practise to do that
Last edited by easy; Oct 22nd, 2006 at 1:11 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,652
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Quick Question

 
0
  #4
Oct 22nd, 2006
Originally Posted by easy View Post
run the in a command prompt then ull be able to see all the output before it quits. or try placing system("PAUSE") before the return statement, but i think thats bad practise to do that
Correct .
Its bad practice to use system calls to achieve such a trivial function.
Better use getchar() if using C and cin.get() when using C++.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 21
Reputation: easy is an unknown quantity at this point 
Solved Threads: 0
easy's Avatar
easy easy is offline Offline
Newbie Poster

Re: Quick Question

 
0
  #5
Oct 22nd, 2006
Originally Posted by ~s.o.s~ View Post
Correct .
Its bad practice to use system calls to achieve such a trivial function.
Better use getchar() if using C and cin.get() when using C++.
is this because the code isnt portable to other systems?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,652
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Quick Question

 
0
  #6
Oct 23rd, 2006
Yes that and many other things to be considered like the kind of overhead introduced when you call system functions for trivial tasks.

A very good tutorial by my friend and forum member Mr. WaltP can be found here which introduces some of the things which should be avoided.
http://www.gidnetwork.com/b-61.html
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: Quick Question

 
0
  #7
Nov 1st, 2006
getline() throws away the \n null character at the end of a string and therefore makes a mess when trying to fluch the buffer. Using cin.get() makes everything much more comfortable
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,652
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Quick Question

 
0
  #8
Nov 1st, 2006
Originally Posted by may4life View Post
getline() throws away the \n null character at the end of a string and therefore makes a mess when trying to fluch the buffer.
More surprises...

'\n' is a newline character, not null character ( '\0' )
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: Quick Question

 
0
  #9
Nov 2nd, 2006
Whoops! yep, i meant getline()'s terminating new line (\n) is read and thrown away. In get() this new line character is left in the input buffer
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2036 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC