something like string to int? didn't solve yet

Hello!

Im trying to make a program that you tipe in any numbers as a string and the
program sums them, like this:
Enter a number:4865

and then it would display 23, becouse 4+8+6+5=23

I managed to do a 3 number example using getch from conio.h library but its a verry stupid way of doing this, but i just didn't know how to do better.
heres the 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <cstdlib>
#include <iostream>
#include <conio.h>
#define input getch()
#define space <<" "<<
using namespace std;

int main(int argc, char *argv[])
{
int a,b,c;

cout<<"Input 3 numbers: ";
a = input;
b = input;
c = input;

int d;
int e;
int f;


cout<<endl<<a space b space c<<endl; //test//
//========= a ============//
if(a == 49)
{
     d = 1;
}
if(a == 50)
{
     d = 2;
}
if(a == 51)
{
     d = 3;
}
if(a == 52)
{
     d = 4;
}
if(a == 53)
{
     d = 5;
}
if(a == 54)
{
     d = 6;
}
if(a == 55)
{
     d = 7;
}
if(a == 56)
{
     d = 8;
}
if(a == 57)
{
     d = 9;
}
//=============== b ========================//
if(b == 49)
{
     e = 1;
}
if(b == 50)
{
     e = 2;
}
if(b == 51)
{
     e = 3;
}
if(b == 52)
{
     e = 4;
}
if(b == 53)
{
     e = 5;
}
if(b == 54)
{
     e = 6;
}
if(b == 55)
{
     e = 7;
}
if(b == 56)
{
     e = 8;
}
if(b == 57)
{
     e = 9;
}
//================= c ===========//
if(c == 49)
{
     f = 1;
}
if(c == 50)
{
     f = 2;
}
if(c == 51)
{
     f = 3;
}
if(c == 52)
{
     f = 4;
}
if(c == 53)
{
     f = 5;
}
if(c == 54)
{
     f = 6;
}
if(c == 55)
{
     f = 7;
}
if(c == 56)
{
     f = 8;
}
if(c == 57)
{
     f = 9;
} 
       
int sum = d + e + f;
cout<<"\nSum is: "<<sum<<"!\n";
    
    
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Please tell me a better way of doing this!
Last edited on
You'll have to use a stringstream for that case -
1
2
3
4
5
6
7
8
9
10
11
#include <sstream>

std::string myString = 50235;
int myNumber = 0;

std::stringstream myString_ss (myString);
myString_ss >> myNumber;
//safely puts the number into an int
//it's recommended you check if the string IS an int first..
//or it might cause errors
                                          
Last edited on
Don't you need to input a string rather than individual numbers?

You can read a line as follows:
1
2
std::string line;
std::getline(std::cin, line);


The you access each letter in the string using:
1
2
3
4
for (size_t i = 0; i != line.size(); ++i)
{
    char c = line[i];
}


Then you convert each of those characters to a number and add them up.
Last edited on
This is not what i had in mind, though i think this is a good way to do it.
How lets say i split an int containing a number 123 into 3 ints, the firts contains the first digit, the second the secont digit etc...
so it would be like this:
int i = 123;
int a,b,c,;

cout<<a (space)<<b(space)<<c(space);

and the program result would be:
1 2 3.

and then i could sum the three ints like this:

int sum = a+b+c;
cout<<sum;

and it would display:
6


EDIT: sorry, i really have to refresh the page before posting
EDIT 2: i dont understand either method and neither works
Last edited on
Ahh.. I didn't understand what you were asking.
Well you can make an array the length of the integer, pick off all the individual integers and place in appropriate array slot (hint - use % and /) then another int, sum would be the total of the array.
And how i should do that?
1
2
3
4
5
6
string s = "34573456";
int sum = 0;
for(unsigned int x = 0; x < s.size(); x++)
  sum += s[x]-48;

//sum will equal 37 


The characters 0-9 have ascii values of 48-57. s[x] is of type char, which automatically converts to type int when used in a context expecting an int. So, subtract 48 from the value, and you get the integer value of that numerical character. A bounding clause could also be used to ignore ascii characters outside of 0-9 ie:

1
2
if(s[x]>=48 && s[x]<=57)
  sum += s[x]-48;


jRaskell (96) Oct 9, 2009 at 9:29pm
1
2
3
4
5
6
7
 string s = "34573456";
int sum = 0;
for(unsigned int x = 0; x < s.size(); x++)
  sum += s[x]-48;

//sum will equal 37 
 


The characters 0-9 have ascii values of 48-57. s[x] is of type char, which automatically converts to type int when used in a context expecting an int. So, subtract 48 from the value, and you get the integer value of that numerical character. A bounding clause could also be used to ignore ascii characters outside of 0-9 ie:
1
2
 if(s[x]>=48 && s[x]<=57)
  sum += s[x]-48;


Good idea
And how i should do that?


modulus the % symbol is used to get the remainder of integer division, so you can use % and / to break up a number into individual numbers eg.

10 / 10 = 1 this gives you the first number.

like say we have 43 / 10 the answer is 4.
43 % 10, gives you the last number '3'.

EDIT: typo
Last edited on
Topic archived. No new replies allowed.