stuck please help-quick question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 77
Reputation: revenge2 is an unknown quantity at this point 
Solved Threads: 2
revenge2's Avatar
revenge2 revenge2 is offline Offline
Junior Poster in Training

stuck please help-command promt closes

 
0
  #1
Dec 18th, 2007
hello, I am following a book and im stuck on this one.....(note it's tacky but just bear with me, im a newb)
  1. #include <iostream.h>
  2. int main()
  3. {
  4. int RedSoxScore, YankeesScore;
  5. cout<<"Enter the score for the RedSox:";
  6. cin>> RedSoxScore;
  7.  
  8. cout<<"Enter the score for the yankees:";
  9. cin>> YankeesScore;
  10.  
  11. cout<<"\n";
  12.  
  13. if (RedSoxScore > YankeesScore)
  14. cout<<"GO THE BLIMMIN REDSOX!!\n";
  15.  
  16. if (YankeesScore > RedSoxScore )
  17. {
  18. cout<<"WHOOPY GO THE YANKS!!!\n";
  19. cout<<"Happy days in NYC!!!\n";
  20. }
  21.  
  22. if (RedSoxScore == YankeesScore)
  23. {
  24. cout<<"A tie?, nha cant be!\n";
  25. cout<<" Give me the real score for the yanks:";
  26. cin >> YankeesScore;
  27.  
  28. if (RedSoxScore > YankeesScore)
  29. cout<<"Knew it, Go the sox!!";
  30.  
  31. if (YankeesScore > RedSoxScore)
  32. cout<<"Knew it go the yanks!!";
  33.  
  34. if (YankeesScore == RedSoxScore)
  35. cout<<"Wow, it was tie!?";
  36. }
  37.  
  38. cout<<"\nThanks for telling me.\n";
  39. return 0;
  40. }

I know im using the old .h header files, because that's what the book uses (it says i can use the new standards if i want, using namespace std but my code tends to not work sometimes. So im sticking to the old ones just untill i get the hang of things.

Anyway i compiled the following on devc++ and it runs the first few line well untill the command promt suddently closes after line 9.

The code seems to be fine because it compiled, but it just flashes.

Any ideas as to what im doing wrong?....
-Thanks
Last edited by Ancient Dragon; Dec 18th, 2007 at 10:01 am. Reason: corected code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz

Re: stuck please help-command promt closes

 
0
  #2
Dec 18th, 2007
Take a look here at cin.ignore() and cin.get()
http://www.cprogramming.com/tutorial/lesson1.html
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: stuck please help-quick question

 
0
  #3
Dec 18th, 2007
Hi revenge2,

I looked in your code. It works as expected. I don't understand what do you mean by "flashing". You may like to add followin lines before return.
I am using Microsoft C++ therefore I used
  1. _getch
you may use anything which blocks the execution and let your program eait for user input.

Final code looks like...
  1. cout<<"Press any key to continue."
  2. int i = _getch();

  1. #include "conio.h"
  2. #include<iostream>
  3. using namespace std ;
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7. int RedSoxScore, YankeesScore;
  8. cout<<"Enter the score for the RedSox:";
  9. cin>> RedSoxScore;
  10.  
  11. cout<<"Enter the score for the yankees:";
  12. cin>> YankeesScore;
  13.  
  14. cout<<"\n";
  15.  
  16. if (RedSoxScore > YankeesScore)
  17. cout<<"GO THE BLIMMIN REDSOX!!\n";
  18.  
  19. if (YankeesScore > RedSoxScore )
  20. {
  21. cout<<"WHOOPY GO THE YANKS!!!\n";
  22. cout<<"Happy days in NYC!!!\n";
  23. }
  24.  
  25. if (RedSoxScore == YankeesScore)
  26. {
  27. cout<<"A tie?, nha cant be!\n";
  28. cout<<" Give me the real score for the yanks:";
  29. cin >> YankeesScore;
  30.  
  31. if (RedSoxScore > YankeesScore)
  32. cout<<"Knew it, Go the sox!!";
  33.  
  34. if (YankeesScore > RedSoxScore)
  35. cout<<"Knew it go the yanks!!";
  36.  
  37. if (YankeesScore == RedSoxScore)
  38. cout<<"Wow, it was tie!?";
  39. }
  40. cout<<"\nThanks for telling me.\n";
  41.  
  42. cout<<"Press any key to continue."
  43. int i = _getch();
  44. return 0;
  45. }
Last edited by Ancient Dragon; Dec 18th, 2007 at 10:38 am. Reason: add line numbers
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: stuck please help-quick question

 
0
  #4
Dec 18th, 2007
>>_getch_getch
This is a C++ program, not C. So don't use that C function. Use cin.ignore() or cin.get() so that you don't mix C and C++ in the same program. Sure of course getch() works, but its not consistent and you might get lower marks from your instructor for using it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: revenge2 is an unknown quantity at this point 
Solved Threads: 2
revenge2's Avatar
revenge2 revenge2 is offline Offline
Junior Poster in Training

Re: stuck please help-quick question

 
0
  #5
Dec 18th, 2007
unfortunately i dont undertand that, ahh i have only been using iostream for the purpose of learning plus the book seems to use this for the time being. I dont have an instructor/teacher or anything im just trying to learn from home, after school.

Anyway is there anywway to dum it down a bit?! like explain what "cin.ignore()" does exactly-oh and please dont mix c with c++ i get even more lost (lol)

Flashing meaning; when i run the program and enter the score' at line 9 the command prompt just closes before i see or get to do anything.
-thanks
Last edited by revenge2; Dec 18th, 2007 at 10:33 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,462
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: stuck please help-quick question

 
0
  #6
Dec 18th, 2007
>>I dont have an instructor/teacher or anything im just trying to learn from home, after school.
Good luck. Please don't hesitate to ask questions here.

>>like explain what "cin.ignore()
See this link for some good info about cin

>>does exactly-oh and please dont mix c with c++ i
It means do not mix the two languages in the same program. C++ supports most C functions but it is not considered good programming practice to mix them up. Specifically -- don't use anything found in stdio.h in a c++ program. And don't use anything in conio.h at all because those functions are not standard, which means they may or may not be supported by a compiler.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC