storing string characters into integer vectors

Can someone help me find what is causing the program to crash. I've been trying for awhile and cant figure it out.
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

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
//stephen I
using namespace std;


int main()
{
    
    string userE;// user equation
    string sHold; // string holdr
    int userNum , ac = 0 , bc = 0, dc = 0;
    vector<int> iNumber;
    vector<char> iChar;
    
    
    getline(cin, userE); // grab equation
    
    bc++;
    for(int i = 0; i < userE.length();i++)
    {
        
        cout << " a";
        if ( userE.at(i) != '*' && userE.at(i) != '+' && userE.at(i) !='-' && userE.at(i) != ' ' && userE.at(i) !='/' )
        {
            cout << "b";
            do {
                
                
                cout << "c";
                sHold = userE.substr(i, dc);
                userNum = atoi(sHold.c_str());
                iNumber.push_back(userNum);
                dc++;
            } while (userE.at(i+dc) != '*' && userE.at(i+dc) != '+' && userE.at(i+dc) !='-' && userE.at(i+dc) != ' ' && userE.at(i+dc) !='/');
            
            cout << iNumber[0];
        }
         if ( userE.at(i) =='*' || userE.at(i) == '+' || userE.at(i) == '-')
        {
            iChar.push_back(userE.at(i));
            cout << iChar[ac] << endl; // need a seperate counter
            ac++;
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
}
Last edited on
well, I ran it with a simple 3+2 equasion. briefly debugging, first time through the do{} loop sHold is empty after the substr(), thus userNum is 0. This seems to be because dc is 0 at that point.

You really should choose better names for variables, and throw in a comment or three to let people know what the code is supposed to accomplish.
Topic archived. No new replies allowed.