Array sum user input..

Q1 using array can i put user input and then it will sum by user...?it work's but the sum wrong..

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
char chs[10];
int cho;
int result=0;


cout<<"Using User Input For Sum's"<<endl;
cin>>cho;
cout<<endl;
for(int i=0;i<cho;i++)
{
cin>>chs[i];
result+=chs[i];
}
cin.ignore();
cout<<result;

cin.get();
}
You're using char for 'chs'. That means the input is '0' (= 48) rather than 0. Use int instead.

By the way: you don't need an array to solve that task above
Minor hijack: I know this can be done by using a single int instead of an array and overwriting the value, but is it also possible to do it without a storage variable (thus summing directly from the cin result, rather than store-then-sum)?

oh thank so very much..now i understand why it become 255 when i plus+plus=sum thank so much this very helpul ..this only for understanding the array .. im make a project using array restaurant .. when user input the list of food it will located the price that i make..so user only type how many food them want then it calculate it by itself..thank it very helpful
Topic archived. No new replies allowed.