+ problem!!!

Hi,
I am trying to make an application who will read a file (app.in) and run what it ask for, for example if in app.in i write:
2 2 + chap
it will add 2 in stack first , then add the next 2 , then pop these two and multiply them and push in stack , then print 4 and pop stack.
here are the commands:
+ - * / ^ (all work like + explained ▲)
adad which mean int , and will make a new v_int and push in var vector.
chap which print the last stack
begir (type) (name) get a type(int or string until now , need to develop more!) and put it in the variable with name (name).
estefade (type) (name) read a type(again int and string) and put it into stack.

well here is 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//includes here:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>


//name spaces here:
using namespace std;

//I-O here:
ifstream fin("app.in");

//structs here:

struct v_int{
    public:
    string name;
    int amount;
};

struct v_str{
    public:
    string name;
    string amount;
};

//variables here:
vector<int> stack;
vector<v_int> var;
vector<v_str> strin;
//functions here:

int tonum(string num){
    int ans = 0;
    while(num.size()>0){
        ans*=10;
        ans += int(num[num.size()-1]-'0');
        num.resize(num.size()-1);
    }
    return ans;
}

bool is_num(string num){
    bool ans=1;
    while(num.size()>0){
        int tmp = (num[num.size()-1]-'0');
        if(tmp>9||tmp<0) return 0;
        num.resize(num.size()-1);
    }
    return ans;
}

void order(string input){
    if(input=="adad"){
        string name;
        fin >> name;
        v_int ne;
        ne.name=name;
        var.push_back(ne);
        return ;
    }
    if(input=="reshte"){
        string name;
        fin >> name ;
        v_str ne;
        ne.name=name;
        strin.push_back(ne);
        return;
    }
    if(input=="+"){
        if(stack.size()>=2){
            int a , b;
            a=stack[stack.size()-1];
            stack.pop_back();
            b=stack[stack.size()-1];
            stack.pop_back();
            int c= a+b;
            stack.push_back(c);
        }
        else cout<<"Adad kafi qabl az + vojud nadarand!"<<endl;
        return ;
    }
    if(input=="-"){
        if(stack.size()>=2){
            int a , b;
            a=stack[stack.size()-1];
            stack.pop_back();
            b=stack[stack.size()-1];
            stack.pop_back();
            int c= a-b;
            stack.push_back(c);
        }
        else cout<<"Adad kafi qabl az - vojud nadarand!"<<endl;
        return ;
    }
    if(input=="chap$"){
        char chap;
        fin >> chap ;
        while(chap!='$')
            fin.get(chap);
        fin.get(chap);
        while(chap!='$'){
            cout<<chap;
            fin.get(chap);
            if(!fin){
                 cout<<endl<<"$ dar akhare reshte yaft nashod"<<endl;
                 return;
            }
        }
        return;
    }
    if(input=="*"){
        if(stack.size()>=2){
            int a , b;
            a=stack[stack.size()-1];
            stack.pop_back();
            b=stack[stack.size()-1];
            stack.pop_back();
            int c= a*b;
            stack.push_back(c);
        }
        else cout<<"Adad kafi qabl az * vojud nadarand!"<<endl;
        return ;
    }
    if(input=="/"){
        if(stack.size()>=2){
            int a , b;
            a=stack[stack.size()-1];
            stack.pop_back();
            b=stack[stack.size()-1];
            stack.pop_back();
            int c= a/b;
            stack.push_back(c);
        }
        else cout<<"Adad kafi qabl az / vojud nadarand!"<<endl;
        return ;
    }
    if(input=="chap"){
        if(stack.size()>=1){
            cout << stack[stack.size()-1]<<endl;
            stack.pop_back();
        }
        else cout<<"Adad kafi qabl az chap vojud nadarand!"<<endl;
        return;
    }
    if(input=="bzar"){
            string type,vari;
            fin >> type>>vari;
            if(type=="adad"){
                for(int i = 0 ; i < var.size();i++)
                    if(vari==var[i].name){
                        int a = stack[stack.size()-1];
                        stack.pop_back();
                        var[i].amount=a;
                        return ;
                    }
                cout<<"Moteqayer yaft nashod!"<<endl;
                return;
            }
            if(type=="reshte"){
                for(int i = 0 ; i < strin.size();i++)
                    if(strin[i].name==vari){
                        string a;
                        char b;
                        fin.get(b);
                        while(b!='$')
                            fin.get(b);
                        while(b!='$'){
                            a.push_back(b);
                            fin.get(b);
                        }
                        //code 2 go!
                        strin[i].amount=a;
                        return;
                    }
                cout<<"moteqayer yaft nashod"<<endl;
                return;
            }
            cout<<"Noe moteqayer yaft nashod!"<<endl;
            return;
    }
    if(input=="rchap$"){
        string name;
        fin>>name;
        for(int i = 0 ; i < strin.size();i++)
            if(strin[i].name==name){
                cout<<strin[i].amount;
                return;
            }
        cout<<"Moteqayer yaft nashod"<<endl;
        return ;
    }
    if(input=="estefade"){
        string type,vari;
        fin >> type >> vari;
        if(type == "adad"){
            for(int i = 0 ; i < var.size(); i ++)
                if(var[i].name==vari){
                    stack.push_back(var[i].amount);
                    return;
                }
            cout<<"Moteqayer yaft nashod!"<<endl;
            return ;
        }
        cout<<"noe moteqayer yaft nashod!"<<endl;
        return;
    }
    if(input=="begir"){
        string type,vari;
        fin >> type >> vari;
        if(type=="adad"){
            int a;
            cin >> a;
            for(int i = 0 ; i < var.size(); i ++){
                if(var[i].name==vari)
                    var[i].amount = a;
                    return ;
            }
            cout<<"Moteqayer yaft nashod"<<endl;
            return ;
        }
        cout<<"Noe moteqayer yaft nashod"<<endl;
        return ;
    }
    cout<<"Dastor yaft nashod!"<<endl;
}

