Results 1 to 12 of 12
  1. #1
    Nashiro's Avatar
    Join Date
    May 2016
    Gender
    male
    Posts
    5
    Reputation
    10
    Thanks
    0
    My Mood
    Fine

    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!
    Last edited by Nashiro; 05-11-2016 at 07:31 AM. Reason: for clarification

  2. #2
    Callme?'s Avatar
    Join Date
    Dec 2015
    Gender
    male
    Posts
    13
    Reputation
    10
    Thanks
    4
    My Mood
    Stressed
    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

  3. The Following User Says Thank You to Callme? For This Useful Post:

    Nashiro (05-11-2016)

  4. #3
    Sammy's Avatar
    Join Date
    Mar 2016
    Gender
    male
    Location
    Vaero
    Posts
    1,102
    Reputation
    224
    Thanks
    228
    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.

  5. The Following User Says Thank You to Sammy For This Useful Post:

    Nashiro (05-11-2016)

  6. #4
    Nashiro's Avatar
    Join Date
    May 2016
    Gender
    male
    Posts
    5
    Reputation
    10
    Thanks
    0
    My Mood
    Fine
    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.

  7. #5
    Sammy's Avatar
    Join Date
    Mar 2016
    Gender
    male
    Location
    Vaero
    Posts
    1,102
    Reputation
    224
    Thanks
    228
    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.

  8. The Following User Says Thank You to Sammy For This Useful Post:

    Nashiro (05-11-2016)

  9. #6
    Nashiro's Avatar
    Join Date
    May 2016
    Gender
    male
    Posts
    5
    Reputation
    10
    Thanks
    0
    My Mood
    Fine
    Okay, thanks again!

  10. #7
    Callme?'s Avatar
    Join Date
    Dec 2015
    Gender
    male
    Posts
    13
    Reputation
    10
    Thanks
    4
    My Mood
    Stressed
    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.

  11. The Following User Says Thank You to Callme? For This Useful Post:

    Nashiro (05-12-2016)

  12. #8
    Nashiro's Avatar
    Join Date
    May 2016
    Gender
    male
    Posts
    5
    Reputation
    10
    Thanks
    0
    My Mood
    Fine
    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!

  13. #9
    MikeRohsoft's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Los Santos
    Posts
    797
    Reputation
    593
    Thanks
    26,317
    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?

  14. #10
    Unicow's Avatar
    Join Date
    Jun 2010
    Gender
    male
    Location
    With My Unicows
    Posts
    2,711
    Reputation
    70
    Thanks
    216
    My Mood
    Lurking
    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".

  15. #11
    Callme?'s Avatar
    Join Date
    Dec 2015
    Gender
    male
    Posts
    13
    Reputation
    10
    Thanks
    4
    My Mood
    Stressed
    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.

  16. #12
    MikeRohsoft's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Los Santos
    Posts
    797
    Reputation
    593
    Thanks
    26,317
    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 ^.^

Similar Threads

  1. [Help] I need help debugging a nil script (Lua coding)
    By TearInHeaven in forum Other Programming
    Replies: 0
    Last Post: 03-16-2015, 04:06 PM
  2. [Help Request] Need Help
    By shane11 in forum CrossFire Help
    Replies: 49
    Last Post: 05-03-2011, 04:29 AM
  3. [Help Request] Need help finding a Vindictus injector
    By VpChris in forum Vindictus Help
    Replies: 2
    Last Post: 05-01-2011, 10:51 PM
  4. [Help Request] I need help~~!!!!
    By c834606877 in forum Alliance of Valiant Arms (AVA) Help
    Replies: 1
    Last Post: 05-01-2011, 01:12 AM
  5. [Help Request] need help with modding
    By BayBee Alyn in forum Combat Arms Help
    Replies: 0
    Last Post: 04-27-2011, 09:06 PM