Click to See Complete Forum and Search --> : if confusion?
codenewb
February 6th, 2003, 10:47 AM
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.
printf("\nEnter number of days equipment rented : ");
scanf("%d", &rentdays);
totamnt = rate * rentdays;
printf("\nTotal Amount $%.2lf\n", totamnt);
}
Paul McKenzie
February 6th, 2003, 11:00 AM
Think of the "braces" logically. You don't need to be a programmer to figure out why the braces are needed. For example:
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?
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
codenewb
February 6th, 2003, 11:15 AM
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
codenewb
February 6th, 2003, 11:18 AM
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?
codenewb
February 6th, 2003, 11:25 AM
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:
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?
Paul McKenzie
February 6th, 2003, 02:03 PM
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.
printf("\nEnter number of days equipment rented : ");
scanf("%d", &rentdays);
totamnt = rate * rentdays;
printf("\nTotal Amount $%.2lf\n", totamnt);
}
Gabriel Fleseriu
February 6th, 2003, 02:17 PM
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:
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:
// is NOT the same as
if(condition)
do_something();
do_something_else();
Just a thought, :)
Paul McKenzie
February 6th, 2003, 02:33 PM
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.
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
codenewb
February 6th, 2003, 02:50 PM
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
filthy_mcnasty
February 6th, 2003, 04:03 PM
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.
codenewb
February 6th, 2003, 04:25 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.