-6

how do I run only one of those if statements in a for loop? For example i have an input of 5...and i just want it to print five...but whenever i run this code, it will execute all if statement..please help me

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a;
    int b;

    cin >> a;

    for (a = 0; 0<a<10; a++)
        {
            if (a == 1)
                {
                    cout << "one";
            }
        if (a == 2)
                {
                    cout << "two";
            }
        if (a == 3)
                {
                    cout << "three";
            }
        if (a == 4)
                {
                    cout << "four";
            }
        if (a == 5)
                {
                    cout << "five";
            }
         if (a == 6)
                {
                    cout << "six";
            }
        if (a == 7)
                {
                    cout << "seven";
            }
         if (a == 8)
                {
                    cout << "eight";
            }
        if (a == 9)
                {
                    cout << "nine";
            }

        else if (a > 9 && a%2 == 0)
            {
                cout << "even";
        }
        else if (a > 9 && a&2 != 0)
            {
                cout << "odd";
        }
    }
    return 0;
}
AJ Tech
  • 13
  • 5
  • what do you want to achieve? you are taking input in a, but again initialize a to 0 in the loop. – Pirate Sep 30 '16 at 09:03
  • 2
    Remove `for` loop. – msc Sep 30 '16 at 09:03
  • 1
    You need to find a [good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read up on conditional expressions. The one in the (unnecessary) `for` loop is not correct. And while reading the book, red about the `switch` statement. And arrays (you don't need a big chain of `if-else` or a `switch` statement). – Some programmer dude Sep 30 '16 at 09:03

3 Answers3

1

The problem seems to be the for loop. Your program accepts a value for a as an input, but then as soon as the loop begins, it resets the value of a to 0 (for (a = 0;...

Therefore it's looping 10 times, and on each loop a will have a different value, starting from 0 and ending at 9. This means that all of your if statements will get hit at some point in the execution, generally one on each of the loops round the for.

To get your expected behaviour " input of 5...and i just want it to print five", simply remove the for loop from your code.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Nice answer but don't forget to mention something about 0 < a < 10; feel free to take the points in my answer. – Bathsheba Sep 30 '16 at 09:18
  • @Bathsheba good point I hadn't noticed that. But if OP removes the loop it won't matter anyway. I think you've edited your answer since I posted mine...so yours is probably better now :-) – ADyson Sep 30 '16 at 09:27
-1

Your unnecessary for loop trashes the input value of a and loops forever! (At least until you overflow your signed type a).

You are replacing a by using it as the counter in the for loop! If you only ever want one output, then drop the for loop completely. If your for loop were to remain then your expression 0 < a < 10 ought to be recast as 0 < a && a < 10 : formally 0 < a < 10 is evaluated as (0 < a) < 10 which is either true < 10 or false < 10 which is always true.

Also consider refactoring your if else to set up explicitly mutually exclusive statements:

if (a == 1){
    cout << "one";
} else if (a == 2){
    cout << "two";    
/*and so on*/
} else {
    /*all other cases*/
}

Although in this case you might want to consider a switch block:

switch (a){
case 1:
    cout << "one";
    break; // to stop program control flowing into the next case
case 2:
    cout << "two";
    break;   
/*and so on*/ 
default:
    /*all other cases*/
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Apologies @Downvoters, I did have the wrong end of the stick initially. – Bathsheba Sep 30 '16 at 09:07
  • What if a problem asked me to use for loop and ask to to output this : For each integer (so all numbers in that range): If , 1<= n <=9 then print the English representation of it. That is "one" for 1, "two" for 2, and so on. Else if n>9 and it is even, then print "even". Else if n>9 and it is odd, then print "odd". – AJ Tech Sep 30 '16 at 09:55
-1
if () {
} else if () {
}

Although,

switch() {
}

will be more efficient in your case.

Update 1

@Aleph0

Below is the solution

int main() {
    int a;
    cin >> a;

    switch (a) {
    case 1: cout << "one"; break;
    case 2: cout << "two"; break;
    case 3: cout << "three"; break;
    case 4: cout << "four"; break;
    case 5: cout << "five"; break;
    case 6: cout << "six"; break;
    case 7: cout << "seven"; break;
    case 8: cout << "eight"; break;
    case 9: cout << "nine"; break;
    default: cout << ((a & (1 << 31)) ? "negative" : (a & 1) ? "odd" : "even"); break;
    }
}

Question has been asked to do following

i have an input of 5...and i just want it to print five

And, someone has correctly mentioned above, for loop is immaterial here.

user3161880
  • 1,037
  • 6
  • 13