Jump to content

  • Log In with Google      Sign In   
  • Create Account

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   Members   

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;
}

 

 



#2   Moderators   

11528
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   Members   

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   Moderators   

9751
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.)
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#5   Members   

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   Members   

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.