Compiled Program premature closing

Go down

Compiled Program premature closing

Post by zodiacescuchion on Sun Feb 14, 2010 6:33 pm

The riddle program i made closes prematurely.
Code:
#include<iostream>
#include<string>
#include<windows.h>
using namespace std;

int main ()
{  string a,b,c;
   cout<<"bet you can't solve this riddle:";
   cout<<'\n';
   do
   {
   cout<<"type in two different numbers in which one is not greater than the other And the other is not greater than the first one"<<'\n'
      << "and you can't cheat by putting in the same number twice";
   cout<<'\n';
   cout<<"first number:";
   cin>> a;
   cout<<"second number:";
   cin>>b;
   if ((a>=b)&&(b>=a))
   {cout<<"hey you cheater i said you can't use the same number twice";
   Sleep(3000);
   break;
   }   
   cout<<"Haha you're wrong" <<'\n'<<"Do you give up:";
   
   cin>>c;
   }
   while ((c!="yes")&&(a!=b));
   cout<<'\n'<<"Awww Well You won. Albeit through negative means";
   Sleep(2000);
   cout<<'\n'<<[b]"but did you really?"[/b];
   Sleep(1000);
   cout<<'\n' <<" you only won because you are a cheater and a quitter" <<'\n'<<"so in the end who really won?";
   return 0;
}
It looks like that but instead of letting me press any key to close it just immediately closes as soon as it gets to the return 0 part so the last thing you see is "but did you really" I know thats because it closes right after it couts the last strings
avatar
zodiacescuchion
Skid

Posts : 28

Back to top Go down

Re: Compiled Program premature closing

Post by Admin on Sun Feb 14, 2010 6:59 pm

you need a pause statement at the end before return 0 so that it pauses and lets you press enter, and THEN ends the program.

i.e. you can either use

cin.ignore(1);

OR

System("pause"); (i recommend the cin statement instead)

_________________
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: Compiled Program premature closing

Post by Unchained on Mon Feb 15, 2010 6:57 am

Or
Code:
cin.get();
avatar
Unchained
Mod

Posts : 448

Back to top Go down

Re: Compiled Program premature closing

Post by zodiacescuchion on Mon Feb 15, 2010 7:21 am

oh ok
avatar
zodiacescuchion
Skid

Posts : 28

Back to top Go down

Re: Compiled Program premature closing

Post by Unchained on Mon Feb 15, 2010 7:53 am

Yea I had that problem when I started out too.
avatar
Unchained
Mod

Posts : 448

Back to top Go down

Re: Compiled Program premature closing

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum