Reading numbers from strings

Just need help getting started on my assignment. Never tried this procedure before but is it possible I can store numbers in strings and read numbers from strings. My assignments wants me to add numbers that are 20 digits long and they're asking me use strings. I don't know how that works so can anyone provide me an example so I can catch an idea :)

for instance string one[20]="12342312...."
string two[20]="23232323....";
by the way if this was character array, this would store in 20 characters with the 20th being null character. How does string work?
Last edited on
I guess they want you to implement an algorithm to add 2 numbers the way you would do it on paper:
Let's look at an example;

1
2
string one = "123"; //store string likes this (with [20] you would create an array of strings)
string two = "259";


What you wanna do now is get the Last element of both strings ( 3 and 9)
put each of them in another int-variable
Add those "ints" and store the result in another variable
create a result string:
 
string result = ""; //for our result 

The result of the ints would be (3 +9 = 12)
store 2 in your result-string
take the next 2 numbers (2 and 5). Add them and an extra one (because the previous result was 12)
And so on..

I hope you get what I'm saying (just add them the way you would on paper)
Glandy, I do get what your trying to say :) Let me work it out and see if I can find the solution :) Thank you!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Variable to store ina a string that is a number;
	string one="123";
	string two="232";


	//Output info on screen
	cout<<"The two numbers are " <<one <<" and "<<two;

	//add the numbers
	int sum=0;
	sum=one[2]+two[2];
	cout<<"\n\nSum: ";
	cout<<sum;
	


Ok I made this code here that will so far find the sum of the last digit of those two numbers. on my console after running this program I'm getting 101 as my sum. It should be 5. I know I'm working with strings over here so that might be an issue. For just general knowledge what is my code doing here that's causing that. Furthermore how can I fix this?
Chars can be converted to ints. Basically, if you look at an ASCII chart, the index of each character is their value. So you're accessing the characters '3' and '2' and adding them together.
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
	string one="123";
	string two="232";


	//Output info on screen
	cout<<"The two numbers are " <<one <<" and "<<two;

	//add the numbers
	int sum=0;
	sum=one[2]+two[2]; //so this is where things get crazy
/*
since sum is a variable of type int and one[2] and two[2] are both of type char
he doesn't iterpret them as numbers but characters
So before he can add anything he has to convert the characters into numbers
Okay how is he doing this:
he looks up in the ascii table the decimal representation of the characters
Our characters are '3' and '2' that means he adds 51 + 50 => 101
*/

//So how do you get from the character '3' to the number 3
// Since the numbers 0-9 are consecutively ordered in the ascii table 
// you can subtract the value for '0' from '3'
int x = '3' - '0'; // 51 - 48 => 3
int y = '2' - '0'; // 50 - 48 => 2

//so both numbers together would look like this:
sum = (one[2] - '0') + (two[2] - '0');
// or
sum = one[2] + two[2] - 2 * '0';

	cout<<"\n\nSum: ";
	cout<<sum;


Have fun !
Last edited on
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
void display (string &number1,string &number2)
{
	//Tell user to type in twenty digit
	cout<<"Twenty digit number: ";
	//get all the numbers until space is found
	getline(cin,number1);
			//If the length is greater then 20. ouput a message
				if (number1.length() >20)
					{
						number1.clear();
						cout<<"Enter Valid Number: ";
						cin>>number1;
					}
	cin.ignore(1000,'\n');
	
	//Tell user to enter in second twenty digit number
	cout<<"\n\nTwenty digit number: ";
	//get all the numbers until space is found
	getline(cin,number2);
			//If the length is greater then 20. ouput a message
				if (number2.length() >20)
					{
						number2.clear();
						cout<<"Enter Valid Number: ";
						cin>>number2;
					}
	cin.ignore(1000,'\n');
	
}


Just started on my code where I'm asking the user to enter ina 20 digit number. Do you think I can modify this more perhaps improve anywhere :) Thanks Glandy :)
Might wanna check for less 20 digits too or else have a way to align smaller numbers.
haha, I was wondering that too and would probably need help on that one :( however right now I'm trying to solve if the number are only 20 digits long then look forward to deal wit hthat issue :)
I would also not use getline to read into your number variables. Just use cin>>number1; since that only reads in a single number (delimited by spaces) at a time.

For the summing of the digits themselves, you need to have a way of dealing with overflow.
"red pandas."

As you said so, I did fix that getline and used the cin operator instead. Meanwhile, I just don't know where to proceed now and would like to get some more ideas on how to store each sum in the result array or like you said dealing with overflows. I have made this function and gotten to a part when it has to deal with the sum of numbers more then 9.
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
oid addnumber_formula(string &one,string &two, int result[],int resultsize)
{
	//Variable will get a sum of each index digit of two numbers
	int sum=0;
	//Row variable declared to store the sums in the result array
	int row=0;
	//variable used if the sum variable consists of two deigits. Left over will have a digit stored
	int leftover=0;
	//make a for loop to get each number from a string. Start from the last index
	for (int index=9;index>=0;index--)
	{
		//Gets the sum of the particular index from string
		sum=(one.length()-'0')+(two.length()-'0');

		//if the length of integers from sum is greater then one do this. Made a function here that finds the length of the total digits invariable sumb
		if (lengthnumber(sum)>1)
		{
			//made a function here that returns the last number of sum
			result[row]=return_lastnumber(sum);

			//store the first number of sum
			leftover=return_firstdigit(sum);
		
		}
		else
		{
			result[row]=(one.length()-'0')+(two.length()-'0')+leftover;
		}

	
	
	}


}
Topic archived. No new replies allowed.