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

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;

Edited 7 Years Ago by firstPerson: n/a

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

Edited 7 Years Ago by restrictment: n/a

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

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

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!

Edited 7 Years Ago by itzaaron: n/a

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.

This question has already been answered. Start a new discussion instead.