Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!


Initializing a variable


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 snǝɥʇǝɯoɹd   Members   -  Reputation: 192

Like
0Likes
Like

Posted 12 February 2013 - 05:38 PM

I'm trying to make a program that takes three scores from the user then averages them, I think I almost have it down except that Im getting an error about not initializing one of my variables but I don't know where the problem lies.

 //Game Score Average
//Program that gets three game scores from user and displays the average

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	int score1, score2, score3;
	int score4 = score1 + score2 + score3;

	//First score input
	cout << "Enter a number:";
	cin >> score1;

	//Second score input
	cout << "Enter a second number:";
	cin >> score2;
	
	//Third Score input
	cout << "Enter a third number:";
	cin >> score3;

	//Three scores averaged
	cout <<  score4;
	cout << "\n";

	system ("pause");
	return 0;
}

 

 



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 10846

Like
3Likes
Like

Posted 12 February 2013 - 05:48 PM

When you add score1, score2 and score3 together you haven't actually given them values yet. You need to add them after you get the input.

#3 snǝɥʇǝɯoɹd   Members   -  Reputation: 192

Like
0Likes
Like

Posted 12 February 2013 - 05:52 PM

Thanks, I wasn't thinking about it linear so I didn't see the problem in the begining.



#4 fastcall22   Crossbones+   -  Reputation: 6250

Like
1Likes
Like

Posted 12 February 2013 - 05:53 PM

You need to add them after you get the input.

The code, int score4 = score1 + score2 + score3;, isn't read as "at any time, score4 is the sum of score1 and score2 and score3"; it is read as "store the sum of score1 and score2 and score3 into score4". Since score1 .. score3 haven't been assigned a value at the time, a compiler warning is issued. (On a side note, you'll need to divide by three to get the average.)
gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=

#5 snǝɥʇǝɯoɹd   Members   -  Reputation: 192

Like
0Likes
Like

Posted 12 February 2013 - 05:59 PM

Thanks for the info, now I have to average but without a decimal vaule, I thought using double instead of int here

double score4 = (score1 + score2 + score3)/ 3;

 

would make it send out an average with decimals but it hasn't.



#6 jellyfishchris   Members   -  Reputation: 300

Like
2Likes
Like

Posted 12 February 2013 - 07:21 PM

Your doing an int / int division this returns a int results and then your passing it to a double so...

 

5 / 2 = 2.5

2.5 - Convert to an int 2.

Convert 2 to a double 2.0

 

So an easy fix would be to do....

 

 

double score4 = (score1 + score2 + score3)/ 3.0;
 





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS