Need help debugging!

Posts 112 of 12 · Page 1 of 1
Need help debugging! ( C++ )
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

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;
}
Also if you could tell me some tips that would be great, since this is the first programming language I tried to learn!
You need to compare the string to each word separately:

Code:
if (str == "addition" or str == "Addition" or str == "add" or str == "Add"){}
The best advice is to read a book at the beginning, but you are already reading a book so
Quote Originally Posted by Nashiro View Post
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

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;
}
Also if you could tell me some tips that would be great, since this is the first programming language I tried to learn!
Quote Originally Posted by Callme? View Post
You need to compare the string to each word separately:

Code:
if (str == "addition" or str == "Addition" or str == "add" or str == "Add"){}
The best advice is to read a book at the beginning, but you are already reading a book so
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.
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"){}
Quote Originally Posted by Nashiro View Post
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"){}
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:
Code:
using std::cout; using std::cin; using std::endl;
- instead on using "\n" use std::endl; (or without std:: if you predefined with using .) It would look like this
Code:
cout << "hi this is a string" << endl;
Lastly from thag little snippet you sent I can't tell whats wrong. While seems to be used properly.
Okay, thanks again!
Quote Originally Posted by Nashiro View Post
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"){}
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.
Quote Originally Posted by Callme? View Post
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.
Cool! it works now thanks!
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?
Quote Originally Posted by MikeRohsoft View Post
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?
This is what I was going to say, "or" is not a thing in C++ it should be || for "or" and && for "and".
Quote Originally Posted by Unicow View Post
This is what I was going to say, "or" is not a thing in C++ it should be || for "or" and && for "and".
You can use "and" and "or" in C++ aswell as "&&" and "||". Maybe one of the solutions is cleaner than the other one, but both are working just fine.
Quote Originally Posted by Callme? View Post
You can use "and" and "or" in C++ aswell as "&&" and "||". Maybe one of the solutions is cleaner than the other one, but both are working just fine.
You are able to use "and" and "or" and other operators in plain text because in the Past you could have Keyboards without Pipe | or ^.
This Operators are default aliases in C, even ANSI C.

It was anyway weird for me ^.^
Posts 112 of 12 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?