INCORPORATE IF ELSE IF STATEMENT INTO WHILE

I need to incorporate an if else that only add points if they are good else make
them enter again. Can someone please help me with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//This program will calculate and display student letter grade
#include <iostream>
#include <iomanip> //needed for manipulating output
#include <cmath>
#include <cstdlib>//needed to clear screen
#include <string>

using namespace std;


int main()
{

[code]
//Declare and initialize variables
double avg = 0;
double grade = 0;
double finalGrade = 0;
double points = 0;
double totalPoints = 0;
int numOfTest= 0;
char letterGrade;
char out1 = 'y';
char in1 = 'Y';
string fname, lname;

cout<<"Would you like to use this grading program ? <Y> or <y>"<<endl;
cout<<"OR enter <N> or <n> to exit"<<endl;
cin>>out1;
while(out1== 'Y'|| out1 =='y')//allows user to enter or exit program

{

system("cls");//clears screen
cout <<"Please enter your first name: " <<endl;
cin >>fname;//user enters name
cout <<"\nPlease enter your last name: " <<endl;
cin >>lname;
system("cls");
while(in1== 'Y' || in1== 'y')
{

cout << fname<<","<<endl;
cout<<"\nPlease enter the grade point value of the assignment:" <<endl;

cin >>points;
totalPoints = totalPoints+points;//Adds point total
numOfTest++;//adds number of grades entered
cout <<"\nPlease enter your score:"<<endl;
cin >> grade;

finalGrade = finalGrade + grade;//adds grade totals together

while(grade < 0 || grade > points)//decision making statement to determine if user entered correct values

{
cout<<fname <<" YOU ENTERED A INVALID VALUE"<<endl;
cout<<"Enter number between 0 and "<<points<<endl;
cin >> grade;

}

cout<<"Do you have another grade? <Y> or <y> yes";
cout<<"\nOr click <N> or <n> to continue to next screen"<<endl;

cin>> in1;//determines if user will leave the loop



}
avg = (finalGrade/totalPoints)*100;//Determines average

system("cls");
cout << setprecision(1);//sets decimal places
cout <<fixed<<showpoint;
cout <<fname <<" You have " <<numOfTest<< " grade(s) entered"<<endl;
cout <<" \nYour Average is: "<<avg<<"%"<<endl;
cout <<endl;
cout <<"Total Points possible: "<<totalPoints<<endl;
cout <<endl;
cout <<"Total points earned: "<<finalGrade<<endl;
cout <<endl;


if(avg <= 100.99 && avg >= 94.00)//determines grade
cout<<fname << " Your grade is a A"<<endl;
else
if(avg <= 93.99 && avg >= 90.00)
cout<<fname << " Your grade is a A-"<<endl;
else
if(avg <= 89.99 && avg >= 87.00)
cout<<fname << " Your grade is a B+"<<endl;
else
if(avg <= 86.99 && avg >= 83.00)
cout<<fname << " Your grade is a B"<<endl;
else
if(avg <= 82.99 && avg >= 80.00)
cout<<fname << " Your grade is a B-"<<endl;
else
if(avg <= 79.99 && avg >= 77.00)
cout<<fname << " Your grade is a C+"<<endl;
else
if(avg <= 76.99 && avg >= 73.00)
cout<<fname << " Your grade is a C"<<endl;
else
if(avg <= 72.99 && avg >= 70.00)
cout<<fname << " Your grade is a C-"<<endl;
else
if(avg <= 69.99 && avg >= 66.00)
cout<<fname << " Your grade is a D+"<<endl;
else
if(avg <= 65.99 && avg >= 60.00)
cout<<fname << " Your grade is a D"<<endl;
else
if(avg <= 59.99 && avg >= 0)
cout<<fname << " Your grade is a E"<<endl;


fname, lname;
avg = 0;
grade = 0;
finalGrade = 0;
points = 0;
totalPoints = 0;
letterGrade;
out1 = 'y';
in1 = 'Y';


cout<<"\nWould you like to continue to run this grading program <Y> or <y>"<<endl;
cout<<"OR enter <N> or <n> to exit"<<endl;
cin>>out1;
}


return 0;
}[/code]
Last edited on
I find the easiest way to do this is to use a function.
1
2
3
4
5
6
7
8
9
10
int get_points ()
{   int points;

    do
    {  cout<<"\nPlease enter the grade point value of the assignment:" <<endl; 
        cin >>points; 
    }   
    while (points <= 0 || points > 100);  // Or whatever your criteria are
    return points;
}

The function doesn't return unless the value is valid. This also simplifies your main line logic.

If you have a number of integer value to ask for, you generalize the routine:
1
2
3
4
5
6
7
8
9
10
int get_int (const string & prompt, int min, int max)
{   int value;

    do
    {   cout << prompt << endl;  
        cin >> value;   
    }          
    while (value < min || value > max) 
    return value; 
}


