CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2003
    Location
    TX
    Posts
    43

    Angry nested if headache

    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?

    Code:
    #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
    Code:
    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'))

  2. #2
    Join Date
    Aug 2002
    Location
    VA, USA
    Posts
    137
    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

  3. #3
    Join Date
    Jan 2003
    Location
    TX
    Posts
    43

    duh

    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

  4. #4
    Join Date
    Jan 2003
    Location
    TX
    Posts
    43

    another problem 'sigh'

    [code]
    #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);

  5. #5
    Join Date
    Sep 2002
    Posts
    1,747
    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.
    Code:
    // 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.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  6. #6
    Join Date
    Jan 2003
    Location
    TX
    Posts
    43

    thx

    Its a basic logic class, so we have not covered those C++ specifiers, but that worked.

    Thanks

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: thx

    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

  8. #8
    Join Date
    Jan 2003
    Location
    TX
    Posts
    43
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured