Help making simple C++ adding test score program

Started by
47 comments, last by HTML 20 years, 6 months ago
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]
Advertisement
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,....*
Old Username: Talroth
If your signature on a web forum takes up more space than your average post, then you are doing things wrong.
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
Matt
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;



RanBlade
"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]

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

Cool, Thanks for the quick replies guys. That worked perfectly.
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..
What do you mean? It''s C++...
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.
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]
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.


RanBlade
"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]

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

This topic is closed to new replies.

Advertisement