Hello so i have been making a program and decided to start on error checking. I'm asking the user to input a 1 to continue and a 2 to exit simple. I worked the code out for the error checking though when i compiled and ran the program all it did was go into a endless loop. here is the code where the error is occuring.

//these are what the variables are declared as
double ran=0;
int error=0;

errorcheck:                    
error=false;
if(ran!=1 || ran!=2)
{

 error=true;

}             
while(error==true)
{
         system("cls"); //it gets stuck in here it wont allow me to enter any thing it just repeats
         cout<<"this is a incorrect value \nenter 1 to start\nor\nenter 2 to exit\n";
         cout<<"answer here -->\t";
         cin>>ran;
         goto errorcheck;
}

1) Get rid of system("cls"); -- it's verry annoying to the user.
2) Do not use goto -- you don't need it. Use a loop if you need to repeat somthing.
3) You logic is all messed up.

a) You start by setting error to FALSE
b) Then you check the value of ran which sets error to TRUE (it's 0)
c) You input a new value for ran
d) You unconditionally jump above the loop.
Why do you have a loop at all? You never use it.

Try this:

Set error to TRUE
Start while() loop, exit if error = FALSE
    input ran
    if ran = 1 or 2 set error to TRUE
      else output  error message
end loop

so i tried what you said and it skipped the loop all together so i changed it up a bit here is what i did

error=false;
while(error==false)
{
cout<<"this is a random future telling device of what will happen in the next 60 secs\n";

cout<<"\njust press 1 and hit enter to continue\n\nor press 2 and hit enter to exit\n\n";

cout<<"answer here -->\t";

cin>>ran;
if(ran==1 || ran==2)
{
          error=true;
}
else
{
error=false;
cout<<"wrong input";
system("pause");//checking to see if it runs. 
}
}

it works its catching the the input f but then it just infinite loops it reloops so i can retype a input though instead of allowing me to retype a input it just repeats.

@ Kanown

You said

I'm asking the user to input a 1 to continue and a 2 to exit simple

Then in your program you have

if(ran==1 || ran==2)
{
   error=true;

}

Does it makes any sense ? I mean for 1 your program should act in one way and for 2, in another completely different way.
Here you say If ran is 1 or if ran is 2 assign true to error, which is not correct.

What you can do is this :

if ( ran == 2)
{
  error = true ;

}

Then it will exit the program when 2 is pressed as the condition will conflict.
Now for wrong input, you can give a condition for wrong input before checking for 1 and 2. Example

while ( error = false )
{
   if( ran != 1 && ran != 2)
   {
      cout << "\n Wrong input, try again  ";
      flag = 1;
   }

   if( ( flag == 0) && ( ran == 2 ))
   {
      error = true ;
   }

}

And avoid using system calls in your program.

Also, have you declared error a bool type variable ?

Does it makes any sense ?

Yes it does. At least to me. My understanding is this loop is the input for the menu, not the entire program.

If the user types anything except 1 or 2, this error loop is supposed to continue asking until a correct response is made.

When the loop exits (1 or 2 was pressed), the program will either
1: do work
2: exit

Kanown:
Always format your code. Always.

cin>>ran;
if(ran==1 || ran==2)
{
    error=true;
}
else
{
    error=false;               // 'error' is already false. 
    cout<<"wrong input";       // That's why you are in this loop
}

Note the comments...
See this about system("pause")

walt is correct and sry about not formatting the code or anything im new to the website and coding but error checking is not the main problem its the input of f and it infinite looping it already checks if its not 1 or 2 and thanks i did change so that when two is inputed it just closes the program though if a letter is inputed i want it just ask to reinput a correct number 1 or 2

(-8 One sentence? Haven't you heard of periods and commas? Sheesh! 8-)

:) sorry im a bit dyslexic and grammar is by far not my best subject. anyways here is my code so far.

error=false;
while(error==false)
{
cout<<"this is a random future telling device of what will happen in the next 60 secs\n";

cout<<"\njust press 1 and hit enter to continue\n\nor press 2 and hit enter to exit\n\n";

cout<<"answer here -->\t";

cin>>ran;
if(ran==1)      **//this continues the program**
{
          error=true;**//i mixed up the true false thing its opposite **
}
else//this exits
{
error=false;
cout<<"good bye\n\n";
return 0;
}
}

I'm just trying to get it so i dont have to exit the program if they input a character and to re ask them to input thats all.

my sugession: change the variable type for "ran" to int, and it will magically start to work. Basiaclly, you can't check the value of a double variable like you do (ran==1), because ran will never be 1 (it will be something like 1.0000000000000012 depending on your harware environment).

Also, have a habit to initialise double variables like this: double ran = 0.0; (add a decimal part to it) read this (no a TL;DR article)

I repeat: format your code

Yeah, why is ran a double? chaau is correct. I missed that.

And that code is not quite what I suggested.

I am now confused. And I'm partly to blame.
If error = true, stay in the loop. If error = false, exit the loop. For the current use of the variable, it should be named exitLoop

Also, you do not want to exit the program in the loop. You should let the exit happen naturally at the end of the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.