You need to compare the string to each word separately:
The best advice is to read a book at the beginning, but you are already reading a book soCode:if (str == "addition" or str == "Addition" or str == "add" or str == "Add"){}
So I decided to learn C++ yesterday and while halfway through reading an e-book I decided to try out some of the things I learned.
I tried to make a really simple addition and subtraction calculator.
Everything seems to work fine when I run it and use the addition function however I cannot select other operations except addition.
The compiler's debugger doesn't give me any errors so I am confused.
Please help!
Here is my code
I use Code::blocks 16.01
Also if you could tell me some tips that would be great, since this is the first programming language I tried to learn!Code:#include <iostream> #include <string> using namespace std; const string end ("end"); int add (float a, float b) { return (a + b); } int subtract (float a, float b) { return (a - b); } int main () { string str; float a, b; float c; while (str != "exit" or "Exit") { cout << "What operation whould you like to perform?\n"; cout << "Addition, Subraction, Exit\n"; cin >> str; if (str == "addition" or "Addition" or "add" or "Add") { cout << "You have selected Addition.\n"; cout << "let : a + b = c\n"; cout << "Assign a value for a..."; cin >> a; cout << "Assign a value for b..."; cin >> b; c = add (a, b); cout << a << "+" << b << "=" << c << "\n"; } else if (str == "subtraction" or "Subtraction" or "sub" or "Sub") { cout << "You have selected Subtraction.\n"; cout << "let : a - b = c\n"; cout << "Assign a value for a..."; cin >> a; cout << "Assign a value for b..."; cin >> b; c = subtract(a, b); cout << a << "-" << b << "=" << c << "\n"; } else if (str != "exit") { cout << "Invalid operation."; } } return 0; }
Last edited by Nashiro; 05-11-2016 at 07:31 AM. Reason: for clarification
You need to compare the string to each word separately:
The best advice is to read a book at the beginning, but you are already reading a book soCode:if (str == "addition" or str == "Addition" or str == "add" or str == "Add"){}
Nashiro (05-11-2016)
As said above that mainly is your problem. Just to give some extra explanation basically what happens is this:
It checks first if the string's content is equal to "addition". if not it will check "Addition". What does it check? It checks if its able to initialize that sequence. Basically it will Always return true. So that's why it will Always go for addition. It returns true and has no more reason to check any other conditions, neither in else if.
Nashiro (05-11-2016)
Thanks guys it helped!
now my next problem is it won't exit when I enter string "exit".
Am I mis-using the while loop?
Code:while (str != "exit" or str != "Exit"){}
Last edited by Nashiro; 05-11-2016 at 05:36 PM.
Just some more tips:
- Don't use this: using namespace std; because good chance you will get conflicts when naming when functions. Instead do this if you want to shorten certain functions:
- instead on using "\n" use std::endl; (or without std:: if you predefined with using .) It would look like thisCode:using std::cout; using std::cin; using std::endl;Lastly from thag little snippet you sent I can't tell whats wrong. While seems to be used properly.Code:cout << "hi this is a string" << endl;
Nashiro (05-11-2016)
Okay, thanks again!
You need to replace the 'or' with an 'and'
If you use 'or' as comparison operator the while loop won't exit if one or both of the statements are true . In your case there will be always one statement true, so you cant use the 'or' operator. You need to use the 'and' operator because then the loop won't exit if both statements are true, if now one statement is false the loop will be exit.
Nashiro (05-12-2016)
I write less code with c++ so, it isn't really a surprise i didn't knew it,
but it is really new for me, "or" and "and" are working too.
I thought "and" and "or" are for Pascal and || and && for C...
Is it working with ANSI C too?