No still looping

I'm learning c++ at my university. the problem is it is a "Try Another Y/N" but, whenever I chose option No the program still loops. What seems to be the problem?


#include <iostream>
#include <string>

using namespace std;

#define MAX_NAME_LENGTH 50

void input (double& b, double& c, double& d, double& e, double& f, double& g, double& h) ;
void calculation ( double& b, double& c, double& d, double& e, double& f, double& g, double& h, double& x);
void printingresult (double& x);

int main(){

char fname[50],ans;
double assign1,assign2,sw1,sw2,quiz1,quiz2,exam;
double final=0;

cout << "Enter student name: ";
cin.ignore();
cin.getline (fname,MAX_NAME_LENGTH);
input (assign1,assign2,sw1,sw2,quiz1,quiz2,exam);
calculation (assign1,assign2,sw1,sw2,quiz1,quiz2,exam,final);


do{
printingresult (final);
cout<<"\n\tTry another Y/N: ";
cin>>ans;
system ("cls");
main ();
} while (ans =='Y' || ans == 'Y');
return 0;
}

void input (double& b, double& c, double& d, double& e, double& f, double& g, double& h)
{
cout << "\nEnter grade for assignment 1: ";
cin >> b;
cout << "Enter grade for assignment 2: ";
cin >> c;
cout << "\nEnter grade for seatwork 1: ";
cin >> d;
cout << "Enter grade for seatwork 2: ";
cin >> e;
cout << "\nEnter grade for quiz 1: ";
cin >> f;
cout << "Enter grade for quiz 2: ";
cin >> g;
cout << "\nEnter grade for major exam: ";
cin >> h;
}
void calculation ( double& b, double& c, double& d, double& e, double& f, double& g, double& h, double& x)
{
x = (((b + c) / 2) * .10) + (((d + e) / 2) * .20) + (((f + g) / 2) * .30) + (h * .40);
}
void printingresult (double& x)
{
cout <<"\nFinal Grade: "<<x<<endl;
if (x>=99){
cout<<"1.00"<<endl;
}
else if(x>=96 && x<=98){
cout<<"1.25"<<endl;
}
else if(x>=93 && x<=95){
cout<<"1.50"<<endl;
}
else if (x>=90 && x<=92){
cout<<"1.75"<<endl;
}
else if (x>=87 && x<=89){
cout<<"2.00"<<endl;
}
else if (x>=84 && x<=86){
cout<<"2.25"<<endl;
}
else if (x>=81 && x<=83){
cout<<"2.50"<<endl;
}
else if (x>=78 && x<=80){
cout<<"2.75"<<endl;
}
else if (x>=75 && x<=77){
cout<<"3.00"<<endl;
}
else if (x>=0 && x<=74){
cout<<"5"<<endl;

}
}
you must not call main recursively (call main from inside main).
put the while loop around all the code you want to repeat, and get rid of that.
main is 'special' and has a few rules that make it different from every other function you write.
among those are
- its parameters come from the OS, so you can call the program with arguments and read them.
- it returns a code to the OS, not another part of the program
- it cannot be called recursively (there does not seem to be a technical limitation but the language forbids it when using strict compiler settings)
- nothing you can see calls main, its the 'starting point' of the program.

code tags help us read posts. use <> on the side editor or [] style (code and /code)
warnings and strict compiler settings would have told you what I said.
Last edited on
there does not seem to be a technical limitation

The restriction exists because some implementations insert code to initialize global variables, etc. into the beginning of the main function.
Last edited on
Consider the following pattern:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    bool done = false;
    
    while (!done)
    {
        // get input...
        // do calculation
        // show results
        std::cout << "Continue (y/n): ";
        char answer;
        std::cin >> answer;
        done = answer == 'n' || answer == 'N';
    }
}
Something like (not tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	for (bool done {}; !done; ) {
		char fname[50];
		double assign1, assign2, sw1, sw2, quiz1, quiz2, exam;
		double final = 0;

		cout << "Enter student name: ";
		//cin.ignore();
		cin.getline(fname >> ws, MAX_NAME_LENGTH);
		input(assign1, assign2, sw1, sw2, quiz1, quiz2, exam);
		calculation(assign1, assign2, sw1, sw2, quiz1, quiz2, exam, final);
		printingresult(final);

		char ans;

		cout << "\n\tTry another Y/N: ";
		cin >> ans;
		done = ans == 'n' || ans == 'N';
	}
}

Topic archived. No new replies allowed.