Whoops, another input problem

How do I make it so when the person enters a Test Score it only lets them enter a number 1-100?

Here is my code:
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
#include <iostream>
#include <string>  //String
#include <stdio.h>
#include <conio.h>

using namespace std;
class student
{
private :
	int sno;
	string name;  
	int g1,g2,g3,g4;
	int absen;
public :
	void Getdata()
	{
		cout << "Enter Student ID:  " << endl; 
		cin >> sno;
		cout << "Enter Student NAME:  " << endl;
		cin.get(); 
		getline(cin, name); 
		cout << "Enter Student TEST SCORES (3):  " << endl;
		cin >> g1 >> g2 >> g3;
		cout << "Enter # of Absences:  " << endl;
		cin >> absen;
	}
	void Putdata()
	{
		cout  << "Student ID:  " << sno << endl;
		cout << "Student NAME:  " << name << endl;
		cout << "# of Absences:  " << absen << endl;
		if (absen == 0) {
  g4 = 2;
} else {
  g4 = 0;
}

cout << "Grade:  " << (g1+g2+g3 + g4)/3 << endl;
		if ((((g1+g2+g3 + g4)/3) >= 73))
		cout << "Grade:  " << (g1+g2+g3)/3 << endl;
		if ((((g1+g2+g3)/3) >= 73))
		{cout << "Successful" << endl;}
		else
		{cout << "Unsuccessful" << endl;}
	};
};


int main()
{
	student s;
	s.Getdata();
	s.Putdata();
	getch();
	return 0;
}
Last edited on
this is difficult because it is inside a class and because i have it entering 3 things of data on one line, that can be changed i believe

idk how else to set this up.
after enter a student id

the cin.get()
Hopefully u change to
cin.ignore();
clear the int buffer before enter string else your input might get problem for your compiler

not really understand about your question
and i see your program
u already know how to do for the if else statement isn't?


so if enter a score 1-100
just use a ifelse statement will do.
or the code is you copy from other?

1
2
3
4
5
6
7
8
9
10
11
12
13
bool match = true;//Intialize boolean
double testScore = 0;//Score might be 72.5

do{
    cout << "Enter test Score : ";
    cin  >> testScore;
    if( testScore < 0 || testScore > 100 ){
           cout << "Invalid Input ! " << endl;
           testScore = false;
    }
    else
           testScore = true;
}while( match );


i wonder if u know what if a statement is . then u should know how to use for the condition for the ifelse already

this is difficult because it is inside a class and because i have it entering 3 things of data on one line, that can be changed i believe

idk how else to set this up.


Do you know how to implement function outside of class?
1
2
3
4
5
6
7
8
9
10
11
class A{
private:
     int testInt;
public:
      int Data( int );
};

int A :: Data ( int testMODE ){
     testInt = testMode;
}

and for good practice .
Please always initialize your value in private data using constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
class AClass{
private:
     string A , B;
     int intA;
public:
     AClass();
};

AClass::AClass(){
    A = "";
    B = "";
    intA = 0;
}
got it but for some reason it keeps repeating the enter first score updated


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
81
82
83
84
85
#include <iostream>
#include <string>  
#include <stdio.h>
#include <conio.h>

using namespace std;
class student
{
private :
	int sno;
	string name;  
	int g1,g2,g3,g4;
	int absen;



public :
	void Getdata()
	{
		cout << "Enter Student ID:  " << endl;    // User enters the Student ID
		cin >> sno;
		cout << "Enter Student NAME:  " << endl;  // User enters the Student Name
		cin.ignore();
		getline(cin, name); 
		bool match = true;
		do{
			cout << "Enter Student TEST SCORE #1:  ";
			cin >> g1;
			if (g1 < 0 || g1 > 100 ){
				cout << "Invalid Input ! " << endl;
				g1 = false;
			}
			else
				g1 = true;
		}while ( match );
		do{
			cout << "Enter Student TEST SCORE #2:  ";
			cin >> g2;
			if(g2 < 0 || g2 > 100 ){
				cout << "Invalid Input ! " << endl;
				g2=false;
			}
			else
				g2 = true;
		}while ( match );
		do{
			cout << "Enter Student TEST SCORE #3:  ";
			cin >> g3;
			if (g3 < 0 || g3 > 100 ){
				cout << "Invalid Input ! " << endl;
				g3 = false;
			}
			else
				g3 = true;
		}while ( match );
		cout << "Enter # of Absences:  " << endl;  // User enters the number of Absences
		cin >> absen;
	}
	void Putdata()
	{
		cout  << "Student ID:  " << sno << endl;   // Displays the student ID
		cout << "Student NAME:  " << name << endl;  // Dislays the student Name
		cout << "# of Absences:  " << absen << endl; // Displays the number of Absences, may effect grade
		if (absen == 0) {
			g4 = 2;
		} else {
			g4 = 0;
		}
		cout << "Grade:  " << (g1+g2+g3)/3 << endl;  // Displays grade and whether the student is Successful/Unsuccessful
		if ((((g1+g2+g3)/3) >= 73))
		{cout << "Successful" << endl;}
		else
		{cout << "Unsuccessful" << endl;}
	};
};


int main()
{
	student s;
	s.Getdata();
	s.Putdata();
	getch();
	return 0;
}
Last edited on
i don't know why you set your g1 into true and false

you should set
1
2
3
match = true;

match = false;

at line 31 and line 34
did you know what is boolean doing actually?

and line 41 and 44
please use another boolean to represent the do while loop.

same to g3 and g4

please change all afterwards post your problem again
Topic archived. No new replies allowed.