Click to See Complete Forum and Search --> : nested if headache


codenewb
February 10th, 2003, 09:25 AM
I had to modify my code to accept when invalid data would be entered, it will now print invalid data entered, but it skips an entire section of my if/else statement?


#include <stdio.h>

#define P 10.50
#define PMON 12.50
#define PPRIN 13.50
#define PMP 15.00
#define PII 15.50
#define PIIMON 18.50
#define PIIPRIN 19.00
#define PIIMP 21.00

main()
{
char comptype, rentmon, rentprint;
int rentdays;
double totamnt, rate;

printf("\nEnter type of computer rented (S=Pentium or P=Pentium II) : ");
scanf("%c", &comptype);

if (comptype == 'S' || comptype == 's' || (comptype == 'P' || comptype == 'p'))
{
printf("\nWas a monitor rented? : ");
getchar();
rentmon = getchar();
if (rentmon == 'Y' || rentmon == 'y' || (rentmon == 'N' || rentmon == 'n'))
{
printf("\nWas a printer rented? : ");
getchar();
rentprint = getchar();
if (!(rentprint == 'Y' || rentprint == 'y' || rentprint == 'N' || rentprint == 'n'))
{
printf("\nInvalid data entered\n");

}
else
{
if (comptype == 'S' || (comptype == 's'))
{
if (rentmon == 'Y' || (rentmon == 'y'))
{
if (rentprint == 'Y' || (rentprint == 'y'))
{
rate = PMP;
}
else
{
rate = PMON;
}
}
else
{
(rentmon == 'N' || (rentmon == 'n'));
if (rentprint == 'Y' || (rentprint == 'y'))
{
rate = PPRIN;
}
else
{
rate = P;
}
}
}
else
{
(comptype == 'P' || (comptype == 'p'));
if (rentmon == 'Y' || (rentmon == 'y'))
{
if (rentprint == 'Y' || (rentprint == 'y'))
{
rate = PIIMP;
}
else
{
rate = PIIMON;
}
}
else
{
(rentmon == 'N' || (rentmon == 'n'));
if (rentprint == 'Y' || (rentprint == 'y'))
{
rate = PIIPRIN;
}
else
{
rate = PII;
}
}
}
}
}
else
{
printf("\nInvalid data entered\n");
}
}
else
{
printf("\nInvalid data entered\na");
}
}


It just says:
Was a monitor rented? : y
was a printer rented? : y
and then just says hit any key to continue

I think the problem is related somewhere in this area

if (!(rentprint == 'Y' || rentprint == 'y' || rentprint == 'N' || rentprint == 'n'))
{
printf("\nInvalid data entered\n");

}
else
{
if (comptype == 'S' || (comptype == 's'))
{
if (rentmon == 'Y' || (rentmon == 'y'))

willchop
February 10th, 2003, 09:43 AM
I'm not seeing where you have the program outputting anything
after the printer renting prompt. It looks like you have entered
valid data in your example. Do you expect to see an notification
of invalid data?

regards, willchop

codenewb
February 10th, 2003, 10:05 AM
Oh man I feel stupid, I'm sorry I have been struggling with this for about an hour and I guess all I needed was someone else to look at it to see what was wrong with it. Thanks

codenewb
February 10th, 2003, 11:22 AM
#include <stdio.h>
#include <ctype.h>

#define P 10.50
#define PMON 12.50
#define PPRIN 13.50
#define PMP 15.00
#define PII 15.50
#define PIIMON 18.50
#define PIIPRIN 19.00
#define PIIMP 21.00
#define PEN S
#define PENII P
#define YES Y
#define NO N


main()
{
char comptype, rentmon, rentprint;
int rentdays;
double totamnt, rate;

printf("\nEnter type of computer rented (S=Pentium or P=Pentium II) : ");
scanf("%c", &comptype);

if (comptype == 'S' || comptype == 's' || (comptype == 'P' || comptype == 'p'))
{
printf("\nWas a monitor rented? : ");
getchar();
rentmon = getchar();
if (rentmon == 'Y' || rentmon == 'y' || (rentmon == 'N' || rentmon == 'n'))
{
printf("\nWas a printer rented? : ");
getchar();
rentprint = getchar();
if (!(rentprint == 'Y' || rentprint == 'y' || rentprint == 'N' || rentprint == 'n'))
{
printf("\nInvalid data entered\n");

}
else
{
if (comptype == 'S' || (comptype == 's'))
{
if (rentmon == 'Y' || (rentmon == 'y'))
{
if (rentprint == 'Y' || (rentprint == 'y'))
{
rate = PMP;
}
else
{
rate = PMON;
}
}
else
{
(rentmon == 'N' || (rentmon == 'n'));
if (rentprint == 'Y' || (rentprint == 'y'))
{
rate = PPRIN;
}
else
{
rate = P;
}
}
}
else
{
if (comptype == 'P' || (comptype == 'p'))
{
if (rentmon == 'Y' || (rentmon == 'y'))
{
if (rentprint == 'Y' || (rentprint == 'y'))
rate = PIIMP;
else
rate = PIIMON;
}
else
{
(rentmon == 'N' || (rentmon == 'n'));
if (rentprint == 'Y' || (rentprint == 'y'))
rate = PIIPRIN;
else
rate = PII;
}

}
else
{
printf("\nInvalid data entered\n");
}
}
}

}
else
{
printf("\nInvalid data entered\n");
}
}
else
{
printf("\nInvalid data entered\n");
}


printf("\nNumber of days the equipment is rented for? : ");
scanf("%d", &rentdays);

//Calculate
totamnt = rate * rentdays;

printf("\nDaily Rental Charge $%.2lf", rate);
printf("\nNumber of Dyas %.d", rentdays);
printf("\nTotal Amount $%.2lf\n", totamnt);
}
[\code]

Well now everything looks ok, but when I input a bad char in the first prompt. What kind of computer rented? if I input a J it says
Invalid data entered
Number of days the equipment is rented for?

It should stop after invalid data entered. Did I put a } in the wrong place?

[code]
}
else
{
printf("\nInvalid data entered\n");
}


printf("\nNumber of days the equipment is rented for? : ");
scanf("%d", &rentdays);

galathaea
February 10th, 2003, 11:37 AM
Since I would strongly suggest you do not nest anymore ifs, I would say just use a boolean to check if valid input has occurred.

// First set boolean in initialization to true somewhere at head of main
bool validResult = true;

// Then on each invalid else put false
printf("\nInvalid data entered\n");
validResult = false;

// Finally, check each subsequent step for validity
if (validResult)
{
printf("\nNumber of days the equipment is rented for? : ");
scanf("%d", &rentdays);

//Calculate
totamnt = rate * rentdays;

printf("\nDaily Rental Charge $%.2lf", rate);
printf("\nNumber of Dyas %.d", rentdays);
printf("\nTotal Amount $%.2lf\n", totamnt);

}

However, I personally think that some of the nested if blocks can be unloaded to functions that would help increase the readability of the code. Thats purely a style note, however.

codenewb
February 10th, 2003, 12:03 PM
Its a basic logic class, so we have not covered those C++ specifiers, but that worked.

Thanks

Paul McKenzie
February 10th, 2003, 12:27 PM
Originally posted by codenewb
Its a basic logic class, so we have not covered those C++ specifiers, but that worked.

Thanks The logic is still there, it hasn't gone anywhere. Galathaea's code is better structured -- that's the only difference.

Regards,

Paul McKenzie

codenewb
February 10th, 2003, 12:37 PM
Well thanks again for all your help. I appreciate it, for my next assignment I have to insert a while loop. So I will probably have more questions :)

Kind Regards