473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

int to char problem ?

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Stack
{
public :
Stack();
void push(char&);
void pop();
char getpop();
void output();

private:
vector<char_vector;
int topOfStack;
};
Stack::Stack():topOfStack(-1){}
void Stack::push(char &x)
{
topOfStack++;
_vector.push_back(x);
}
void Stack::pop()
{
topOfStack--;
}
char Stack::getpop()
{
return _vector[topOfStack--];
}
void Stack::output()
{
for(int i = topOfStack-1; i != -1 ; i--)
{
cout<<"item "<<i<<"= "<<_vector[i]<<endl;
}
}

int main()
{
string _str;
cin>>_str;
Stack _stack;
for(int i=0 ;i != _str.size(); i++)
{
if((_str[i] != '+') && (_str[i] != '*'))
{
_stack.push(_str[i]);
}
else if(_str[i] == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str[i] == '*')
{
int temp;
temp = _stack.getpop() * _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
}
cout<<endl;
_stack.output();
return 0;
}

I have problem in line

else if(_str[i] == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str[i] == '*')
{
int temp;
temp = _stack.getpop() * _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}

b'coz ex. 56+
5 =53
6 =54
sum is 107 =k but i want 11 not k and push in vector is 11 plese
help.

Nov 9 '07 #1
4 1489
bushido wrote:
>
b'coz ex. 56+
5 =53
6 =54
sum is 107 =k but i want 11 not k and push in vector is 11 plese
help.
Huh? Could you use sentences. Do you know the difference between
a character and it's numeric representation? Your "cast" doesn't
make this conversion, the numeric representation in char converts
with no modification to other integral times.

Since you're using single chars, how do you think a single push is
going to push two characters?

How are you going to distinguish 56 as two tokens 5 and 6 from
56? Me thinks you should introduce some punctuation into your
program and use a non-char integral type. for example:

5 6 +

could be parsed with

int operand;
if(cin >operand) {
// converted to int
_stack.push(operand);
} else {
cin.clear(); // clear error
string operator;
cin >operator;
switch(operator[0]) {
case '+':
...
Nov 9 '07 #2
>
b'coz ex. 56+
5 =53
6 =54
sum is 107 =k but i want 11 not k and push in vector is 11 plese
help.
Note one fact that:

'0' -48
....
'5' -53
'6' -54

therefore:
'6' - '0' -6
'5' - '0' -5

You need to write "temp = (_stack.getpop() - '0') + (_stack.getpop() -
'0'); ".

Nov 9 '07 #3
bushido wrote:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Stack
{
public :
Stack();
void push(char&);
void push(const char&);
void pop();
char getpop();
A better name would be 'top'.
void output();

private:
vector<char_vector;
int topOfStack;
Why is it 'int'?
};
Stack::Stack():topOfStack(-1){}
void Stack::push(char &x)
{
topOfStack++;
Why do you need this?
_vector.push_back(x);
}
void Stack::pop()
{
topOfStack--;
Why do you need this? You can change the size of the vector.
}
char Stack::getpop()
{
return _vector[topOfStack--];
You can simply do _vector.back();
}
void Stack::output()
{
for(int i = topOfStack-1; i != -1 ; i--)
{
cout<<"item "<<i<<"= "<<_vector[i]<<endl;
}
Use _vector.size() instead of 'topOfStack'.
}

int main()
{
string _str;
cin>>_str;
Stack _stack;
for(int i=0 ;i != _str.size(); i++)
{
if((_str[i] != '+') && (_str[i] != '*'))
{
_stack.push(_str[i]);
So, you push every character from the string into the stack until
you meet a plus or an asterisk.
}
else if(_str[i] == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
When you add '5' and '6', what are you going to get? Write a simple
program:

#include <iostream>
int main() {
std::cout << "'5' + '6' is " << '5'+'6' << std::endl;
}

what do you get? Why?
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str[i] == '*')
{
int temp;
temp = _stack.getpop() * _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
}
cout<<endl;
_stack.output();
return 0;
}

I have problem in line
Huh?
>
else if(_str[i] == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str[i] == '*')
{
int temp;
temp = _stack.getpop() * _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}

b'coz ex. 56+
5 =53
6 =54
sum is 107 =k but i want 11 not k and push in vector is 11 plese
help.
You probably need to convert the char you're about to insert into its
"meaning". '5' you need to convert to value 5. '6' to value 6. YOu
can do it by subtracting '0' from the char.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 9 '07 #4
temp = (_stack.getpop()-'0') + (_stack.getpop()-'0');

not work 56+ b'coz is ';'

Nov 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.