for loop

hi, my prof recently assigned us a project in which we had to use a for loop. the program would have to ask the user for a number and the for loop would then iterate that number of times. now in the for loop the program asks for another number and if that number is -1 then the program has to terminate, easy enough. problem is, she said we have to do this without break, continue, goto or multiple return statement, i've gotten this far, any suggestions as to how i can terminate the program when -1 is entered as the value for studentnumber?

#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{
double Percentagegrade;
int studentnumber;
char lettergrade;
int numberofstudents;
int numberofstudentstoread;

cout << "Welcome to the Percentage to Letter Grade Converter!\n";

cout<< "Please enter number off students to be processed: ";
cin >> numberofstudents;

for (int numberofstudentstoread = 1; numberofstudentstoread <= numberofstudents; numberofstudentstoread++)
{

do
{
cout << "Please enter Student Number: ";

cin >> studentnumber;

if ( (studentnumber <= 99999999) && ( studentnumber != -1))
{
cout << "Please re-enter your student number." <<endl;
cout << "Your student number should have 9 digits and should not start with 0.\n";
cout <<"\n";
}

}while ( (studentnumber <= 99999999) && ( studentnumber != -1));


if ( studentnumber == -1)
{
cout << "\n";
cout << "The program will now Terminate\n";
cout << " Goodbye =)" <<endl;
}

else
{
do
{
cout << "Please Enter A Percentage Between 0 and 100(%): ";

cin >> Percentagegrade;

if (( 101 <= Percentagegrade ) || ( Percentagegrade <=0 ))
{
cout << "please enter a perentage between 1 and 100.\n";
cout << "\n";
}

}while (( 101 <= Percentagegrade ) || ( Percentagegrade <=0 ));


if ( 85.5 < Percentagegrade )
{
lettergrade = 'A';
}
else if ( 73.5 < Percentagegrade )
{
lettergrade = 'B';
}
else if ( 61.5 < Percentagegrade )
{
lettergrade = 'C';
}
else if ( 49.5 < Percentagegrade )
{
lettergrade = 'D';
}
else
{
lettergrade = 'F';
}

cout << " Student number:" << studentnumber << ", percentage\n";
cout << "grade: " << Percentagegrade << "%.\n";
cout << " This student will reveive a " <<lettergrade << ".\n";
}
}if (studentnumber != -1)
return 0;
}
Last edited on
You should wrap your code in CODE tags; it would make it easier to read.

In any case, after taking a quick look through it, couldn't you use a while loop? For example, right after reading in the student number, could you just wrap the rest of the program in a while loop? For example,

1
2
3
4
while (studentnumber != -1) 
{
   do rest of program
}


If studentnumber == 1, arrange it so the program just drops through the end and terminates.
No break, continue, goto, or multiple return statements
Maybe I'm shooting myself in the foot here but I did eventualy come up with a solution, a for loop with mutiple ways of breaking it, easy.
This program isn't perfect by the way you still need to test the studentNumber
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	double Percentagegrade = 0.0;
	int studentnumber = 0;
	char lettergrade = 'F';
	int numberofstudents = 0;

	cout << "Please enter the amount of students: ";
	cin >> numberofstudents;

	for(int i = 0; i < numberofstudents, studentnumber != -1; i ++)
	{
		cout << "-------------------------------------" << "\n";
		cout << "Please enter the students number: ";
		cin >> studentnumber;
		cout << "Please enter a percentage between 0 and 100: ";

		cin >> Percentagegrade;
		cout << "The student achieved a ";
		if ( 85.5 < Percentagegrade )
		{
			cout << 'A';
		}
		else if ( 73.5 < Percentagegrade )
		{
			cout << 'B';
		}
		else if ( 61.5 < Percentagegrade )
		{
			cout << 'C';
		}
		else if ( 49.5 < Percentagegrade )
		{
			cout << 'D';
		}
		else
		{
			cout << 'F';
		}
		cout << "\n";
	}

	if(studentnumber == -1)
	{
		cout << "The program will now terminate" << "\n";
		cout << "Bye";
	}
	return 0;
}


Note: I have used your variables just for the sake of it, please if you want to be a serious code learn how to initilize and name variables properly, it helps people read your code.
thank you very much for replying, it solved my problem :)
and i apologize for my post, this is my first time posting and i have no idea how to wrap everything in a code tag, i am just starting to learn how to code and prof only has a masters ):
anyways, thanks for all the help!
Topic archived. No new replies allowed.