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?


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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

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<<letter<<'\n';
 }
 else if(grade >= 80)
 {
	 letter = ('B');
	 std::cout<<letter<<'\n';
 }
 else if(grade >= 70)
 {
	 letter = ('C');
	 std::cout<<letter<<'\n';
 }
 else if(grade >= 60)
 {
	 letter = ('D');
	 std::cout<<letter<<'\n';
 }
 else if (grade < 60)
 {
	 letter = ('F');
	 std::cout<<letter<<'\n';
 }
 weighted1 = (first  * .20);
 weighted2 = (second * .20);
 weighted3 = (third  * .20);
 weighted4 = (fourth * .40);
 weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);

 std::cout << "Exam Grades: " <<  first << "," << second << "," << third << "," << fourth << '\n';
 std::cout << "This is the average for " << name << ": " << weighted_average << '\n';
 std::cout << "This is the letter grade: " << letter << '\n';
  }
  {
  for(count =0; count < num; count++)	
  {
		std::ofstream myfile;
        myfile.open ("grades.txt");
        myfile << "Writing this to a file.\n";
		myfile << name << '\n';
        myfile << weighted_average << '\n';
		myfile << letter << '\n';
        myfile.close();
		
  }
  break;
  }


case 2:

in_stream.open("grade.txt");
out_stream.open("finalgrade.dat");

double first, second, third, fourth;
in_stream >> 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.

1
2
3
4
5
6
7
 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.