Why is my switch case not looping?

Oct 17, 2017 at 7:24pm
This is my first time using the forum so im sorry for the bad format but please if someone can assist me it would be great, thanks. this is my code i want to to loop the whole program depending on the number of students but it doesn't for some reason.

#include<iostream>
#include <iomanip>
using namespace std;
void main() {
int no_students, no_courses, count = 1.0, q = 1;
float credit, sum = 0, sum_credits = 0;
char lgrade, fname[100], lname[100];
cout << "Enter number of students: ";
cin >> no_students;
while (q <= no_students)
{
{
cout << "Enter First name: ";
cin >> fname;
cout << "Enter Last name: ";
cin >> lname;
cout << "How many courses do you have? ";
cin >> no_courses;
while (count <= no_courses) {
cout << "Enter letter grade for course number " << count << " : ";
cin >> lgrade;

switch (lgrade) {
case 'A': cout << "Enter credit hours for course number " << count << " : ";
cin >> credit;
lgrade = 4.0; sum = sum + (credit * 4);
sum_credits += credit; break;

case 'B': cout << "Enter credit hours for course number " << count << " : ";
cin >> credit;
lgrade = 3.5; sum = sum + (credit * 3.5);
sum_credits += credit; break;

case 'C': cout << "Enter credit hours for course number " << count << " : ";
cin >> credit;
lgrade = 3.0; sum = sum + (credit * 3.0);
sum_credits += credit; break;

case 'D': cout << "Enter credit hours for course number " << count << " : ";
cin >> credit;
lgrade = 2.0; sum = (sum + credit * 2.0);
sum_credits += credit; break;

case 'F': cout << "Enter credit hours for course number " << count << " : ";
cin >> credit;
lgrade = 0.0; sum = (sum + credit * 0.0);
sum_credits += credit; break;

default: cout << "Invalid letter grade\n";
continue;
}
count++;

}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);


cout << "\nTotal GPA Grade for " << fname << " " << lname << " is: " << sum / sum_credits << endl;
}
q++;

}

}
Last edited on Oct 17, 2017 at 7:36pm
Oct 17, 2017 at 7:47pm
Have a look at all the warnings your code produces.

main.cpp(10): warning C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
main.cpp(31): warning C4244: '=': conversion from 'double' to 'char', possible loss of data
main.cpp(36): warning C4244: '=': conversion from 'double' to 'char', possible loss of data
main.cpp(36): warning C4244: '=': conversion from 'double' to 'float', possible loss of data
main.cpp(41): warning C4244: '=': conversion from 'double' to 'char', possible loss of data
main.cpp(41): warning C4244: '=': conversion from 'double' to 'float', possible loss of data
main.cpp(46): warning C4244: '=': conversion from 'double' to 'char', possible loss of data
main.cpp(46): warning C4244: '=': conversion from 'double' to 'float', possible loss of data
main.cpp(51): warning C4244: '=': conversion from 'double' to 'char', possible loss of data
main.cpp(51): warning C4244: '=': conversion from 'double' to 'float', possible loss of data


If you fix these, maybe your program will work as expected.
Oct 17, 2017 at 8:46pm
i fixed them but the problem is still there :/
Oct 17, 2017 at 11:51pm
error: '::main' must return 'int'

your inner loop needs the variable to be re initialized.

count = 1;
while (count <= no_courses) ...
Last edited on Oct 17, 2017 at 11:58pm
Topic archived. No new replies allowed.