[issue] couting dec from ascii table, not number

hi, I have little problem with this code, I wanna make something like calculator where you will write scheme and it will calculate it... but when I wanna cout number so I can see if it works it will print decimal value from ascii table

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void main()
{
	stringstream ss;
	int DlzkaVzorca;
	int Cislo[100];
	int Cislo2[100];
	int Vysledok = 0;
	string vzorec;
	cout << "vzorec: ";
	getline(cin, vzorec);
	DlzkaVzorca = vzorec.size();
	char *vzorec2 = new char[DlzkaVzorca];
	ss << vzorec;
	ss >> vzorec2;
	for(int i = 0; i <= DlzkaVzorca; i++)
	{
		if(vzorec2[i] == '+')
		{
			for(int o = 0; o < i; o++)
			{
				Cislo[o] = vzorec2[o];
				cout << Cislo[o];
			}
		}
	}
	system("pause");
}


output > http://filebeam.com/0e6d2e52c4f1e5a713a3204042b48cdc.jpg
ASCII table > http://www.asciitable.com/

as you can see, it won't print 3 but 51, any suggestions?
vzorec2 contains character '3'. You cast it to an integer and thus get its numeric value 51. You can simply subtract '0' from it to get integer 3.
1
2
3
	char *vzorec2 = new char[DlzkaVzorca];
	ss << vzorec;
	ss >> vzorec2;
¿what is the purpose of that yo-yo?
Last edited on
@hamsterman thanks, it works!

@ne555 yo-yo? lol, I'm making array of chars from string so I can easily manipulate with them

so I will have

vzorec2[0] = '3';
vzorec2[1] = '+';
vzorec2[2] = '5';

so I can easily find + etc...

because I don't know how to manipulate with chars inside strings alone (I don't know if it is possible), and I'm using string first because strings are easier to work with than chars (for me...)
See http://www.cplusplus.com/reference/string/string/
Strings support [] operator, have some other useful functions.
Topic archived. No new replies allowed.