C++ help.

1
2
3
4
5
int sum=82; 
for(int i; i < sum.length; i++) 
{ 
final=sum[i]+final; 
}


i need help getting the sum and adding the numbers together. for example 82 would be 8+2.
i keep getting an error on this piece of code and i cant figure it out please help.
Please post all of your code.

First off, sum is an integer. Integers don't have lengths. You are asking the computer to find the length of a number... Number's don't have lengths.

Second, when you start the:

 
final=sum[i]+final; 


The computer doesn't know what final is. You need to specify that final is another integer. You can't actually use "final" though for your integer as that is a word reserved for other things.

Try this:
1
2
3
4
5
6
int sum=82;
int total = 0;
for(int i; i < sum.length /*(<- this is still wrong)*/; i++) 
{ 
total=sum[i]+total;
}


Also, you are trying to do an array access on an integer. To the computer 82 is 82 not an 8 and a 2, just 82. You could make a function that takes in an integer and enters it into an array, then modify your current code to basically do what you where already doing, just with an array instead of with a single int.
Last edited on
Try to code this out, I will outline it for you:

1
2
3
4
5
6
7
8
9
10
//Declare your integers;

//Allow someone to input any integer;

//With a loop, input each of the integers into your array; (82 is moved to an array as [8,2])

//Take the sum of all the numbers in the array with another loop;

//Print out the new sum of the numbers;
Last edited on
sorry i didnt have my whole code up. just thought it was enough info. here is the rest of my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int main()
{
	
	string name;
	int sum = 0;
	int sum1=0;
	
cout << "Enter Name: " << endl;
getline(cin,name);

cout << "Hello: " << name << endl;


for (int i=0; i<name.length (); i++)
{
	 
	cout<< toupper(name[i]) - 64<< " ";
	
	
	/*if(name[i] > 0 && name[i] < 27)
	{
   	name[i]=name[i];
	}
	else
	{
    name[i]=0;
	}*/

	 sum=sum + toupper(name[i]) - 64;
	 
	 
  int q[sum];	  
  for (int k=0; k<q.size(); k++)
  	sum1=q[k]+sum;
	
}
 cout << "Your number is: " << sum1;

return 0;
}




I already post on http://www.cplusplus.com/forum/general/154342/ thread.
good luck.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main(){
int t=0;
int q;
int num;
cout<<"enter no";
cin>>num;

while(num>0){
q=num%10;
num=num/10;
t=t+q;
}

cout<<t;


return 0;
}
If you are looking for a simple answer to your question go with sujitnag above this.

I assumed from:

1
2
3
4
5
int sum=82; 
for(int i; i < sum.length; i++) 
{ 
final=sum[i]+final; 
}


you were doing a homework assignment where arrays may have been a requirement. If they are not a requirement, using modulus(%) 10 is much simpler, and more efficient. So go with sujitnag's code.
Topic archived. No new replies allowed.