#### Archived

This topic is now archived and is closed to further replies.

# Help making simple C++ adding test score program

This topic is 5169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am taking the tutorials from www.cplusplus.com and I am on the part with variables, datatypes, and contants. I am trying to use the information they give me to make my own little programs along the way. Here is one that is supposed to add up test scores if you enter 3 of them.
#include <iostream.h>

int main()
{

//declare variables

int score1;
int score2;
int score3;
int result;

//process

result = score1 + score2 + score3;

//display it

cout << "Enter your test scores >" << endl;
cin >> score1;
cin >> score2;
cin >> score3;
cout << "Your overall score is: ";
cout << result;

}

For some reason it doesn't want to add the scores right. I enter 3 scores and it comes out with some random number even though I declared my variables, set result to = score1 + score2 + score3, and I told it to output result. Am I missing something? Thanks [edited by - HTML on October 15, 2003 1:44:51 PM]

##### Share on other sites
You have to have your values of your scores entered BEFORE you can do calcs on them,... Otherwise it will just use ramdom numbers from some where.

*Yeah,... first post I think I answered right for a question,....*

##### Share on other sites
you need to set result after you got your 3 scores

cout << "Enter your test scores >" << endl;cin >> score1;      cin >> score2;      cin >> score3;    result = score1 + score2 + score3;cout << "Your overall score is: " << result << endl;

for your random numbers, it''s only because when you create a variable, it is assigned an adress in memory, so the value in the variable may be anything that is at this adress in memory so you can never rely on variable to be 0 at creation.

hope that helps
Matt

##### Share on other sites
You need to do your calculation after you have recived the value of the variables. so do

cout << "Enter 3 Test Scores\n";cin >> score1;cin >> score2;cin >> score3;result = score1 + score2 + score3;cout << "Your result is " << result;

"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Mage Tower Ent-My Site]

##### Share on other sites
Cool, Thanks for the quick replies guys. That worked perfectly.

##### Share on other sites
LOL. I''m just wondering, what language supports the thing that he did?

--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..

##### Share on other sites
What do you mean? It''s C++...

##### Share on other sites
XD He means what language supports the calculation of a result before the variables are actually assigned.

Hey Pipo, I know one: F++ for Fictitious.

##### Share on other sites
I have another question:
Now I am trying to make it display the percent out of 300 points( assuming all the test were out of 100pts)

#include <iostream.h>int main(){//declare variables  int score1;  int score2;  int score3;  int result;  int percent;  //display it    cout << "Enter your three test scores >" << endl;      cin >> score1;      cin >> score2;      cin >> score3;    result = score1+score2+score3;    cout << "Your overall score is: ";    cout << result;    cout << " out of 300 points." << endl;// tell what percent you got out of 300pts        cout << result;    cout << " out of 300 points is a "    cout << percent;    percent = result \ 300;    //the result divided by all pts makes a percent}

I made percent a declared variable, then I did percent = result \ 300( I put it after this time....or did I??)

Thanks

[edited by - HTML on October 15, 2003 3:36:26 PM]

##### Share on other sites
you need to do the calculation after you have been given the variables but you need to do the calculation before you display the result.

"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Mage Tower Ent-My Site]

##### Share on other sites
AND to do divison you need / operator not \ "\" is the escape operator... "/" is the divison operator.

"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Mage Tower Ent-My Site]

##### Share on other sites
I really really really hope this is a joke...

First, you did the calculation before getting the values...

Second, you did the calculation after you displayed the result...

##### Share on other sites
#include <iostream.h>int main(){//declare variables  int score1;  int score2;  int score3;  int result;  int percent;  //display it    cout << "Enter your three test scores >" << endl;      cin >> score1;      cin >> score2;      cin >> score3;    result = score1+score2+score3;    cout << "Your overall score is: ";   cout << result;    cout << " out of 300 points." << endl;// tell what percent you got out of 300pts        cout << result;    cout << " out of 300 points is a "  percent = result / 300;  cout << percent;        //the result divided by all pts makes a percentreturn 0;}

that is how your program should look. could be better formatting but i cut pasted ure code.

"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Mage Tower Ent-My Site]

[edited by - ranblade on October 15, 2003 3:46:54 PM]

##### Share on other sites
Sorry for the pseudo-flame of my previous post. You must be very new to programming in general. Have you had any experience with any other programming languages? C++ would not be my first choice for someone first learning to program (Visual Basic might be nicer)?

Here''s some advice: Try to think of the computer as someone who is very very dumb. The computer only does what you tell it to do AND ONLY in the order that you tell it to.

When you say:

- display this number
- calculate this number

The computer will first display the number, THEN it will calculate the number. The computer does not know that you really want it to display the number after you calculate it. You have to tell it how to do things in the proper order.

Regards,
Jeff

##### Share on other sites
quote:
Original post by rypyr
Sorry for the pseudo-flame of my previous post. You must be very new to programming in general. Have you had any experience with any other programming languages? C++ would not be my first choice for someone first learning to program (Visual Basic might be nicer)?

Here's some advice: Try to think of the computer as someone who is very very dumb. The computer only does what you tell it to do AND ONLY in the order that you tell it to.

When you say:

- display this number
- calculate this number

The computer will first display the number, THEN it will calculate the number. The computer does not know that you really want it to display the number after you calculate it. You have to tell it how to do things in the proper order.

Regards,
Jeff

Thanks for the good interpretation. And for learning another language...I really don't want to do that because I might just get confused and use them both together or something stupid. Besides, I have only been doing these tutorials for a few days(and only a few hours in those days)...so I think I am making ok progress...

And about displaying the number then calcuating the number after, that makes more sense..I probably should have thought about it a little more before posting...

Thanks

[edited by - HTML on October 15, 2003 3:55:01 PM]

##### Share on other sites
#include <iostream.h>int main(){//declare variables  int score1;  int score2;  int score3;  int result;  int percent;  //display it    cout << "Enter your three test scores >" << endl;      cin >> score1;      cin >> score2;      cin >> score3;        result = score1+score2+score3;    cout << "Your overall score is: ";    cout << result;    cout << " out of 300 points." << endl;    cout << result;    cout << " out of 300 points is a ";        percent = result / 300;    cout << percent << "%";       return 0;}

I made the changes, but it seems to always come out at 0%. Even though I am telling it that percent = result / 300.

[edited by - HTML on October 15, 2003 4:06:13 PM]

##### Share on other sites
You might also want to change the type of the percent variable to float and change the 300 to 300.0f. Otherwise it will be an integer division which will not give you the desired results. For example 200/300 will turn out as 0. Then perhaps you want to multiply the result by 100.0f to get it as a percentage instead of a number from 0 to 1.

Edit: I was just in time, it seems.

[edited by - Lantz on October 15, 2003 4:02:41 PM]

##### Share on other sites
Argh is not in the standard anymore, that code won''t even compile on VS .NET 2003 or GCC 3.4

##### Share on other sites
quote:
Original post by noVum
Argh is not in the standard anymore, that code won't even compile on VS .NET 2003 or GCC 3.4

I am using KDevelope....it should at least compile on GCC....

I am adding some more code and fixing it, i'll post it again when I am done. I am also trying to get it to say : ___% is a ____+, - where ___ = A B C D or F

@Lantz , thnks for the tip, that works great

Thanks for helping guys

[edited by - HTML on October 15, 2003 4:15:16 PM]

##### Share on other sites
result / 300 will give you something between 0 and 1
so if you want percentage, you must multiply it by 100
so percent = (result / 300) * 100;

##### Share on other sites
Made it standard code now. Now I want to try to make say ___% is a A B C D or F. How would I go about doing that? I know I have to make variables A B C D F and then set them to a number, but do I really have to do A = 90, 91, 92 ect.... The next thing I am not sure about is making a or an appear depending on if there is a vowel or not. Don''t tell me excatly what to do yet, I want to try to figure it out first, then if I can''t then you can. Thanks

#include <iostream>using namespace std;int main(){//declare variables  int score1;  int score2;  int score3;  int result;  float percent;  //display it    cout << "Enter your three test scores >" << endl;      cin >> score1;      cin >> score2;      cin >> score3;        result = score1+score2+score3;    cout << "Your overall score is: ";    cout << result;    cout << " out of 300 points." << endl;    cout << result;    cout << " out of 300 points is a(n) ";        percent = result * 100.0f / 300.0f;    cout << percent << "%" << endl;    //tell what percent = what grade letter NEW//    cout << percent << "%";    cout << " is a(n) ";    return 0;}

##### Share on other sites
you could do a swtich statement

switch(percent)case 100:{cout << "A+\n";break;}case 95:{cout << "A\n";break;}//and so on

imo that is easiest and least amount of typing

"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Mage Tower Ent-My Site]

[edited by - ranblade on October 15, 2003 4:30:30 PM]

##### Share on other sites
Can I also do an "if then" statement? if it is an A then cout << so on....

EDIT: I could also possibly do an "if then " statement with the 'a' and the 'an' too, right? if ...t is a then cout << an .

[edited by - HTML on October 15, 2003 4:36:45 PM]

##### Share on other sites
Use an if-statement. Switch won't work unless you feel like typing in every percentage.

if( percentage >= 98.0f )    cout << "A+\n";else if( percentage >= 95.0f )    cout << "A\n";else if( percentage >= ... )    cout << "...\n";

And so on with whatever values those grades represent. Remember that they need to be in descending order.

Edit: And of course there are more elegant solutions but I think you should stick with a series of if's for now.

[edited by - Lantz on October 15, 2003 4:43:51 PM]

##### Share on other sites
Thanks That worked out great.

I have 2 more questions though:

1) What does the f do? ex: 90.0f I know
what it does...(makes it so it is not ''0.90'', but
what does it stand for and all that?

2) How should I go about doing the average for the 3 scores?
I know it would be something like adding score1,2,3 then
maybe / 3?

I want to kind of make that like another option, so
after it finds out all that stuff. Then it has you type
in your scores again for the average (unless there is
an easy way to make it remember the old scores then
find the average of the 3 scores)

• ### Forum Statistics

• Total Topics
628651
• Total Posts
2984044

• 10
• 9
• 9
• 10
• 21