Array bug in calculator program.

C++ is my first language and I've been reading the tutorial on this site too long without actually programming anything (got almost to the end without learning to use functions properly) so I'm going back to the basics - forgive me if I am asking stupid questions. Anyway I am trying to make a calculator that accepts a string like "9x53", breaks it down to it's components (first number, second number and x) and then does the math. My code uses a loop to assign each number or symbol to either the index of a string (next step is to turn it into a whole number) or a character to represent x, *, + etc. Anyway everything encodes with no errors but when I check to see that everything is assigned as it should be the array that holds the individual digits to be converted into whole numbers spits out "4953" when I input 15x9, it should spit out "159". Any ideas?

Oh, and apologies for the x, y z maze.

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
#include <iostream>
#include<string>
#include <sstream>

using namespace std;

string problem;
char symbol;
int numbers[2][11];
int numberofnumbers=0;
void getproblem();

void breakdown (string& x, int y[2][11], int&z)
{
  for (int n=0, z=0;x[n]!='N';n++)
{   if (x[n]==' ') continue;
        if (x[n]=='x'||x[n]=='X'||x[n]=='*'||x[n]=='/'||x[n]=='+'||x[n]=='-')
            {symbol = x[n]; z++; continue;}
        if (x[n]>=0||x[n]<=9) y[z][n]=x[n];
    else {cout<<"Error, error - danger Will Robinson!  Try Again.";
    getproblem();}
}


}
void getproblem()
{cout<<"Give me a problem to solve, e.g. 15x9 or 9-7:"<<endl;
getline(cin, problem);
breakdown(problem, numbers, numberofnumbers);}

int main()
{
getproblem();
cout<<(int)numbers[0][0]<<(int)numbers[0][1];

return 0;
}
Your cout statement at the very end is printing out the ASCII values of '1' and '5.' Note that you made the same mistake on line 19. You are checking if the ASCII value of x[n] is between 0 and 9. A simple correction would be to use chars, i.e. '0' and '9.' Of course, you are still assigning the ASCII value of x[n] to y[n] and not the number you are expecting.
To convert characters/strings to numbers, use the stringstream object.
1
2
3
4
5
std::stringstream catalyst;
string thisthing("101");
int final;
   catalylst << thisthing;
   catalyst >> final;


Edit:
A way to convert individual characters to numbers is to subtract the ASCII value of '0.'
1
2
char first('7');
int final = first - '0';
Last edited on
Thank you, that is a fantastic explanation. Not sure on how to use stringstream but I will review my notes and the tutorial section on it and I'm sure I'll be fine. If I may trouble you (or whomever) for a follow up answer, what determines whether the compiler/program sees an object as the ASCII code or as the value it points to?
I also worked out that changing

cout<<(int)numbers[0][0]<<(int)numbers[0][1];

to

cout<<(char)numbers[0][0]<<(char)numbers[0][1];

did the trick too. Is there any practical advantage to using the stringstream method?
Using arrays will only work with single digit numbers. In order to convert strings to int/float/double with stringstream, you would design your own algorithm to translate the number of digits into digits with a position (e.g. "100" to 1*100 + 0*10 + 0*1). In addition, you would need to figure out how to tell if the number in string form is negative, a decimal, an integer, in scientific notation, etc.
Just as std::string was designed as an easier way to work with an array of characters, you can use std::stringstream because it was created to make your life easier.
The purpose of the exercise is to learn how to code my own algorithms. I'm not to the point where I feel comfortable jumping into using libraries. I used getline but only because it was suggested in the tutorial. If this is a stupid approach feel free to let me know.
Oh, then by all means go ahead. :3
Just be careful that you are working with chars and not ints.
Topic archived. No new replies allowed.