This program collects 3 numbers from the user and then simple adds the total and spits out a response. problem is the final output is blank, it just says 'The total is: '_____ but nothing there. Whats wrong??

``````#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int num[3];
for(int x=0; x<3; x++)
{
cout << "Enter a number: ";
cin >> num[x];
}
int sum=0;
int x=0;
for(int x=0; x<3; x++);
{
x=x+sum;
cout << "The total is: ";
cin >> x;
}

return 0;
}``````

## All 14 Replies

``cout << "The total is: " << x;``

thanks, i feel kinda dumb putting x as cin i changed that to

``cout << "The total is: " << x << endl;``

but the total just comes out as zero.

``````int sum=0;
int x=0;
for(int x=0; x<3; x++);
{
x=x+sum;
cout << "The total is: ";
cin >> x;
}

return 0;
}``````

Here you're not using your array to add the variables. The first pass it is always going to be zero because x = 0 and sum = 0 so 0 + 0 = 0. You should really use separate variables, one for your for statements and another for your array variables. It would clean up your code a bit and make it easier to understand.

This is problematice :

``````int sum = 0;
int x=0; //  you declared it inside the for loop below
for(int x=0; x<3; x++);
{
x=x+sum; //what happened to num array?
cout << "The total is: ";
cin >> x;
}``````

change to :

``````int sum = 0;
for(int x=0; x<3; x++);
sum = sum + num[x];

cout << "The total is: " << x << endl;``````

Okay I see the logic, but there is a building error: 'x' is undeclared identifier. Heres my updated code.

``````#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int num[3];
for(int x=0; x<3; x++)
{
cout << "Enter a number: ";
cin >> num[x];
}
int sum = 0;
for(int x=0; x<3; x++);
sum = sum + num[x];

cout << "The total is: " << x << endl;

return 0;
}``````

declare x outside of the for loop. Usually works for some reason..

``````#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int num[3], x = 0;
for(x; x<3; x++)
{
cout << "Enter a number: ";
cin >> num[x];
}
int sum = 0;
for(int x=0; x<3; x++);
sum = sum + num[x];

cout << "The total is: " << x << endl;

return 0;
}``````

It compiles without errors but the total is still coming out '0'

print out the sum, cout<<sum << endl;

I already tried that it just outputs the first number that the user entered as the total.

Have I stumped everyone?

``for ( int x=0; x<3; x++ );``

Do you know what the semicolon does to your intended loop?

``for ( int x=0; x<3; x++ );``

Do you know what the semicolon does to your intended loop?

No I don't. But it is working now thank you!

``for ( int x=0; x<3; x++ );``

Do you know what the semicolon does to your intended loop?

No I don't. But it is working now thank you!

This would be an equivalent:

``````for ( int x=0; x<3; x++ )
{
/* empty loop */
}``````

Okay, well I would like to thank everyone who helped. I am adding to everyone's rep and marking this one as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.