0

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;
}
6
Contributors
14
Replies
15
Views
7 Years
Discussion Span
Last Post by itzaaron
Featured Replies
  • [code] cout << "The total is: " << x;[/code] Read More

  • [QUOTE=itzaaron;1025720] [CODE] int sum=0; int x=0; for(int x=0; x<3; x++); { x=x+sum; cout << "The total is: "; cin >> x; } return 0; }[/CODE][/QUOTE] 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 … Read More

  • declare x outside of the for loop. Usually works for some reason.. [CODE]#include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int num[3], [COLOR="Red"]x = 0[/COLOR]; for(x; x<3; x++) { cout << "Enter a number: "; cin >> num[x]; } int sum = 0; for(int x=0; x<3; x++); … Read More

  • print out the sum, cout<<sum << endl; Read More

  • [CODE]for ( int x=0; x<3; x++ )[COLOR="Red"];[/COLOR][/CODE] Do you know what the semicolon does to your intended loop? Read More

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.

1
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.

0

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 by firstPerson: n/a

0

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

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 by restrictment: n/a

0

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

0

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

0
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 by itzaaron: n/a

0
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 */
}
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.