String issue

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster

String issue

 
0
  #1
Oct 24th, 2009
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??

  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7. int num[3];
  8. for(int x=0; x<3; x++)
  9. {
  10. cout << "Enter a number: ";
  11. cin >> num[x];
  12. }
  13. int sum=0;
  14. int x=0;
  15. for(int x=0; x<3; x++);
  16. {
  17. x=x+sum;
  18. cout << "The total is: ";
  19. cin >> x;
  20. }
  21.  
  22. return 0;
  23. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 480
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 56
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
1
  #2
Oct 24th, 2009
  1. cout << "The total is: " << x;
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster
 
0
  #3
Oct 24th, 2009
thanks, i feel kinda dumb putting x as cin i changed that to
  1. cout << "The total is: " << x << endl;

but the total just comes out as zero.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 19
Reputation: Kontained is an unknown quantity at this point 
Solved Threads: 5
Kontained Kontained is offline Offline
Newbie Poster
 
1
  #4
Oct 24th, 2009
Originally Posted by itzaaron View Post
  1. int sum=0;
  2. int x=0;
  3. for(int x=0; x<3; x++);
  4. {
  5. x=x+sum;
  6. cout << "The total is: ";
  7. cin >> x;
  8. }
  9.  
  10. return 0;
  11. }
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,425
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 183
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
0
  #5
Oct 24th, 2009
This is problematice :

  1. int sum = 0;
  2. int x=0; // you declared it inside the for loop below
  3. for(int x=0; x<3; x++);
  4. {
  5. x=x+sum; //what happened to num array?
  6. cout << "The total is: ";
  7. cin >> x;
  8. }
change to :
  1. int sum = 0;
  2. for(int x=0; x<3; x++);
  3. sum = sum + num[x];
  4.  
  5. cout << "The total is: " << x << endl;
Last edited by firstPerson; Oct 24th, 2009 at 10:45 pm.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster
 
0
  #6
Oct 24th, 2009
Okay I see the logic, but there is a building error: 'x' is undeclared identifier. Heres my updated code.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7. int num[3];
  8. for(int x=0; x<3; x++)
  9. {
  10. cout << "Enter a number: ";
  11. cin >> num[x];
  12. }
  13. int sum = 0;
  14. for(int x=0; x<3; x++);
  15. sum = sum + num[x];
  16.  
  17. cout << "The total is: " << x << endl;
  18.  
  19. return 0;
  20. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 133
Reputation: restrictment is on a distinguished road 
Solved Threads: 10
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
1
  #7
Oct 24th, 2009
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;
}
Last edited by restrictment; Oct 24th, 2009 at 11:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster
 
0
  #8
Oct 24th, 2009
It compiles without errors but the total is still coming out '0'
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,425
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 183
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
1
  #9
Oct 24th, 2009
print out the sum, cout<<sum << endl;
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: itzaaron is an unknown quantity at this point 
Solved Threads: 0
itzaaron itzaaron is offline Offline
Light Poster
 
0
  #10
Oct 24th, 2009
I already tried that it just outputs the first number that the user entered as the total.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC