C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 16th, 2005, 07:03 AM
Ad1234 Ad1234 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 6 Ad1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 4 sec
Reputation Power: 0
Pausing the program

Hi,

This is my first post, but I am really ripping my hair out here as I can't see what is going wrong. Heres the thing I want my program to wait for the user to push any key before the program calls the main() function again I am using getchar() to do this . Heres the bit of code that is causing the problem (I havn't been doing this for long so the code is probably a bit rough, but it should work)

Code:
  
  while (x=1)
  {
  
      cin  >> LD;
  
      if (LD >= 1 && LD <= 12)
      {
      
          cout << "\n\n You must roll lower or equal to " << LD << " to pass the test and not break. Good Luck\n";
          break;
      }
      else
      {
      cout <<"\nPlease enter a number between 1 and 12 for the LD statistic";
      continue;
      }
  }
  
  
  getchar();
  main();
  
  }


The program does pause, but it pauses when the user enters a correct number beforre it has displayed the text

<< "\n\n You must roll lower or equal to " << LD << " to pass the test and not break. Good Luck\n";

to the screen. It will then wait for a keystroke before displaying the text and returning to main(). I have also tried using the system("pause") command but this gives me exactly the same results.

I am using MS Visual Studio 6.0 to compile it.

Any help would be much appriciated thanks very much for reading. Ad.

Last edited by B-Con : April 16th, 2005 at 09:30 PM. Reason: don't forget to use the [code] tags ;)

Reply With Quote
  #2  
Old April 16th, 2005, 07:39 PM
astralvoid astralvoid is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 9 astralvoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 42 sec
Reputation Power: 0
I am a bit confused on what you're trying to accomplish, but I see one hidden bug at the top.

Code:
    while (x=1)
 


I am guessing, but I assume you really mean:

Code:
 while (x==1)
 


Because you are effectively setting x=1 in each loop.

In addition, cin captures the character and any extra characters like the carraige return. I would suggest you do something like

Code:
 cout << endl;
 


before you call your getch() function. The endl manipulator essentially does a carraige return followed by a flush of the buffer. Then your getch() function should be able to wait until a key is pressed.

Hope this helps!

Reply With Quote
  #3  
Old April 17th, 2005, 05:13 AM
Ad1234 Ad1234 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 6 Ad1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 4 sec
Reputation Power: 0
Thanks that worked a treat!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Pausing the program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT