average

Posts 1–15 of 24 · Page 1 of 2
average
as a "Mastery Check" for chapter 1 of a tutorial
i needed to make a program that can calculate the average of 5 values.
after doing this (easypeasylemonsqueezy) i tried to make a program that can calculate the average of more values (you can enter te amount of values).
the result was this.
the only problem is it doesn't work
can someone explain how to make it work?

#include <iostream>
using namespace std;

int main ()
{
int amount;
double result, value, tellen;
result = 0;

cout << "put in the amount of values you want the average of: ";
cin >> amount;
cout << endl;

for(tellen = 1; tellen <= amount; tellen++)
{
cout <<"Put in the " << amount << " number.";
cin >> value;
cout <<endl;
result + value = result; //what is wrong with this line?

}

cout << "The average is " << result/amount <<"." <<endl;

system ("pause");

return 0;
}


what do u mean by it doesnt work? cause i get a syntax error when i try to compile...is that what the problem was?
nvm, i see where u put a comment.
Quote Originally Posted by Corndog View Post
what do u mean by it doesnt work? cause i get a syntax error when i try to compile...is that what the problem was?
yeah it is a problem when it doesn't work
my compiler thinks this is the problem.
could u plz find the mistake in my code

result + value = result;
okay. heres ur problem. you have it saying:
result + vaule = result

But C++ is very picky. you need it to be like this:
result = result + value

and it works good, besides the way it says "put in the 2 number" if i want it to average out 2 numbers...it just looks weird.
Quote Originally Posted by Corndog View Post
okay. heres ur problem. you have it saying:
result + vaule = result

But C++ is very picky. you need it to be like this:
result = result + value

and it works good, besides the way it says "put in the 2 number" if i want it to average out 2 numbers...it just looks weird.
i'll go change it to "put in number 2"

why does it need to be in that order?
i am not sure why...but it just does.

and you dont have to change the wording...it just seemed weird to me.
i replaced it by this and now it is working perfectly

cout <<"Put in number " << tellen << " : ";
okay. i just changed the cout line with this. i think it looks better.

cout << "You Want The Average of " << amount << " Numbers. Please Enter One of These Numbers: ";
cout << "Please enter number " << tellen << "/" << amount << ":"
yep that works too...glad i could help you with ur problem.

okay. good luck with chapter 2, 3, and so on.
Glad you got that worked out.

Just FYI:
The assignment operator ("=") isn't the same as the equals sign in mathematics. This is very important.

It only works one way. whatever value that is on the right once calculated will be added to the left. This works the same way for many things. Imagine if you used the input operator for the cout statement the way you used the assignment operator:

"Hi ">> "There" >> cout;

This would absolutely not work. so just get used to this aspect of programming. In almost all English versions of programming languages. the math (adding, sub, mult.) is done on the right and then assigned to the left. This is universal.
thx Why06. i didnt really know how to explain it...just knew it wouldnt work, lol.
Quote Originally Posted by lalakijilp View Post
as a "Mastery Check" for chapter 1 of a tutorial
i needed to make a program that can calculate the average of 5 values.
after doing this (easypeasylemonsqueezy) i tried to make a program that can calculate the average of more values (you can enter te amount of values).
the result was this.
the only problem is it doesn't work
can someone explain how to make it work?
PLEASE READ THE COMMENTS, IDK IF IM RIGHT OR WRONG

Code:
#include <iostream>
using namespace std;

int main ()
{
int amount;
double result, value, tellen;
result = 0;

cout << "put in the amount of values you want the average of: ";
cin >> amount;
cout << endl;

for(tellen = 1; tellen <= amount; tellen++)
{
cout <<"Put in the " << amount << " number."; // rnt u supposed to give space after << 
cin >> value;
cout <<endl; // u didnt give space between << and endl
result + value = result; 

}

cout << "The average is " << result/amount <<"." <<endl; // shouldnt it be << "." << endl;
system ("pause");

return 0;
}
Is that y u are getting syntax error?
result + value = result didn't work because in C++ using '=' indicates that you are passing the value of the expression on the right side, to the variable or whatever on the leftside
Posts 1–15 of 24 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us