It doesn't hurt at all to do the calculation here since it is valid for a zero amount. An exit in the middle loop (via break) is obviously the correct choice, however it does complicate matters for a newbee because the condition at the end is now always true.
#include <stdio.h>
/*calculates a brokers commission*/
int main (void)
{
float commission, value ;
int done = 0, range;
printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
while (!done) {
//initialize input so that scanf handles errors better.
value = 0;
printf ("Enter value of trade: ") ;
scanf ("%f", &value) ;
//Valid input
if (value > 0){
range = (value >= 2500.00) + (value >= 6250.00) + (value >= 20000.00) + (value >= 50000.00);
switch (range) {
case 0:
commission = 30.00 + (0.017 * value) ;
break;
case 1:
commission = 56.00 + (0.0066 * value) ;
break;
case 2:
commission = 76.00 + (0.0034 * value) ;
break;
case 3:
commission = 155.00 + (0.0011 * value) ;
break;
case 4:
commission = 255.00 + (0.0009 * value) ;
break;
default: //range should always be 0-4, but just in case...
commission = 39.00;
}
//Override for minimum commission
if (commission < 39.00)
commission = 39.00;
printf ("Commission: $%.2f\n", commission) ;
}
// Exit condition
else if (value == 0)
done = 1;
//unrecognized input
else
printf ("ERROR: Unrecognized input, please enter a positive number or 0 to exit\n");
};
printf ("Thank You, this session has been terminated\n") ;
return 0 ;
}
commission = intercept[range] + (slope[range] * value)
Just for fun, try it with the "value = 0;" commented out and enter text instead of a number.
range = (value >= 2500.00) + (value >= 6250.00) + (value >= 20000.00) + (value >= 50000.00);
Good idea there. But I personally don't like scanf and used to teach the use of fgets followed by sscanf to handle garbage input. A bit beyond what the OP has learned so far.
Clever, but I don't like this because it relies on the documented but funky behavior of C returning "1" for true.
((value >= 2500.00)?1:0)
Guys, somebody doesn't want me around. I got an "infraction" for writing too many "consecutive" posts. What is this about? I followed the posts they provided for the "infraction" and it was just me replying to different people that were posting stuff to help me. As you all know, I'm sitting here working on my computer, so I post replies pretty quickly (right away) to your ideas. I got in trouble for that. They said to use a multi quote feature. But to do this, I would have to wait like an hour or so, (it was already between 4am and 5am as it was) and reply to a bunch of messages at once. Why is this an infraction? I'm not an idiot. I'm using the forum to try to learn something.
If they read my messages/posts, they would easily see that my messages are serious and I'm working on something important. Not just chatting on here for the sake of chatting. I can't believe now I'm considered a "trouble maker."
I don't know if the infraction was auto-generated or issued by hand, but going strictly by the letter of the law subsequent posts are a no-no.
#include <stdio.h>
main ()
{
int userNumber ;
do {
printf ("Enter a number 1-5 to see what this program can't believe\n") ;
printf ("I can't believe....");
scanf ("%d", &userNumber) ;
switch (userNumber) {
case 1 : printf ("I got an infraction\n") ; break ;
case 2 : printf ("Somebody reported my posts as continuous\n"); break ;
case 3 : printf ("it because I only want honest help and feedback\n"); break ;
case 4 : printf ("it could not have been mentioned as a courtesy and out of respect\n"); break ;
case 5 : printf ("it and I just wanted to say thank you for my demerit\n") ; break ;
}
} while ((userNumber > 0) && (userNumber < 6)) ;
printf ("Your session has been terminated\n") ;
return 0 ;
}
The law is a bit fuzzy on the details. See this recent thread on bumping for a great explanation by Doctor Q, https://forums.macrumors.com/threads/981449/ (post #10).
Post count is an measure of activity on the board and if a user has many posts that could have been combined as one by the methods lee suggests, this distorts the statistics.
Simple thread bumping is against the rules as is artificially boosting your post count. I think the main thing that set off a flag to generate the yellow card was that cybrscot said something to the effect of "at this rate I'll get an avatar soon".
EDIT: Looks like TextWrangler doesn't have a re-indent function of it's own. I've been flitting between TW and Xcode so I missed that. Apparently though there is a plugin for astyle. I'm going to see if I can get that running.
B
I just assumed that nobody should enter a trade in a negative dollar amount.
If you continue to think this way, at some point in your career (if you end up a a programmer) there will be an all day, all company meeting featuring you being screamed at by everyone from your boss all the way to the president of the company.
The only thing you should assume is that carbon based life forms will abuse your code. So, plan accordingly.When it comes to programming, never assume anything! 🙂
Hey, I whipped out this quick little program with a do loop, on my own in about 20 minutes. I think I'm getting better. It's amazing what a motivated individual can achieve on his own sometimes!
For a little humor, run your program and enter "2 x" as your response (don't type the quote symbols, just 2 space x).
In drug wars on my TI-8[23] calculator, the programmer didn't sanitize inputs, so you could say you wanted to buy -1000 units of heroin, and you'd end up having negative units in your inventory, but also a few million dollars. From then on you could buy a ton of any other products, and make tons of money.
It was cheating, but the game let you do it and it shouldn't have.
-Lee