//main function here:
int main(){
    while(fin){
        string input;
        fin >> input;
        if(is_num(input)) stack.push_back(tonum(input));
        else {
            order(input);
        }
    }
    return 0;
}


and here is the app.in i have problem with!
1
2
3
4
5
6
7
8
9
10
11
12
adad a
adad b 
begir adad a 
begir adad b 
estefade adad a 
estefade adad b 
+ 
estefade adad a
estefade adad b
chap
chap
chap

it must get to int , add the + of them to stack , print those int , print the + of them , but look your self!!!
the anser for 2 2 is this:

2
2686400
2686402

what should i do now???
Thanks for your helpful answers!
Hi,

Please be specific and clear, write where you really need a help. If you just post a huge list of code perhabs no one will read your code, be simple and clear...

Well in that case

Thhere is a vector of a struct which save an int and an string
I get two of these structs , and then print + of them .

It give wrong answer , why?
Hi

your first problem is in

1
2
3
4
5
 for(int i = 0 ; i < var.size(); i ++){
                if(var[i].name==vari)
                    var[i].amount = a;
                    return ;
            }


it should be like following

1
2
3
4
5
6
 for(int i = 0 ; i < var.size(); i ++){
                if(var[i].name==vari){
                    var[i].amount = a;
                    return ;
               }
 }


you should correct this, if I find other error I'll post it

for you to better see the problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(input=="begir"){
		string type,vari;
		fin >> type >> vari;
		if(type=="adad"){
			int a;
			cin >> a;
			for(int i = 0 ; i < var.size(); i ++){
				if(var[i].name==vari){
					var[i].amount = a;
					return ;
				}
			}
			cout<<"Moteqayer yaft nashod"<<endl;
			return ;
		}
		cout<<"Noe moteqayer yaft nashod"<<endl;
		return ;
	}


I recommend you to use switch case, if you have so many cases( if's)
Last edited on
Wow
Thx , didn't even notice that
Working now
Thx
Topic archived. No new replies allowed.