### Loop depending on user input

EDIT:

I'm trying to get this program to loop the number of times I get it to input. The program compiles alright, and it does loop when I tell it too, but how do I output the grades of the multiple students?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235`` ``````#include #include #include #include #include int weighted1 = 0; int weighted2 = 0; int weighted3 = 0; int weighted4 = 0; int weighted_average = 0; const int MAX = 20; int flag = 0; int choice; double sum = 0; double average = 0; char name [10]; char letter; char file [MAX]; int num = 0; int count = 0; int main( ) { std::ifstream in_stream; std::ofstream out_stream; in_stream.open("grade.txt"); out_stream.open("finalgrade.dat"); double first, second, third, fourth, fifth; in_stream >> first >> second >> third >> fourth >> fifth; //std::cout >> " 1: " >> first >> " 2: " >> second >> double grade = 0.0; grade = (first + second + third + fourth)/4; //this computes the name //output bool menu = true; while (menu != false) { std::cout << "Would you like to open as keyboard or file?" << '\n'; std::cout << "1. keyboard" << '\n'; std::cout << "2. file" << '\n'; std::cin >> choice; switch (choice) { case 1: std::cout << "How many students? "; std::cin >> num; for(count =0; count < num; count++) { { std::cout << "Student's Name: "; std::cin >> name; } do { flag = 0; std::cout << "Please input your first exam grade and press enter: \n"; std::cin >> first; if ((first < 0) || (first > 100)) { std::cout << "You've entered invalid data!" << '\n'; flag = 1; } }while (flag == 1); do { flag = 0; std::cout << "Please input your second exam grade and press enter: \n"; std::cin >> second; if ((second < 0) || (second > 100)) { std::cout << "You've entered invalid data!" << '\n'; flag = 1; } }while (flag == 1); do { flag = 0; std::cout << "Please input your third exam grade and press enter: \n"; std::cin >> third; if ((third < 0) || (third > 100)) { std::cout << "You've entered invalid data!" << '\n'; flag = 1; } }while (flag == 1); do { flag = 0; std::cout << "Please input your final exam grade and press enter: \n"; std::cin >> fourth; if ((fourth < 0) || (fourth > 100)) { std::cout << "You've entered invalid data!" << '\n'; flag = 1; } }while (flag == 1); grade = (first + second + third + fourth)/4; sum = first + second + third + fourth; average = sum/4; //Letter grade and it's weighted averages letter = 'A'; letter = 'B'; letter = 'C'; letter = 'D'; letter = 'F'; if(grade >= 90) { letter = ('A'); std::cout<= 80) { letter = ('B'); std::cout<= 70) { letter = ('C'); std::cout<= 60) { letter = ('D'); std::cout<> first >> second >> third >> fourth; //std::cout >> " 1: " >> first >> " 2: " >> second >> double grade = 0.0; grade = (first + second + third + fourth)/4; letter = 'A'; letter = 'B'; letter = 'C'; letter = 'D'; letter = 'F'; if(grade >= 90) letter = ('A'); else if(grade >= 80) letter = ('B'); else if(grade >= 70) letter = ('C'); else if(grade >= 60) letter = ('D'); else if (grade < 60) letter = ('F'); weighted1 = (first * .20); weighted2 = (second * .20); weighted3 = (third * .20); weighted4 = (fourth * .40); weighted_average = (weighted1 + weighted2 + weighted3 + weighted4); std::cout << "Enter file name: "; std::cin >> file; if(file!="grade.txt") { std::cout << std::fixed << "The average grade for: " << name << '\n'; std::cout << "average in grade.txt is: "<< weighted_average << std::setprecision(2) << '\n'; std::cout << "and the letter grade is: " << letter << '\n'; } in_stream.close(); out_stream.close(); } system ("pause"); return 0; } } ``````
Last edited on
closed account (Dy7SLyTq)
firstly, when you call functions you shouldnt put the return type. secondly just get a number from the user and have another variable start at zero and count up to it
Line 63 big NO! Recursion is already expensive enough, why call the entire program in the program itself?? Plus you didn't add a break after that call to main
closed account (Dy7SLyTq)
good catch smac. @op: if you just do return (or if your deeply nested setjmp) it will return to the calling function, so you can just unwind
I tried coming up with something like this, at the beginning of the "void keyboard" function, but now it won't recognize it.

 ``1234567`` `````` std::cout << "How many records would you like to input?" << '\n'; std::cin >> records; for (int input = 0; input < records; records ++) { std::cin >> input; std::cout << "You entered " << input << "times." << '\n'; }``````
closed account (Dy7SLyTq)
a) its just keyboard not void keyboard
b) @smac: you dont need to have a break in a default. think about it for a sec im sure youll get it ;)
c) why is menu bool?
d) why are you upping records? its gonna be an infinite loop because it will always be greater than zero (unless they want to enter negative records)
e) where do you expect all of keyboards args to come from. you call keyboard(), but have keyboard(double, double, double, etc)
f) why are you using passed args for input? that makes no sense. and why are you testing doubles against ints?
g) why do you call get_from()? i see no function called get_from(). i think you mean get_file(). does your compiler really allow all of this?
h)why does getfile have those args? you call a function with no args
Last edited on
a. When I did that, it gave me the "Error: too few arguments in function call"
c. Because it's supposed to start out with the menu.
d. Not sure why, my mistake there.
g. That's supposed to come later.
closed account (Dy7SLyTq)
a) if you look at all of the other letters youll see why it says that.
c) yeah i see that its supposed to start with menu, but why does it return bool?
I don't know. I just wanted it to return when the user finished inputting the first time. I edited my code, maybe it can answer those other questions.
Last edited on
closed account (Dy7SLyTq)
is that edited piece above your actual code? if so you didnt listen to a word i said
No, I just rewrote it a little.
closed account (Dy7SLyTq)
well take care of all the things i said were wrong then repost it
I rewrote a more simplistic version of the code I had before without all the arguments and double variables. Now the problem lies within writing all the students to grade.txt instead of just the last one I enter.
Topic archived. No new replies allowed.