Hi guys I'm new to C++ in college and am struggling with some codes. Here is one that has me stumped. It is supposed to prompt the user for 5 numbers and output the average of the numbers and assign a letter grade to it. Keep in mind I am really new(this is my third code) and would appreciate it being kept simple and an explanation. Thanks so much!
//This program will prompt the user for 5 numbers and output the average of these numbers
//and a letter grade
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int avg=0;
int sum=0;
int num1=0;
int num2=0;
int num3=0;
int num4=0;
int num5=0;
char letter;
letter='A';
letter='B';
letter='C';
letter='D';
letter='F';
double name;
//Prompt for 5 numbers and your name
string firstname;
cout << "Please input your name\n" << endl;
cin >> name;
cout << "Okay "<< name <<" please input five numbers";
cin >> num1;
cin >> num2;
cin >> num3;
cin >> num4;
cin >> num5;
//Calculate the average
sum=num1+num2+num3+num4+num5;
avg=((num1+num2+num3+num4+num5)/5);
//Determine letter grade based on average
if(avg>=90);
letter = ('A');
elseif(avg>=80);
letter = ('B');
elseif(avg>=70);
letter = ('C');
elseif(avg>=60);
letter = ('D');
else (avg<60);
letter = ('F');
else cout << "There is an error";
system ("pause");
return 0;
}
It would be helpful if you indicated what kind of problem you're having.
Doesn't compile? Doesn't run? Gives the wrong answer?
What I see is:
Lines 38,40,42,44 - Remove the ; after the condition in your if statements. it doesn't belong there.
Line 48: You have an else, but no matching if.
1) Your if/else-if statements shouldn't have a ; at the end.
2) Your reading a name into a double, it should be a string
3) The avg<60 code should either be else-if or else without the avg<60, resulting in a F grade.
4) You need to print the grade at the end.
This whole process would be a bit simpler with arrays but I'm not sure you'll have covered them yet.
Yeah I'm sorry I only have access to the program that runs the code while I am in class so I kinda forget where things went wrong, but I remember my first problem I think started somewhere on line 25 or near there I couldnt get the program to read back my name after I typed it in. Also I have no idea what an array is sorry :P