-4

Today I learned about if and else if. So an idea came in my mind and I make a program, that takes six numbers and ask for a choice, so the adding decision work correctly, but other two also work as first decision. What is the solution?

#include <iostream.h>
#include <conio.h>

void main ()
{
    int a, a1, a2, a3, a4, a5, adse, sum, sub;

    cout << "\nPlease Enter The First Number: ";
    cin >> a;
    cout << "\nPlease Enter The Second Number: ";
    cin >> a1;
    cout << "\nPlease Enter The Third Number: ";
    cin >> a2;
    cout << "\nPlease Enter The Fourth Number: ";
    cin >> a3;
    cout << "\nPlease Enter The Fifth Number: ";
    cin >> a4;
    cout << "\nPlease Enter The sixth Number: ";
    cin >> a5;

    sum = a + a1 + a2 + a3 + a4 + a5;
    sub = a - a1 - a2 - a3 - a4 - a5;

    cout << "\nPlease Select the chioce (1, 2, or 3)";
    cout << "\n1: Adding";
    cout << "\n2: Subtracting";
    cout << "\n3: Exit";
    cout << "\nPlease Enter Your Choice: ";
    cin >> adse;

    if (adse = 1)
    {
        cout << "\nThe Addition is : "<<sum;
    }
    else if(adse = 2)
    {
        cout << "\nThe Subtraction is : "<<sub;
    }
    else if(adse = 3)
    {
        cout << exit(0);
    }
    else
    {
        cout << "\nSorry Wrong Choice, Program is Closing";
    }
    getch();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 6
    Use a compiler that wasn't meant for close to 20 years ago and turn up the warning level. – chris Jun 21 '15 at 01:06
  • Beginner-level mistakes in C or C++, like assignment instead comparison, e.g. `if (adse = 1)` here, ***should not be answered***. They should be closed as a duplicate of a canonical question (that also has much better answers). Or at least indicated (so others can close it). There is a canonical question from the first few days of Stack Overflow existence (September 2008). There is not even an indication here of what the canonical question might be. – Peter Mortensen Jun 26 '22 at 14:50
  • The canonical is not easy to find. But it must be there somewhere. New trial: [List of the oldest C questions](https://stackoverflow.com/questions/tagged/c?tab=newest&page=7630&pagesize=50) (the oldest visible is from 2008-08-01 (from the private beta)). That doesn't work either (too advanced). – Peter Mortensen Jun 26 '22 at 15:03
  • Candidate: *[Why would you use an assignment in a condition?](https://stackoverflow.com/questions/151850/why-would-you-use-an-assignment-in-a-condition/151869#151869)*. An answer says *"GCC can help you detect (with -Wall) if you unintentionally try to use an assignment as a truth value"*. But there be a better one, with a question more directly containing this problem. – Peter Mortensen Jun 26 '22 at 15:10
  • Candidate: *[Why would you use an assignment in a condition?](https://stackoverflow.com/questions/151850/why-would-you-use-an-assignment-in-a-condition/151869#151869)*. An answer says *"GCC can help you detect (with -Wall) if you unintentionally try to use an assignment as a truth value"*. But there must be a better one, with a question more directly containing this problem. – Peter Mortensen Jun 26 '22 at 15:23

2 Answers2

5

You should use "==" instead of "=" in comparisons.

HelpMe000
  • 98
  • 2
  • 5
0

First

You need to use == rather than = when comparing statements. This is because in your example, adse is not being compared, but adse is being set to one, just like if you wrote adse = 1;.

Now bools are true/false statements, but when stored in memory represent a 0 for false and a true is 1. So when you test the state of a boolian you are actialy testing if its equal to one or not. Now for how this relates to your question. Because adse is being set to one, the if statement is the same as if (true) and will always enter. Because the first statement will always enter, the else statement will never execute and there-four will never run

To fix, replace = with == in conditional statements, like this:

if (adse == 1)
{
    ....
}

Second

Indent your code, it makes it much easier to read for others and yourself.

Kieren Pearson
  • 416
  • 3
  • 15
  • `the other two do not work because they are 2 and 3`: that's not the reason. http://ideone.com/wtaRFq -- they are not executed because the first branch is taken (note the `else if`s) – Diego Jun 21 '15 at 02:22