samedi 30 mars 2019

How would I fix this if statement in C to get it working as intended?

In this piece of code for a "contact management system" I am having difficulty in getting the intended output for a line. Basically, during this one part when you are adding a new contact, it asks you to "please enter an apartment #" as shown below:

if (yes() == 1)
{
printf("Please enter the contact's apartment number: ");
address->apartmentNumber = getInt();
if (address->apartmentNumber > 0) {
}
else {
printf("*** INVALID INTEGER *** <Please enter an integer>: ");
address->apartmentNumber = getInt();
}

}
else
{
address->apartmentNumber = 0;
}

Now, according to my assignment, you're supposed to enter the word (instead of a number, get it?) "bison" which brings up the output:

* INVALID INTEGER * Please enter an integer:

For context, this part works absolutely fine. However, you're then directed to put in the integer "-1200" which should then bring up the prompt

* INVALID APARTMENT NUMBER * Please enter a positive number:

It is this part that I'm having issue in because simply put, I don't know where to put it, whether in the if statement or outside of it. Im not sure, and would kindly like some help with this.

I have attempted to correct the problem my self, but it just gives me double of the Invalid integer output instead of this correct Invalid apartment number statement. Here is my (failed) attempt:

 if (yes() == 1)
    {
        printf("Please enter the contact's apartment number: ");
        address->apartmentNumber = getInt();
        if (address->apartmentNumber > 0) {
        }
        else {
        printf("*** INVALID INTEGER *** <Please enter an integer>: ");
        address->apartmentNumber = getInt();
        }
        if (address->apartmentNumber < 0) {
        }
        else {
        printf("*** INVALID APARTMENT NUMBER *** <Please enter a 
        positive number>: ");
        address->apartmentNumber = getInt();       
    }
        else
        {
        address->apartmentNumber = 0;
        }

Aucun commentaire:

Enregistrer un commentaire