Hello everyone! I have a little bit of a problem, It shouldn't be a problem for anyone. On that note, this post is basically just to say hi to everyone! I decided to join this community, because its always nice to
have people who can annoy people who can answer my newbie questions.
I didn't think it was right to put a post just saying hi, So I will be including a problem!
When i write this little snippet of code
1 2 3 4 5 6 7 8 9 10 11 12 13
|
...
int iSomething;
for(;;)
{
cin >> iSomething;
if(iSomething == 0)
{
cout << "Zeros are ignored, Input a Positive number" <<endl;
continue;
}
...
...
}}
|
When i have this in my program, i realize one thing. If a user inputs any charaters (Or a number outside the limits of int), then iSomething seems to get overloaded, and gets stuck with a value of 0, creating an endless loop.
I thought of a way to get out of the constant loop that occurs, by calling a function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
bool ExitCheck ()
{
static int nExitCount = 0;
nExitCount++;
if(nExitCount == 100)
{
return true; //tells the program to Exit and return 1
}
else
{
return false; //Tells the for loop to continue
}
}
...
...
...
int iSomething;
bool Exit;
for(;;)
{
cin >> iSomething;
if(iSomething == 0)
{
cout << "Zeros are ignored, Input a Positive number" <<endl;
Exit = ExitCheck();
if(!Exit)
{
continue;
}
else
{
return 1;
}
}
...
...
}}
|
This would prevent the program from terminating if the user actually inputted the number zero(unless they did it a hundred times) Yet it would shut the program down if the the loop was causing a zero over and over again.
My question is (finally) how would i go about making my exit code obsolete? If some one enters a character for my nSomething, i would want to be able to erase that value, and allow them to give it another attempt.
I was looking around at the other posts, just to take a peek at other peoples programming, and i saw the mentioning of a "cin.ignore()" and i am wondering if that's where the answer lies
Well, i would like some feedback. I don't care if its just to say "Hello, welcome to the community" or if it's to say "Your programming style sucks!" or if its the answer to my question! Like i said, im just posting this to say hi to the community.
So to Sum up my post in a couple of words:
Hello World! ^_^