New to C++ and confused

Pages: 12
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.

I dont even know where to start. please help
Well start off logically, you will need

5 cin statements(considering you havnt learned loops yet)
a variable to hold a value
a constant variable to hold the number of test scores

ask the user for 5 seperate scores, add each onto the variable, then divide by the const variable(5).
you can go to this site:

http://www.cplusplus.com/doc/tutorial

your assigment is very easy, it should be done very fast... hope it helps... :)
Here is a similar assignment.
http://www.cplusplus.com/forum/beginner/79703/
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
This is where im at right now and I think i messed up but Im working on the sum and then dividing by 5 for the average

#include <iostream>
using namespace std;

int main()
{


cout << "Enter score of test 1: ";
cout << "Enter score of test 2: ";
cout << "Enter score of test 3: ";
cout << "Enter score of test 4: ";
cout << "Enter score of test 5: ";
cin >> score 1;
cin >> score 2;
cin >> score 3;
cin >> score 4;
cin >> score 5;

cout << "a = " << score 1 << " b=" << score 2 << "c=" << score 3 << "d=" << score 4 << "e=" << score 5 << endl;
cout << "a + b + c + d + e" << endl;
cin << sum /5
return 0;
}
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: ";

cout << "a = " << score 1 << " b=" << score 2 << "c=" << score 3 << "d=" << score 4 << "e=" << score 5 << endl;
cout << "sum = " << "a + b + c + d + e" << endl;
cout << "average =" << "sum" << /5 endl;
return 0;
}
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.



Ok I think I fixed it but am just missing a couple things. I have a few expected a ';' errors and I have 1 identifier "sum" is undefined error


#include <iostream>
using namespace std;

int main()
{

double score 1 = a,
score 2 = b,
score 3 = c,
score 4 = d,
score 5 = e,
sum,
average;

cout << "Enter score of test 1: ";
cout << "Enter score of test 2: ";
cout << "Enter score of test 3: ";
cout << "Enter score of test 4: ";
cout << "Enter score of test 5: ";
cin >> score 1;
cin >> score 2;
cin >> score 3;
cin >> score 4;
cin >> score 5;

cout << "a = " << score 1 << " b=" << score 2 << "c=" << score 3 << "d=" << score 4 << "e=" << score 5 << endl;
cout << "a + b + c + d + e" << endl;
cin << sum /5
return 0;
}
Ok, here's a bit more of how I think it needs to be:
1
2
double score1, score2, score3, score4, score5; 
double sum, average;


1
2
3
4
5
6
7
8
9
10
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;
By the way, do you have a compiler to test this out?
yes i have a compiler but it shows no errors but then i run it and their are build errors

I think I finally have it fully correct. But every time I try to run in I have build errors.

#include <iostream>
using namespace 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;


cin >> "sum =" >> "score1 + score2 + score3 + score4 + score5";
cin >> "average =" "sum / 5";
cout << "your average is:" << "average";

return 0;
}
Last edited on
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
#include <iostream>
using namespace 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
Last edited on
i still get build errors no matter what i put in.
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 if i take everything out of double quotes i get the red error lines under them.
these are the errors i keep running into
1>------ Build started: Project: project 1, Configuration: Debug Win32 ------
1> project 1.cpp
1>c:\users\nathan\desktop\project 1\project 1.cpp(8): warning C4101: 'sum' : unreferenced local variable
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Users\Nathan\Desktop\project 1\Debug\project 1.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
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.
I used 'sum' though. im going to uninstall and reinstall it tomorrow
Pages: 12