Hello forum. Im new to this site and I am new to C++ programming. I just started a class at school and wow im confused. Its propably a lot easier then i think but all the different words and characters in programming are too much for me to understand it all. I just got a assignment to create a c++ algorithm that
A user will enter five test scores. Calculate and display the average of the five test scores.
Im checking out the tutorial and the similiar problem. I still feel like im doing it wrong but Im going to post my result when im done and hopefully you guys can help me with where i go wrong. That tutorial looks good and I feel like once i read it fully it might help me get unconfused in this class
I corrected a few things and this is where im at now but I think Im doing something wrong with getting the average but I think I have the sum right.
#include <iostream>
using namespace std;
int main()
{
cin >> "Enter score of test 1: ";
cin >> "Enter score of test 2: ";
cin >> "Enter score of test 3: ";
cin >> "Enter score of test 4: ";
cin >> "Enter score of test 5: ";
Did you try compiling any of this code? I can see several errors or just things missing. However the previous version looks, broadly speaking, closer to me. It seems you are drifting away from a solution.
A hint: you will need some variables in which to store the five scores. Then you will need two more variables, one for the sum, and one for the average. I suggest you make these of type double.
You could define these at the top of your code, then use them at the relevant places in the code where they are needed.
cout << "Enter score of test 1: ";
cin >> score1;
cout << "\nEnter score of test 2: ";
cin >> score2;
cout << "\nEnter score of test 3: ";
cin >> score3;
cout << "\nEnter score of test 4: ";
cin >> score4;
cout << "\nEnter score of test 5: ";
cin >> score5;
Note, above, each cout is a prompt to the user, and is followed by the corresponding cin which will get the input from the user. I inserted the newline code "\n" at the start of the prompts, to put each one on its own line.
That still leaves the rest for you to work on. What you have above is roughly heading in the right direction, but has a few errors, including the cin at the end which is not correct. I'm trying not to do the whole thing as it would be counter-productive. Sorry if this isn't entirely helpful to you.
no i understand and im glad because I dont want anyone just doing it for me because i want to get more familiar with all of it. instead of the \n can i just put endl; to end the line? and if so would i put the endl; like this
cout << "Enter score of test 1: ";
cin >> score1; endl;
cout << "Enter score of test 2: ";
cin >> score2; endl;
cout << "Enter score of test 3: ";
cin >> score3; endl;
cout << "Enter score of test 4: ";
cin >> score4; endl;
cout << "Enter score of test 5: ";
cin >> score5;
#include <iostream>
usingnamespace std;
int main()
{
double score1, score2, score3, score4, score5;
double sum, average;
cout << "Enter score of test 1: ";
cin >> score1;
cout << "\nEnter score of test 2: ";
cin >> score2;
cout << "\nEnter score of test 3: ";
cin >> score3;
cout << "\nEnter score of test 4: ";
cin >> score4;
cout << "\nEnter score of test 5: ";
cin >> score5;
sum = score1 + score2 + score3 + score4 + score5;
average = sum/5;
cout << "your average is:" << average <<endl;
cout<< average;
system("pause");
return 0;
}
cout<< function used to print information in console cin>> function used to get value from user input system("pause"); pause screen to let user read displayed information
Remember, everything between double quotes "like this" is a text string. When you want to refer to one of your variables, whether to print it out, or to perform some calculation, do not put it in quotes. If you do, the compiler (which doesn't know what you really wanted to do) will quite happily treat it as a character string, and not as a numeric value.
Well, this one "warning C4101: 'sum' : unreferenced local variable" seems to indicate that "sum" was defined, but never used. It's just a warning and wouldn't stop the program from running, but suggests there is a logic error.
The other errors may be related to your compiler, or the way the project was set up, I don't know why "WinMain" would even be mentioned in a console application. Perhaps someone else can help with that.