Doesn't complete if statement properly

86 views Asked by At

I want this code to:

IF unsaved packets is greater or = 1,
then
print "There are unsaved packets"
      "Would you like to saved them?"
      "Type Y for Yes or N for No"
Get user input
      IF User input = Y then function save
 ELSE IF User input = N then exit
      ELSE Return to menue

here is my current code, the problem is it won't take the input at all and if it does it doesn't use it to determine what happens after.

if(unsavedPackets >= 1)
{
    puts("There are currently unsaved packets in the memory");
    puts("\nWould you like to save them?");
    puts("\nType Y for Yes or N for No");
    getch(saveChoice);
    printf("%c", saveChoice);
    if(saveChoice == "Y")
    {
        puts("Saving records");
        save(recordCount, records);
        exit(1);
    }
    else if(saveChoice == "N")
    {
        exit(1);
    }
    else
    {
        printf("Returning to main menu");
    }
}
break;
3

There are 3 answers

0
doptimusprime On

One problem is that

 saveChoice == "Y"

where you should write

saveChoice == 'Y'

Similarly for "N"

In the first case, you are comparing char with const char * where second is pointer, not a character.

Your break statement will always be executed no matter if condition is true or false.

2
SoulRayder On

You are doing

if(savechoice =="Y") 

it should be

if(savechoice == 'Y') 

and likewise for 'N', since you are using char variable for storing user inputs.

0
AudioBubble On

There is problem with your if statement and also else if statement.

if(saveChoice == "Y")
and
else if(saveChoice == "N")

You are comparing a char with a string. You have to compare with char with a char like this if(saveChoice == 'Y') and else if(saveChoice == 'N')

Always remember single quote for single character!!!