Jump to content

how do i make the decision work

- - - - -

  • Please log in to reply
4 replies to this topic

#1
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
how do i make this nested if decision work

is the weather good,
if yes
I will go out in the yard.
if No
"Oh sorry u can not go out sit indoors \n"

int main()

{

puts("this is just to test nested ifs \n");


char num0;


printf("press y for Yes and n for No \n");

printf("is the weather good \n");

scanf("%c",&num0);

if (num0 == 'y')

{

printf("Good u can go out now \n");

if(num0 == 'n')

printf("Oh sorry u can not go out sit indoors \n");

}

return 0;

}


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
This program does not require a nested if statement. You nearly have it right, all you'd have to do is get rid of the internal if statement and do as follows:
if (num0 == 'y')

{

    puts("Good u can go out now.");

}

else

{

    puts("Oh sorry u can not go out sit indoors.");

}
I'd also check if the user input is correct.
Wow I changed my sig!

#3
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
thank z

#4
atoivan

atoivan

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
what is u want to do it nested if how will it be

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

atoivan said:

what is u want to do it nested if how will it be
I am sure you could do it like this, to provide a more useful nesting.
if (num0 == 'y' || num0 == 'n') {

    if(num0 == 'y') {

         ...

    } else {

         ...

    }

} else {

   /* invalid input */

}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users