Error validating still on going

May 13, 2018 at 2:58am
I was doing homework while I encountered this problem. In the menu, I didn't want the menu part to accept anything but an integer, it works but... after putting in an integer it proceeds to still use the error validation from the menu...

Example Scenario:
Enter value : abc
Error! Value must be a proper integer. Enter value again : 1
Enter name : abc
Error! Value must be a proper integer. Enter value again : (this is the part i'm having a problem with

Why is this happening? What is the solution to this?

my code below :
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <conio.h>

using namespace std;

typedef struct subject{
	char name[50];
}sub;

typedef struct student{
	struct subject *sub; 
	int age;
	char name[50];
}stu;

void reg(stu *student);
int input(int d);

int main(int argc, char** argv) {
	int x=0;
	stu *student1 = (stu*)malloc(sizeof(stu));
	stu *student2 = (stu*)malloc(sizeof(stu));
	
	for(;;){
		system("cls");
		cout<<"____________________________"<<endl;
		cout<<"[1]Register student 1"<<endl;
		cout<<"[2]Register student 2"<<endl;
		cout<<"[3]Display Student names"<<endl;
		cout<<"[4]Exit"<<endl;
		cout<<"____________________________"<<endl;
		cout<<"Enter value : ";
		x=input(x);
		
		if(x==1){
			reg(student1);		
		}
		else if(x==2){
			reg(student2);
		}
		else if(x==3){
			cout<<student1->name<<endl;
			cout<<student2->name<<endl;
			getch();
		}
		else if(x==4){
			break;
		}
	}
	return 0;
}

void reg(stu *student){
	int lol;
	
	cout<<"Enter name : ";
	cin.ignore();
	cin.get(student->name, 100);
	
}

int input(int d){
	string line;
    bool cont = true;
    while(cont){
        getline(cin,line);
        stringstream ss(line);
        if(ss>>d){
        	if(ss.eof()){
        		cont = false;
        		return d;
			}
		}
		cout << "Error! Value must be a proper integer. Enter value again : ";
	}
}
Last edited on May 13, 2018 at 2:59am
May 13, 2018 at 3:19am
1
2
3
           } else {
           cout << "Error! Value must be a proper integer. Enter value again : ";
           }

instead perhaps?
Last edited on May 13, 2018 at 3:20am
May 13, 2018 at 3:25am
Still getting the same error :/ . I appreciate the help though
May 13, 2018 at 3:43am
Don't pass d in. Declare it as a local variable instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void reg(stu *student){
	cout << "Enter name : ";
	cin.get(student->name, 100);
}

int input() {
	int d = 0;
	string line;
	while (true) {
		getline(cin,line);
		stringstream ss(line);
		if (ss >> d)
			break;
		cout << "Error! Value must be a proper integer. Enter value again : ";
	}
	return d;
}

Last edited on May 13, 2018 at 3:44am
May 13, 2018 at 4:07am
@tpb it works now but I have this problem now

Scenario :
Enter number : 1
Enter name : peter
Enter number : Error! Value must be a proper integer. Enter value again : <---problem
May 13, 2018 at 4:16am
You should use cin.getline() instead of cin.get(). cin.get() leaves the newline in the input stream, so the next getline() call in input() will read a blank line since it hits the newline right away.

 
    cin.getline(student->name, 50);    // sizeof name is 50 (not 100) 


Or you could make name a string and read it with std::getline():
 
    getline(cin, student->name);    // if name is a std::string. 

May 13, 2018 at 4:32am
@tpb it's working now! Thank you very much
Topic archived. No new replies allowed.