CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: if confusion?

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

    Angry if confusion?

    I'm trying to write yet another basic program for my logic class, I seem to be stuck on my if statements. I know I don't have the brackets ({, endifs) in place but thats where I am confused on where to put them, or is it a problem with my logic?

    I am also supposed to throw a do while loop into the beggining as to process more than one customer.

    Any help would be greatly appreciated.
    Thanks


    #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=PentiumII) : ");
    scanf("%c", &comptype);

    if (comptype == 'S' || (comptype == 's'))
    printf("\nWas a monitor rented? : ");
    scanf("%c", &rentmon);
    if (rentmon == 'Y' || (rentmon == 'y'))
    printf("\nWas a printer rented? : ");
    scanf("%c", &rentprint);
    if (rentprint == 'Y' || (rentprint == 'y'))
    rate = PMP;
    else
    rate = PMON;
    else
    printf("\nWas a printer rented? : ");
    scanf("%c", &rentprint);
    if (rentprint == 'Y' || (rentprint == 'y'))
    rate = PPRIN;
    else
    rate = P;

    else

    printf("\nWas a monitor rented? : ");
    scanf("%c", &rentmon);
    if (rentmon == 'Y' || (rentmon == 'y'))
    printf("\nWas a printer rented? : ");
    scanf("%c", &rentprint);
    if (rentprint == 'Y' || (rentprint == 'y'))
    rate = PIIMP;
    else
    rate = PIIMON;

    else
    printf("\nWas a printer rented? : ");
    scanf("%c", &rentprint);
    if (rentprint == 'Y' || (rentprint == 'y'))
    rate = PIIPRIN;
    else
    rate = PII;






    printf("\nEnter number of days equipment rented : ");
    scanf("%d", &rentdays);

    totamnt = rate * rentdays;

    printf("\nTotal Amount $%.2lf\n", totamnt);

    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    Think of the "braces" logically. You don't need to be a programmer to figure out why the braces are needed. For example:
    Code:
    if I want to buy something at the store then
    begin
         Put on my clothes
         Leave the house
         Start the car
         drive to store
         buy bread
         go back in car
         drive home
    end
    The above describes the steps if you want to go to the store. Note the begin / end. They denote the steps required to buy something from the store. What would happen if I placed the begin / end in the wrong places?

    Code:
    if I want to buy something at the store then
    begin
        Put on my clothes
        Leave the house
        Start the car
        drive to store
    end
    
    buy bread     
    go back in car
    drive home.
    Do you see the problem here? The problem is that if you don't want to buy something at the store, you still buy bread, go back in your car, and drive home. What if you aren't in the store -- how do you "buy bread?"

    The braces, begin / endif make perfect sense when you think of if() statements in real-world terms. (Also, this is a C++ forum, so there is no "endif". I just tried to illustrate how to think of if blocks logically).

    Regards,

    Paul McKenzie

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

    if confusion

    Ok I understand what you mean about the way it should flow logically. Being that if you put and end after drive to the store, you can't continue on doing

    buy bread
    go back in car
    drive home.

    As far as the start and end of an if statement what exactly do the braces encompass?

    if whatever

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

    if confusion

    Ok I understand what you mean about the way it should flow logically. Being that if you put and end after drive to the store, you can't continue on doing

    buy bread
    go back in car
    drive home.

    As far as the start and end of an if statement what exactly do the braces encompass? Like in my code is it done correctly with the nesting like:

    if
    if
    else
    else
    if
    else

    Or if I am understanding your point the else would have to go below the last else like:

    if
    if
    else

    if
    else
    else?

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

    code

    Sorry about the format of the previous post I didn't know there was a code command. Hope this example will help a bit more:

    Code:
           if (comptype == 'S' || (comptype == 's'))
    		printf("\nWas a monitor rented? : ");
    		scanf("%c", &rentmon);
    		if (rentmon == 'Y' || (rentmon == 'y'))
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PMP;
    			else 
    				rate = PMON;
    		else
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PPRIN;
    			else 
    				rate = P;
    		
    	
    	else
    		printf("\nWas a monitor rented? : ");
    		scanf("%c", &rentmon);
    		if (rentmon == 'Y' || (rentmon == 'y'))
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PIIMP;
    			else
    				rate = PIIMON;
    		
    		else
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PIIPRIN;
    			else
    				rate = PII;

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

    compiled

    Ok here is what I have so far:

    Code:
           if (comptype == 'S' || (comptype == 's'))
    	{
    		printf("\nWas a monitor rented? : ");
    		scanf("%c", &rentmon);
    		if (rentmon == 'Y' || (rentmon == 'y'))
    		{
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PMP;
    			else 
    				rate = PMON;
    		}
    		else
    		{
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PPRIN;
    			else 
    				rate = P;
    		}
    	}
    	else
    	{
    		printf("\nWas a monitor rented? : ");
    		scanf("%c", &rentmon);
    		if (rentmon == 'Y' || (rentmon == 'y'))
    		{
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PIIMP;
    			else
    				rate = PIIMON;
    		}
    		else
    		{
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PIIPRIN;
    			else
    				rate = PII;
    		}
    	}
    I am able to compile and run the program now but it seems to what to skip part of the input needed.

    It asks what kind of computer:
    Enter type of computer rented (S=Pentium or P=PentiumII):

    that if seems ok because it will accept both S or P
    but then it just skips to

    Was a monitor rented? :
    Was a printer rented?: n

    I can put input in for the 2nd statement but it seems to skip the 1st printf completely, I don't seem to know what the problem is?

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    that if seems ok because it will accept both S or P
    but then it just skips to...
    First, we need to know what type of variable is "comptype" and how it got its value.

    Regards,

    Paul McKenzie

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

    compiled

    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
    
    void main()
    {
    	char comptype, rentmon, rentprint;
    	int  rentdays;
    	double totamnt, rate;
    
    	printf("\nEnter type of computer rented (S=Pentium or P=PentiumII) : ");
    	scanf("%c", &comptype);
    	
    	if (comptype == 'S' || (comptype == 's'))
    	{
    		printf("\nWas a monitor rented? : ");
    		scanf("%c", &rentmon);
    		if (rentmon == 'Y' || (rentmon == 'y'))
    		{
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PMP;
    			else 
    				rate = PMON;
    		}
    		else
    		{
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PPRIN;
    			else 
    				rate = P;
    		}
    	}
    	else
    	{
    		printf("\nWas a monitor rented? : ");
    		scanf("%c", &rentmon);
    		if (rentmon == 'Y' || (rentmon == 'y'))
    		{
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PIIMP;
    			else
    				rate = PIIMON;
    		}
    		else
    		{
    			printf("\nWas a printer rented? : ");
    			scanf("%c", &rentprint);
    			if (rentprint == 'Y' || (rentprint == 'y'))
    				rate = PIIPRIN;
    			else
    				rate = PII;
    		}
    	}
    
    	
    	
    		
    	
    		
    	
    
    	printf("\nEnter number of days equipment rented : ");
    	scanf("%d", &rentdays);
    
    	totamnt = rate * rentdays;
    
    	printf("\nTotal Amount $%.2lf\n", totamnt);
    	
    }

  9. #9
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    As a side note on the if statement: I found it a good practice to use { } for if/else statements, even if there is only one statement to be executed. Example:
    Code:
    if(condition){
        do_something();
    }
    There are two reasons to do this: the first one is that I find the code to be more readable this way -- but one could argue about this. The second reason is, that if you decide later do add a second statement to your if, you don't introduce subtle bugs into your code:
    Code:
    if(condition){
        do_something();
        do_something_else();
    }
    
    // is NOT the same as
    if(condition)
        do_something();
        do_something_else();
    Just a thought,
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

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

    Re: compiled

    The reason why your input "jumps" over the scanf() is that you are inputting a single character followed by a carriage return.

    The carriage return is then used in the next scanf(). The format statement for the scanf is "%c" which means input a single character -- you're inputting two characters.

    I have not used scanf() in ages, but you have to tell scanf() to not process the carriage return at the end, or add another scanf character to eat the carriage return.
    Code:
    scanf("%c%c", &whatever, &eatCRLF);
    You'll quickly find out that scanf() is a piece of crap for getting console input. Instead, you would get character strings and parse out the information.

    Also, if your intention is to learn C++, quit using scanf and use the stream operators.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Jan 2003
    Location
    TX
    Posts
    43
    I see so I could also use getchar() then?
    I was able to compile and run it, and it flows fine. Thank you very much for your help, I was going out of my mind trying to figure out the problem. Since its just a logic class my teacher is cutting our teeth on just C, the next class is oop which exposes us to C++.

    Thanks again
    kind regards

  12. #12
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    i just want to stress what Gabriel said. having braces will NEVER hurt your program but will always help you follow the flow. even if it's one command they will help.

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

    Smile

    Well I will keep that in mind, even though my professor doesn't make it a point to use them with just one statements, but like you I feel they help me keep everything organized.

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