Jump to content
  • Advertisement

Archived

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

HTML

Help making simple C++ adding test score program

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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,....*

Share this post


Link to post
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 this post


Link to post
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;




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]

Share this post


Link to post
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 this post


Link to post
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.



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]

Share this post


Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!