two strings sum in another string
Sep 7, 2014 at 12:59pm UTC
My program works something like this:
First string: 123
Second string :321
123 + 321 = 444
----------------------------------------
I want to make it like this:
First string: 1 2 3
Second string: 3 2 1
1 2 3 + 3 2 1 = 4 4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
int main()
{
char a[10], b[10], c[20];
int sum, n1, n2;
cout << "First string of integers: " ;
cin.getline(a,10);
cout << "Second string of integers: " ;
cin.getline(b,10);
n1 = atoi(a);
n2 = atoi(b);
sum = atoi(a) + atoi(b);
cout << a << "+" << b << "=" << sum;
_getch();
}
Sep 7, 2014 at 2:18pm UTC
Why are you storing these numbers in C-strings? Why not just get the information from the user as integers?
Since this is C++ why the C-strings instead of std::string, and then stringstreams instead of atoi()?
Sep 7, 2014 at 2:48pm UTC
I can solve this problem very easy using arrays.
But I found this problem in a c++ book, and I want to practice strings.
Topic archived. No new replies allowed.