The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 ViewsIF you set up a program to accept intiger values and store them in a int variable, and if the user inputs text instead of an intiger. would this cause the program to close? ie:
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
int magic = rand();
int guess;
cout << "Enter Your Guess: ";
cin >> guess;
if ( guess == magic ) {
cout << "***Right***";
} else {
cout << "..Sorry, wrong answer";
}
cin.get();
cin.get();
return 0;
}
if i input text instead of a number, the app closes.. please help
// MustyWindows - Jump Through The Windows
// AmpFusion - Where Underground Becomes Mainstream
Neo Enterprise Technologies Coming soon.
At 6/12/05 12:35 AM, BulletProof57 wrote: #include<iostream>
#include<cstdlib>
using namespace std;
int main() {
int magic = rand();
int guess;
while(guess != magic){
cout << "Enter Your Guess: ";
cin >> guess;
if ( guess == magic ) {
cout << "***Right***";
} else {
cout << "..Sorry, wrong answer";
}
}
//What's with that two 'cin.get()'s?
cin.get();
return 0;
}
Yea will i just fixed some of your code. The while will keep asking to get an input from the user until he guesses correctly. Also i don't understand why you had two 'cin.get();' s. As for the problem, i'm sorry i can't help.
At 6/12/05 10:26 AM, Cinjection wrote:At 6/12/05 12:35 AM, BulletProof57 wrote:Also i don't understand why you had two 'cin.get();'
Hes trying to keep the console window open.
Good man, not using system("PAUSE");
At 6/12/05 12:35 AM, BulletProof57 wrote: IF you set up a program to accept intiger values and store them in a int variable, and if the user inputs text instead of an intiger. would this cause the program to close?
Oh, and yea with my expierience it will close. Need Input validation for everything thses days..
At 6/12/05 10:50 AM, Mo4545 wrote:At 6/12/05 12:35 AM, BulletProof57 wrote: IF you set up a program to accept intiger values and store them in a int variable, and if the user inputs text instead of an intiger. would this cause the program to close?Oh, and yea with my expierience it will close. Need Input validation for everything thses days..
thank you good sir. unfortunatly i havnt gotten that far in c++ though. so is ther eany chance you could show me a couple functions that check the data type?
and yeah, the 2 cin.get();'s were to keep the window open.
// MustyWindows - Jump Through The Windows
// AmpFusion - Where Underground Becomes Mainstream
Neo Enterprise Technologies Coming soon.
At 6/12/05 11:49 AM, BulletProof57 wrote: thank you good sir.
Oh your very welcome.
:so is ther eany chance you could show me a couple functions that check the data type?
Hmm functions ey.. well there is probably some hangin around some where., but you dont need em, checking input is easy with loops or your own functions.
Theres is probably cajillions of ways to do it, but most of the time a simple while loop or if statement will do the job.
Just look at Cinjections post up top^, that maybe one of a zillion ways to solve your problem.
well if i use that example. and enter text. i just keep getting the "..sorry wrong answer Enter your Guess" over and over and over(infinate ammount of times) again.
// MustyWindows - Jump Through The Windows
// AmpFusion - Where Underground Becomes Mainstream
Neo Enterprise Technologies Coming soon.
Idea, how about you have the random number be a char value so that you don't have that problem?
char magic = (char) (rand(10)+48);
That should be the code to do that....(but I'm not 100% sure if that's legal C++)
Anyways, because a computer can't do random things and everything is scripted, the rand() function does the same pattern everytime, here's a linky...
At 6/12/05 05:29 PM, BulletProof57 wrote: well if i use that example. and enter text. i just keep getting the "..sorry wrong answer Enter your Guess" over and over and over(infinate ammount of times) again.
Ofcourse you will, your sopposed to get that answer, because your text doesnt equal to an integer. Enter the right random number(because your using rand(); it wont be too heard because it uses the same numbers over and over.) and your program will go to ***Right***.