Need help C++ calculator with word input -


hi again attempting bjarne stroustrup exercise ppp book. have managed of exercises, having problem one.

in program basic idea have calculator can take both integer , string input. actual calculator part works fine , can have integer inputs no problem. problem lies when trying convert string input "one" integer one.

my idea of how have loop run through vector stores numbers 1-10 in words, , when finds index contains string matches users input uses loops counter variable; should equal amount user typed in.

this idea should work, , bjarne's sample code uses similar idea, mine different , doesn't seem work, problem i'm having appears fact when comparing user input vector indexes doesn't seem find matches, i've been messing around hours , can't seem find reason. anyway here code:

//simple calculator program, users can input words 1-10 , integer     returned.   // header files #include "../../std_lib_facilities.h"  //global varible- vector set here.  vector<string> numbers;  //functions, 1 initiliase vectors, 1 number, , 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 wish enter number or word? n/w" << endl; cin >> choice;  if ( choice == 'n'){ cin >> val; return val; }  else if(choice == 'w'){ cin >> type_val; (int = 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 problem final for loop.
currently, cannot return other 0, problem else statement inside loop, refers if (numbers[i] == type_val).

you might want try :

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

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -