[try Beta version]
Not logged in

 
variable problem

Nov 1, 2010 at 11:53pm
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
double a,b,c,d,e;


cout << "a: ";
cin >> a;
if(a <= 0)
{ cout << "invalid input\n";
system("pause");
return 0; }

cout << "b: ";
cin >> b;
if (b <= 0)
{ cout << "invalid input\n";
system("pause");
return 0; }


cout << "c: ";
cin >> c;
if (c <= 0)
{ cout << "invalid input\n";
system("pause");
return 0; }

d=a+b+c;
if (d>150)
{ e=100; }
else
{ e=150; }


cout << "e";
system("pause");
return 0;
}

The problem is when i enter wrong input, like 5z, it first prints " b: " and than it prints "invalid input"
But it's suppose to print" invalid input "just after i entered something wrong.
It works normally if I type " z5 " , but it doesnt if i type " 5z ".

Please help
Thx
Nov 2, 2010 at 12:33pm
does anyone know ?
Nov 2, 2010 at 1:30pm
when you're entering "5z", cin>>a; reads '5' into the a and the program continues.
Nov 2, 2010 at 1:38pm
ye, but why is it happening ? and how should i rebuild it to work correctly ?
Nov 2, 2010 at 1:43pm
it is the way the streams work. You can check if there is something in a stream after the cin>>a worked. if yes, then user entered some rubbish.
Nov 2, 2010 at 1:48pm
please use code tags next time!

cin works like a queue.
If you ask cin to read a double it will try to do so and if it can remove this for it's queue.
So with the input of '5z' it will read in 5, remove it and moves on to cout << "b: ", then the 2nd time you do a cin it will find the remaining 'z' and your code will print out "invalid input".

To fix this, what i do is always have cin put whatever is in the queue in a string, and parse the data myself. This will make sure that '5z' will be detected as invalid by your parser and that the entire input gets used and not only up till the point where the input is valid.

If you don't care about your program seeing '5z' as valid you can use cin.ignore() to empty the queue before every use. But then your program will see '5z' as being the double 5, but it will not pass the remaining 'z' on to your next cin.
Last edited on Nov 2, 2010 at 1:50pm
Nov 2, 2010 at 2:45pm
bah, i thought i figure it out, but obviously i did not.
can u pls recode just this paragraph so i might solve the problem :P

cout << "a: ";
cin >> a;
if(a <= 0)
{ cout << "invalid input\n";
system("pause");
return 0; }
Nov 2, 2010 at 3:08pm
ones more: please use code tags!



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
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
double a,b,c,d,e;


cout << "a: ";
cin >> a;
if(a <= 0) 
{ cout << "invalid input\n"; 
system("pause");
return 0; }
cin.ignore();
cout << "b: ";
cin >> b;
if (b <= 0)
{ cout << "invalid input\n"; 
system("pause");
return 0; }
cin.ignore();
cout << "c: ";
cin >> c;
if (c <= 0)
{ cout << "invalid input\n"; 
system("pause");
return 0; }
cin.ignore();
d=a+b+c;
if (d>150)
{ e=100; }
else
{ e=150; }
cout << "e";
system("pause");
return 0;
}


This will fix the problem of 'z' being passed on to the next cin, but not that of '5z' being a valid read in for '5'.
Nov 2, 2010 at 3:25pm
Oke, i got that, but i don't want to be a valid read anything except the numbers. So, if some letter is entered, print "invalit input"
Also, in your case it just ignores the z, but if i write 5zz, its same again :/


Nov 2, 2010 at 3:30pm
and sorry cos i didn't use code tag :P
Nov 2, 2010 at 5:00pm
try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "a: ";
cin >> a;
if(a <= 0) 
{
cout << "invalid input\n"; 
system("pause");
return 0;
}
char p = cin.get(); // here we try to get the next char in the queue.
if (cin.good()) // If cin.get() was successful there was more in the queue then just a double
{
cout << "invalid input\n"; 
system("pause");
return 0;
}
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


This is not the safest way to do this, there may be leftover white-spaces or end-of-line chars in the queue that will kill your program.
As I was saying, the safest way to do this is to put everything in a string by using getline(cin, string) and write your own parser to parse the string to a double.
Also, in your case it just ignores the z, but if i write 5zz, its same again :/

Yes sorry cin.ignore() removes one entry to clear the buffer there are 2 ways:
1) Ignore to the end of file:
cin.ignore(std::numeric_limits<std::streamsize>::max()) or cin.flush()
2) Ignore to the end of line:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
Last edited on Nov 2, 2010 at 5:07pm
Nov 2, 2010 at 9:30pm
thank you man, u helped me a lot !
Topic archived. No new replies allowed.