1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Another C++ Q

Discussion in 'Software Development' started by hyler, Oct 1, 2003.

Thread Status:
Not open for further replies.
  1. hyler

    hyler Thread Starter

    Joined:
    Jul 30, 2001
    Messages:
    126
    When I compile my program, the IF statements fail to work correctly. I ask the user to input my newly defined int variable, and have the IF statements run off of whatever they put in, but no matter what they enter, it starts up on the first IF statement.

    int choice;
    cin>>choice;
    if (choice = 1)
    cout<<"Addition-";
    cout<<"Enter first number: ";
    int first;
    cin>>first;
    cout<<"Enter second number: ";
    int second;
    cin>>second;
    cout<<first + second;
    goto start;
    if (choice = 2)
    cout<<"Subtraction-";
    cout<<"Enter first number: ";
    int first1;
    cin>>first1;
    cout<<"Enter second number: ";
    int second1;
    cin>>second1;
    cout<<first1 - second1;
    goto start;
    if (choice = 3)
    cout<<"Multiplication-";
    cout<<"Enter first number: ";
    int first2;
    cin>>first2;
    cout<<"Enter second number: ";
    int second2;
    cin>>second2;
    cout<<first2 * second2;
    goto start;
    if (choice = 4)
    cout<<"Division-";
    cout<<"Enter first number: ";
    int first3;
    cin>>first3;
    cout<<"Enter second number: ";
    int second3;
    cin>>second3;
    cout<<first3 / second3;
    goto start;
    }
     
  2. deuce868

    deuce868

    Joined:
    Nov 2, 2000
    Messages:
    637
    to check you need two = signs, not just one. All you are doing is setting choice = 1 each time.
     
  3. Guy

    Guy

    Joined:
    Feb 19, 1999
    Messages:
    260
    Just thought I would add that a good coding habit to get into that will help new coders (and advanced one too :p) locate this common error easily is to put the constant as the first element in the check. If you do this the error will be produced at compile time and is much easier to remedy.

    ie:
    Code:
    // do it like this
    if(1 == choice)
    {
    ...
    }
    // rather than like this
    if(choice == 1)
    {
    ...
    }
    if you leave out an = sign in the first if statement, you will get a compile time error because you cannot assing a value to a constant, but if you do it the second way, the program will run, but it will behave strangely and unexpectedly.

    Cheers
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/168781

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice