Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
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.
 
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.

Here's an approach to the problem based on your code that doesn't calculate for invalid data and doesn't require a break either. (Ignore the if/then/else -> switch/case/break construct if it's not your cup of tea).

Nothing in this is beyond where cybrscot is in his book.

Code:
#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 ;
	
}

I like trying to encourage good scalable behavior. Even though in this case doing the calculation for invalid data doesn't hurt anything that is not always the case, so why not just approach the problem in the most general way?

Just for fun, try it with the "value = 0;" commented out and enter text instead of a number.

NOTE: The range construct I use for the switch statement scales very easily to using arrays for the commission slope and intercept which would turn the switch/case/break into a single formula once the arrays are populated.
Code:
commission = intercept[range] + (slope[range] * value)

B
 
Just for fun, try it with the "value = 0;" commented out and enter text instead of a number.

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.

range = (value >= 2500.00) + (value >= 6250.00) + (value >= 20000.00) + (value >= 50000.00);

Clever, but I don't like this because it relies on the documented but funky behavior of C returning "1" for true.
 
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.

Fully agreed. My point is: use the tools you have, but still try to handle all cases gracefully. Anticipate people (including yourself) misusing your program up front, rather than deal with the unintended consequences later.

The use of fgets/sscanf is one way to improve the robustness of the input routine and allow for better up front data validation before you try to process invalid data.

Clever, but I don't like this because it relies on the documented but funky behavior of C returning "1" for true.

As I said, ignore it and use the original if/else if/else if you prefer. You can use the ternary operator around the conditionals if it is more comfortable as was suggested in an earlier thread.

Code:
((value >= 2500.00)?1:0)

However, some approach that takes the continuous range and turns it into integers corresponding to the ranges makes for an easy transition to using arrays.

B
 
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."
 
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."

First, don't worry too much about it. You aren't in trouble, I'd think of it as a warning.

I would suggest that if after you make a post (reply or otherwise), if you see something new you also wish to reply to, do so as an edit to your previous post. It can be a bit of a pain, but you can hit reply, copy the contents to get the quote in there properly, then go to your most recent post, choose edit, and paste the new quote you want to reply to and write up your response. It seems a little laborious, but with multiple tabs it's a few clicks and a few keyboard shortcuts.

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. Another issue is that you're probably 6 or 7 hours off western Europe and likely 12 or 13 off many places in the US. This means when you post and when we post might be very different.

Anyway, multiquote all at once when you can, and do it manually with an edit otherwise. I don't know if exceptions are made, but I would say if many hours go by and there's totally new information a subsequent post that will indicate from the main forum page that there's new content in the thread isn't so bad. The mods may disagree and think of this as bumping, so it may be safer to always edit.

-Lee
 
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.

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
 
Last edited:
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!

Looks like a do loop is the best option, agreed?


Code:
#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 ;
	
}

Edit:
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

Yeah, I was just messing with Text Wrangler and I didn't see that option anywhere either. That would be very nice.

Re, posts. It's funny that I'd say something in jest, like a sarcastic remark about getting an avatar and somebody complained? Please!! Like I'm on here till 5am for that! After a long night, and a lot of frustration, it was just some humor. haha funny, funny. Reading my posts would make it clear to anybody that I'm posting serious material and trying to accomplish something.

Anyway, let me know if you find out anything about using a feature with TextWrangler that can help indent.

I can' t believe that while I'm replying to you, I can't submit because nobody has posted after my last post yet. So I would be consecutive posting again! So I've got to copy and paste this into my last post. What a ridiculous, laborious, thing to have to do. IMO
 
Last edited:
A glimpse of your future

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.
 
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.

Ha, this definitely made me chuckle. When it comes to programming, never assume anything! :)
 
When it comes to programming, never assume anything! :)
The only thing you should assume is that carbon based life forms will abuse your code. So, plan accordingly.

I can't count the number of times I've managed to break my own code inadvertently.

B
 
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
 
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

Hah, I remember doing similar things on my calculator when I played games many years ago :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.