-
C++ Help Me!!!!!!!!
I need to take a 3 digit user input and add each individual number in the digit together......its a crack head program but its for school....now im not asking anyone to do my homework for me however i dont know where to start....im confused and aggitated!!!
-
Code:
int main(){
cout<<"Enter three numbers: ";
int num1, num2, num3;
cin>>num1>>num2>>num3;
cout<<num1+num2+num3<<endl;
return 0;
}
or you could enter the three numbers as a string and then add each element by using atoi
[code]
int main(){
cout<<Enter three numbers: ";
char *s;
cin.getline(s);cin.ignore();
int num1, num2, num3;
num1=atoi(s[0]);num2=atoi(s[1]);num3=atoi(s[2]);
cout<<num1+num2+num3<<endl;
return 0;
}
-
sorry, it was supposed to look like this
Code:
int main(){
cout<<Enter three numbers: ";
char *s;
cin.getline(s);cin.ignore();
int num1, num2, num3;
num1=atoi(s[0]);num2=atoi(s[1]);num3=atoi(s[2]);
cout<<num1+num2+num3<<endl;
return 0;
}
-
Hey,
whoever repiled to this the first and second time, your replies are the same, and the program doesnt run.(it has warnings that crash it)
Thanks anyway
-
Put this at the top of that program and it should work fine,
Code:
#include <iostream>
using namespace std;
-
this code is slightly wrong......
Code:
int main(){
cout<<Enter three numbers: ";
char *s;
cin.getline(s);cin.ignore();
int num1, num2, num3;
num1=atoi(s[0]);num2=atoi(s[1]);num3=atoi(s[2]);
cout<<num1+num2+num3<<endl;
return 0;
}
The arguements to getline are wrong and you attempt to input into memory you dont 'own'.
here is the same piece of code with fixes....
Code:
#include<iostream>
using namespace std;
int main(){
cout<<Enter three numbers: ";
char s[81]
cin.getline(s,sizeof(s));cin.ignore();
int num1, num2, num3;
num1=atoi(s[0]);num2=atoi(s[1]);num3=atoi(s[2]);
cout<<num1+num2+num3<<endl;
return 0;
}
-
your all wrong
You guys are doing it all wrong. He said 3 DIGIT number, and then add each individual digit. so if you entered 125, you would add 1+2+5 wich equals 8.
However, I do not have time to explain the code. So hopefully you guys can work off of this. If I had a few more minutes, I'd write the code for you. Sorry!
-
how about getting the input a an array of integers and adding the integers?
-
If you can't get the input as seperate numbers or in an array then you can seperate the individual digits using the / and % operators (divide by 100 to get first digit and get remainder using %, divide remainder by 10 to get second digit..etc).
-
a three digit number is a meaningless concept to the compiler. The input could be stored as a string, an int, a double, a float, or any number of variable types. If the requirement was to enter an integer consisting of three digits, then the concepts as presented so far would be off base.
char sEntry[4];
int entry;
cout << "enter a 3 digit number" << endl;
cin >> sEntry;
cout << "enter another 3 digit number" << endl;
cin >> entry;
However, here's a completely different approach that doesn't require conversion from int to string or char to int or any of that stuff:
int num1, num2, num3, i, temp;
int total = 0;
temp = entry;
num3 = temp % 10;
temp = temp/10;//this is integer math, not routine math//
num2 = temp % 10;
num1 = temp/10;
total = num1 + num2 + num3;
cout << "the sum of the digits of the number " << entry << " is " << total << "." << endl;
Oops, looks like Zen beat me to the punch as I was waylaid in my posting my response. Oh well.