Check if return value is 1

Hey guys,

I've been wondering, if it's possible to check the return value in this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
double Test()
{
       double num = 0;

       if(!(cin>>num)){
       cout << "Error: Bad input type, must be a number... As I haven't been able to make the error message work\
       \n close down the console yourself...";
       num = Test;
       cin.clear();
       cin.clear();
       }
       return 1;
}


So basically what this does is give an error message if the input isn't a number.
In main you'd get something like this:
1
2
3
4
5
6
7
8
cout << "Enter number 1: ";         
         num1 = Test();
         
         cout<<"\nEnter number 2: ";
         num2 = Test();
         // the function for additions/ substractions
         result = num1 + num2;
         cout<<num1<<" + "<<num2<<" = "<<result<<"\n";


What currently happens if the Test() returns 1, is that the program doesn't crash, but it just displays the error message, and then result of 1+1=2 (so it runs as if nothing happend..)

So my question is, can I make the program check if Test() has returned 1 and then make it skip the output of the result?

If i'm unclear, please say so (=

Thanks guys :)

Cheers!
Your function doesn't really work as I think you intend; it ALWAYS returns 1. The number you read from the user is gone when the function returns.

In any case, if you want to check if Test() returned 1, you should check the values of num1 and num2 (as those are where the return values are stored.
Thanks for your reply!

I fixed that (it returns num if it's correct).
if you want to check if Test() returned 1, you should check the values of num1 and num2 (as those are where the return values are stored.
how do I do that (that was basically my question :P)?
1
2
3
4
5
6
7
8
9
10
if(num2 == 1)
{
    //num2 is 1 code here
    cout << "Num2 is 1!";
}
else
{
    //num2 is not 1 code here
    cout << "Num2 is not 1 :<";
}


Read about control structures here http://cplusplus.com/doc/tutorial/control/
Theyre very very very important and you will most likely always use them in everything you write
You used an if statement in your Test() function but i guess that was just copy paste work =P
Last edited on
yeah and no :P

but i already tried something like you suggested. but then if the user enters 1, it also displays the error.

the error is supposed to pop up only if there's a character or string inputted..
That's the problem with returning error codes.
Try one of the followings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pair<bool, double> Test(); //check pair.first
bool Test(double &num);

//or use exceptions
double Test(){
	double num = 0;
	if(!(cin>>num))
		throw 42;//error, interrupts normal flow
	return num;
}

int main(){
	try{//check for errors
		num1 = Test();
		num2 = Test();
	}
	catch(...){
		//handle the error 
		//by instance, keep reading.
	}
	//normal flow
	result = num1 + num2;
	return 0;
}


What if the user inputs 1? You'll get problems.
hey guys, it worked! :)

Thanks all of you for replying and helping me =)

Cheers!
Topic archived. No new replies allowed.