Line 122, 128: These statements don't do anything.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on
I'm sorry I never used the do function would I have to declare it as a new variable?
do while is a type of loop. It is not a function or variable.
http://www.cplusplus.com/doc/tutorial/control/

1
2
3
4
  do
  { <statements> 
  }
  while ( <condition> );

I was able to get the program to work correctly by moving the calculations down. Thank you for your help

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//This program will calculate and display student letter grade
#include <iostream>
#include <iomanip> //needed for manipulating output
#include <cmath>
#include <cstdlib>//needed to clear screen
#include <string>

using namespace std;


int main()
{


	
	//Declare and initialize variables
	double avg = 0;
	double grade = 0;
	double finalGrade = 0;
	double points = 0;
	double totalPoints = 0;
	int numOfTest= 0;
	char letterGrade;
	char out1 = 'y';
	char in1 = 'Y';
	string fname, lname;
	
	cout<<"Would you like to use this grading program ? <Y> or <y>"<<endl;
	cout<<"OR enter <N> or <n> to exit"<<endl;
	cin>>out1;
    while(out1== 'Y'|| out1 =='y')//allows user to enter or exit program
		
{
	
	system("cls");//clears screen
	cout <<"Please enter your first name: " <<endl;
	cin  >>fname;//user enters name
	cout <<"\nPlease enter your last name: " <<endl;
	cin  >>lname;
	system("cls");
	while(in1== 'Y' || in1== 'y')
{
	
	cout << fname<<","<<endl;
	cout<<"\nPlease enter the grade point value of the assignment:" <<endl;
	
	cin  >>points;
			
	cout <<"\nPlease enter your score:"<<endl;
	cin >> grade;
	
	
	if(grade < 0 || grade > points)
	{
	
		while(grade < 0 || grade > points)//decision making statement to determine if user entered correct values
		
	{		
		cout<<fname <<" YOU ENTERED A INVALID VALUE"<<endl;
		cout<<"Enter number between 0 and "<<points<<endl;
		cin >> grade;
		
	}
	}
	
		
	cout<<"Do you have another grade? <Y> or <y> yes";
	cout<<"\nOr click <N> or <n> to continue to next screen"<<endl;
	
	cin>> in1;//determines if user will leave the loop
	
	
	
}
			finalGrade = finalGrade + grade;//adds grade totals together
			totalPoints = totalPoints+points;//Adds point total
			numOfTest++;//adds number of grades entered
			avg = (finalGrade/totalPoints)*100;//Determines average
		 
	system("cls");
	cout << setprecision(1);//sets decimal places
    cout <<fixed<<showpoint;
    cout <<fname <<" You have " <<numOfTest<< " grade(s) entered"<<endl;
	cout <<" \nYour Average is: "<<avg<<"%"<<endl;
	cout <<endl;
    cout <<"Total Points possible: "<<totalPoints<<endl;
    cout <<endl;
    cout <<"Total points earned: "<<finalGrade<<endl;
    cout <<endl;
	
			
	if(avg <= 100.99 && avg >= 94.00)//determines grade
		cout<<fname << " Your grade is a A"<<endl;
	else
	if(avg <= 93.99 && avg >= 90.00)
		cout<<fname << " Your grade is a A-"<<endl;
	else
	if(avg <= 89.99 && avg >= 87.00)
		cout<<fname << " Your grade is a B+"<<endl;
	else
	if(avg <= 86.99 && avg >= 83.00)
		cout<<fname << " Your grade is a B"<<endl;
	else
	if(avg <= 82.99 && avg >= 80.00)
		cout<<fname << " Your grade is a B-"<<endl;
	else
	if(avg <= 79.99 && avg >= 77.00)
		cout<<fname << " Your grade is a C+"<<endl;
	else
	if(avg <= 76.99 && avg >= 73.00)
		cout<<fname << " Your grade is a C"<<endl;
	else
	if(avg <= 72.99 && avg >= 70.00)
		cout<<fname << " Your grade is a C-"<<endl;
	else
	if(avg <= 69.99 && avg >= 66.00)
		cout<<fname << " Your grade is a D+"<<endl;
	else
	if(avg <= 65.99 && avg >= 60.00)
		cout<<fname << " Your grade is a D"<<endl;
	else
	if(avg <= 59.99 && avg >= 0)
		cout<<fname << " Your grade is a E"<<endl;
		
		
	fname, lname;	
	avg = 0;
	grade = 0;
	finalGrade = 0;
	points = 0;
	totalPoints = 0;
	letterGrade;
	out1 = 'y';
	in1 = 'Y';
	
	
	cout<<"\nWould you like to continue to run this grading program <Y> or <y>"<<endl;
	cout<<"OR enter <N> or <n> to exit"<<endl;
	cin>>out1;
}
			
		
	
	return 0;
}[code]
[/code]
Last edited on
Topic archived. No new replies allowed.