Error Checking in Program Help

Hey guys,

Have a program that I've been working on that is supposed to check whether a user inputs valid numbers. In the segment of code that I'm working on, you'll see the numbers, so that's self explanatory.

I think I have a pretty good idea of what I'm doing wrong, but I think it's good to post the whole thing to show my logic and the best way for me to fix it.

Segment of not-working code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 double value;
    if (! (value <= 100.0 && value >= 0.0))
      {
	cout << "Invalid value. Must be between 1 and 100." << endl;
	cout << "Program will now terminate. Try again later." << endl;
	return 1;
      }
    else
      {
	readVector (v1);
	readVector (v2);
	setOutputDigits (1);

	printVectors (v1, "Vector 1 => ");
	cout << endl;
	printVectors (v2, "Vector 2 => ");
	cout << endl;
      }


Here's my readVector function:

1
2
3
4
5
6
7
8
void
readVector(vector<double> & v)
{
  unsigned int i = 0;
  while ((i < v.size ()) && (cin >> v[i])) {
    i++;
  }
}


The code is designed to check whether the user inputs a valid floating point number, yet it doesn't actually check. I think it's because value has nothing to do with readVector so the check isn't occurring.

How would I format this code so that it would check the input and terminate when a number such as 101 or -5 is inputted?

Thanks!
Last edited on
You must give your variable value an actual value before you check it in your if statement. Here you haven't.
1
2
3
double value;
cin >> value;
if(//check 0<value<100) 
Right, I forgot to mention that I did use cin to see if that accomplished anything.

The problem with cin >> value in this case is that I'm using my readVector function to determine values.

My thinking is that I would set value to 0, check readVector(v1) and readVector(v2), and increment value. Not sure though.
Can anybody lead me in the right direction please?
Still looking for help with this.
Topic archived. No new replies allowed.