Multiply integer within string array

Hey everyone. I have been trying to solve this problem but can't find the solutions online. This is just a massively simplified version of the program where the problem is pinpointed! All help would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main() 
{
	string test;
	getline(cin, test);
	cout << test[0] * 5;
	return (0);
}


And this is the output:

1
2
11
245


As you can see, it should be:

1
2
11
5


but for some reason it thinks test[0] is 49. Can anyone see the problem?
Last edited on
You cannot perform mathematical operations on a string, because it is stored as a series of characters. So you first have to convert test[ 0 ] to an integer, float etc., and then multiply it.
The computer thinks test[ 0 ] is 49, because each character is identified by a number ( 49 is the character '1' )
Have a look at this one for converting between string and int: http://www.cplusplus.com/forum/general/13135/

Good luck!
Instead of

cout << test[0] * 5;

you should write

cout << ( test[0] - '0' ) * 5;

provided that test[0] contains a character that corresponds to a digit.
Last edited on
@Fransje
You cannot perform mathematical operations on a string, because it is stored as a series of characters. So you first have to convert test[ 0 ] to an integer, float etc., and then multiply it.


You are wrong. He does not perform a mathematical operation with a string. He performs a mathematical operation with a character test[0]. Character is an integral type and can take part in mathematical operations without any explicit conversions to other arithmetic types.
Last edited on

insted of
1
2
 
cout << test[0] * 5;


try using
 
cout << atof(&test[0]) * 5;


atof -> converts string to double . http://www.cplusplus.com/reference/clibrary/cstdlib/atof/


by doing only " test[0]*5 " u multiply the ASCII code of a character with 5 for example if you have test[0]="1" you will get 49 * 5 so the aplication will display 245

http://www.asciitable.com/index/asciifull.gif -> ASCII code table
Last edited on
Hey

I have tried using cout << atof(&test[0]) * 5;

But when I use test[0] it multiplies the whole string

i.e
1
2
111
555
Just tried vlad from moscow's idea an it works! Thank you very much! Don't know why but it does. Do you know why?

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main() 
{
	string test;
	getline(cin, test);
	cout << (test[0] - '0') * 5;
	return (0);
}
I showed you how to perform the operation. What is the problem?!
No, I am sorry. I didn't mean to be rude or anything. I was just wondering why it works. It is fine if you don't want to expand :) You have been a great help
@mikeecb

it works because u subtract ASCII code for '0' (48) from the the ASCII code that is stored in test[0]
for example if test[0] is '5' (53) and u calculate (test[0] - '0') you practically calculate (53 - 48 )

char____ASCII code
0_________48
1_________49
2_________50
3_________51
4_________52
5_________53
6_________54
7_________55
8_________56
9_________57

so in order to get any int number between 0-9 from a string u must subtract the char '0' or integer number 48 from the character

cout << (test[0] - 48) * 5; will display the same result as cout << (test[0] - '0') * 5;
Last edited on
Topic archived. No new replies allowed.