简体   繁体   中英

Need help C++ calculator with word input

Hi again I am attempting a Bjarne Stroustrup exercise from his PPP book. I have managed to do most of the exercises, however I am having problem with one.

In this program the basic idea is to have a calculator which can take both integer and string input. The actual calculator part works fine and I can have integer inputs with no problem. My problem lies when trying to convert a string input say "one" to the integer one.

My idea of how to do this was have a for loop run through my vector which stores numbers 1-10 in words, and when it finds an index which contains a string which matches the users input it uses the for loops counter variable; which should then equal the amount the user typed in.

This idea should work, and Bjarne's sample code uses a similar idea, however mine is slightly different and doesn't seem to work, the problem I'm having appears to be the fact that when comparing the user input to the vector indexes it doesn't seem to find any matches, I've been messing around with it for hours and can't seem to find the reason. Anyway here is the code:

//simple calculator program, users can input words 1-10 and an integer will be     returned. 

// header files
#include "../../std_lib_facilities.h"

//global varible- vector set up here. 
vector<string> numbers;

//functions, one to initiliase vectors, one to get number, and a main function. 

void initiliase() {  
numbers.push_back ("zero");
numbers.push_back ("one");
numbers.push_back ("two");
numbers.push_back ("three");
numbers.push_back ("four");
numbers.push_back ("five");
numbers.push_back ("six");
numbers.push_back ("seven");
numbers.push_back ("eight");
numbers.push_back ("nine");
numbers.push_back ("ten");
}

int get_number(){
char choice;
string type_val;
int val = 0;
cout << "do you wish to enter a number or word? n/w" << endl;
cin >> choice;

if ( choice == 'n'){
cin >> val;
return val;
}

else if(choice == 'w'){
cin >> type_val;
for (int i = 0; i<numbers.size(); i++){
    if (numbers[i] == type_val)
    val = i;
else
    cout << "number not found in vector.";
    return val;
 }
}
}

      void print_answer(int ans, char oper, int val1,int val2) {
cout << "Your Answer is:" << ' ' << val1 << ' ' << oper << ' ' << val2 << ' ' << '=' <<   ans << endl;

}

void main() {

initiliase(); 
int val1, val2, answer;
char op;
val1 = get_number();
val2 = get_number();
cout << "Please input operation:";
cin >> op; 
switch (op){

case '+': cout << "You have chosen addition!" << endl;
      answer = val1 + val2;
      print_answer (answer, op, val1, val2);
      break;
case '-': cout << "you have chosen subtraction!" << endl;
      answer = val1 - val2;
          print_answer (answer, op, val1, val2);
      break;
case '*': cout << "you have chosen multiplication!" << endl;
          answer = val1 * val2;
          print_answer (answer, op, val1, val2);
      break;
case '/': cout << "you have chosen division!" << endl; 
          answer = val1 / val2;
      print_answer  (answer, op, val1, val2);
      break;
case '%': cout << "you have chosen modulos!" << endl;
      answer = val1 % val2;
      print_answer (answer, op, val1, val2);
      break;
default: cout << "incorrent operation" << endl;
}
keep_window_open ("~");
}

You have a problem with your final for loop.
Currently, it cannot return anything other than 0, the problem is that your else statement is inside your loop, an thus refers to if (numbers[i] == type_val) .

You might want to try something like :

if ( choice == 'n')
{
    cin >> val;
}
else if(choice == 'w')
{
    cin >> type_val;
    bool found = false;
    for (int i = 0; i<numbers.size() && !found; i++){
        if (numbers[i] == type_val)
        {
            val = i;
            found = true;
        }
    }
    if(!found){ cout << "Number not found."; }
}
return val;

You could use a hashmap to store the correspondency between the string and the number:

std::unordered_map<std::string,int> string_to_integer;

//Hashmap setup:
string_to_integer["one"] = 1;
string_to_integer["two"] = 2;
//... etc.

//Example of use:

std::string input;
int input_number;

std::cin >> input;
input_number = string_to_integer[input];

Note that this also is valid for operators, to avoid the repetitive code of the switch . But I not supply an example of that because you are beggining with C++, and I don't want to confuse you with more advanced code. Try string_to_input first, and when you understand that, ask for more information.

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM