Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
I'm supposed to add a loop to a previous program written in Chapter 4. It sounds easy enough, I gave it a go, tried a few different things, and I can't get it to work either.

By adding the loop, I basically want the program to run exactly as before, only rather than terminate after output, it should again prompt the user for another trade, etc, etc, loop, loop.. until the user enters 0 to terminate.

I added a printf line to instruct the user to enter 0 to terminate, then I added a do loop which engulfs the printf, scanf, and the if, else if, else statments. I did this because I want everything to be done just as before, and continue looping until the user enters 0. Instead only the printf and scanf keep looping, never calculating a commission. But all the if, else if calculation code is included in the loop with brackets as part of the do/statement/while/expression. My while is simple while (value != 0 ).

Why doesn't this work? It seems easy.


Code:
/*calculates a brokers commission*/
main ()
{

	float commission, value ;
	
	printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
	
	
	do {
	printf ("Enter value of trade: ") ;
	scanf ("%f", &value) ;
	 
	
	if (value < 2500.00)
		commission = 30.00 + .017 * value ;
	else if (value < 6250.00)
		commission = 56.00 + .0066 * value ;
	else if (value < 20000.00)
		commission = 76.00 + .0034 * value ;
	else if (value < 50000.00)
		commission = 155.00 + .0011 * value ;
	else
		commission = 255.00 + .0009 * value ;
		
	if (commission < 39.00)
		commission = 39.00;
		
	}
	while (value != 0 ) ;
		
	printf ("Commission: $%.2f\n", commission) ;
	
	return 0 ;
	
}

Also tried this, still doesn't calculate commissions.




Code:
main ()
{

	float commission, value ;
	
	printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
	
	
	do {
	printf ("Enter value of trade: ") ;
	scanf ("%f", &value) ;
	 
	 if (value == 0)  //added this line, not in first version
	 	printf ("Thank You, this session has been terminated\n") ;
	
	if (value < 2500.00)
		commission = 30.00 + .017 * value ;
	else if (value < 6250.00)
		commission = 56.00 + .0066 * value ;
	else if (value < 20000.00)
		commission = 76.00 + .0034 * value ;
	else if (value < 50000.00)
		commission = 155.00 + .0011 * value ;
	else
		commission = 255.00 + .0009 * value ;
		
	if (commission < 39.00)
		commission = 39.00;
		
	}
	while (value > 0 ) ;  //changed this from != to >
		
	printf ("Commission: $%.2f\n", commission) ;
	
	return 0 ;
	
}
 
Last edited by a moderator:
Is the part that prints the commission inside the loop or outside the loop?

If it's outside the loop, when does it execute: while the loop is looping, or after the loop ends?

What should you change so the printing of the commission occurs every time the loop goes around?


Code:
Instead only the printf and scanf keep looping, never calculating a commission.
This is wrong. The commission is calculated every time the loop goes around. You can see this by looking at the code for calculating a commission, which is inside the loop.

Calculating a commission is not the same thing as printing what was calculated.
 
Is the part that prints the commission inside the loop or outside the loop?

If it's outside the loop, when does it execute: while the loop is looping, or after the loop ends?

What should you change so the printing of the commission occurs every time the loop goes around?


Code:
Instead only the printf and scanf keep looping, never calculating a commission.
This is wrong. The commission is calculated every time the loop goes around. You can see this by looking at the code for calculating a commission, which is inside the loop.

Calculating a commission is not the same thing as printing what was calculated.

Oh, so you're saying that the commission structure "is inside" the loop as I had thought, it "is" calculating, but not printing. So I need to include the printf inside the loop as well so the user sees the output, then the "thank you have a nice day" is outside the loop after the user inputs 0. Am I correct?

This works! Yesss! Thanks for the direction!


Code:
//this update adds a loop to the program from chapter 4



#include <stdio.h>

/*calculates a brokers commission*/
main ()
{

	float commission, value ;
	
	printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
	
	
	do {
	printf ("Enter value of trade: ") ;
	scanf ("%f", &value) ;
	
	
	 
	
	if (value < 2500.00)
		commission = 30.00 + .017 * value ;
	else if (value < 6250.00)
		commission = 56.00 + .0066 * value ;
	else if (value < 20000.00)
		commission = 76.00 + .0034 * value ;
	else if (value < 50000.00)
		commission = 155.00 + .0011 * value ;
	else
		commission = 255.00 + .0009 * value ;
		
	if (commission < 39.00)
		commission = 39.00;
		
		printf ("Commission: $%.2f\n", commission) ;
		
	}
	while (value > 0) ;
		if (value == 0)
	 	printf ("Thank You, this session has been terminated\n") ;
	
		
	
	
	return 0 ;
	
}
 
Last edited by a moderator:
Oh, so you're saying that the commission structure "is inside" the loop as I had thought, it "is" calculating, but not printing. So I need to include the printf inside the loop as well so the user sees the output, then the "thank you have a nice day" is outside the loop after the user inputs 0. Am I correct?

Try it. See what happens.

You should be able to do this analysis and test yourself.
 
cybrscot, I think it is time you learned how to format a program! If you use indentation to differentiate the levels of program structure issues such as this one are easier to see.

Here is your original, properly formatted:

Code:
/*calculates a brokers commission*/
main ()
{

    float commission, value ;

    printf ("Calculates the broker's commission, enter 0 to end program.\n") ;


    do {
        printf ("Enter value of trade: ") ;
        scanf ("%f", &value) ;


        if (value < 2500.00)
            commission = 30.00 + .017 * value ;
        else if (value < 6250.00)
            commission = 56.00 + .0066 * value ;
        else if (value < 20000.00)
            commission = 76.00 + .0034 * value ;
        else if (value < 50000.00)
            commission = 155.00 + .0011 * value ;
        else
            commission = 255.00 + .0009 * value ;

        if (commission < 39.00)
            commission = 39.00;

    }
    while (value != 0 ) ;

    printf ("Commission: $%.2f\n", commission) ;

    return 0 ;

}

and here is your fixed program:

Code:
//this update adds a loop to the program from chapter 4

#include <stdio.h>

/*calculates a brokers commission*/
main ()
{

    float commission, value ;

    printf ("Calculates the broker's commission, enter 0 to end program.\n") ;

    do {
        printf ("Enter value of trade: ") ;
        scanf ("%f", &value) ;

        if (value < 2500.00)
            commission = 30.00 + .017 * value ;
        else if (value < 6250.00)
            commission = 56.00 + .0066 * value ;
        else if (value < 20000.00)
            commission = 76.00 + .0034 * value ;
        else if (value < 50000.00)
            commission = 155.00 + .0011 * value ;
        else
            commission = 255.00 + .0009 * value ;

        if (commission < 39.00)
            commission = 39.00;

        printf ("Commission: $%.2f\n", commission) ;

    }
    while (value > 0) ;
    
    if (value == 0)
        printf ("Thank You, this session has been terminated\n") ;

    return 0 ;

}

It makes it easy to see that the printing of the commission is in the wrong place.

It also makes it stand out that even for a 0 input a commission will be printed, and for a negative input the program will exit without thanking the user.
 
Okay, this is when I get most frustrated. I completed the program successfully with one caveat. I have now spent more time trying to address a simple little bug, than I did to get the program to work correctly thanks to chown 33's direction.

The problem....when the user enters 0 to terminate, it does terminate, and prints the "goodbye, have a nice day" line, but also prints a commission of 39.00. I hope I can remember everything I tried as I will comment it and you can see it in the code, but there is certainly a way to fix that, and I have tried probably about 7-8 different things, and could I have at some point at least stumbled on the correct fix? No! Of course not! That would be too easy. Like Edison, I have found 7-8 ways not to fix it, but haven't yet stumbled on the one way to fix it. Unbelievable, just a little luck sometimes would be nice!!:mad::mad:

Clearly I can see that if the value is < 2500, the program will charge a minimum commission of 39.00. There's also a line that says if the commission is < 39.00, then the commission is 39.00. So I can see where both of these would be a problem when a 0 is entered. I have tried combinations of entering !=0, && !=0, also commenting out the part of if the commission < 39.00 then it's 39.00, so as to only charge the exact calculated commission and not a 39.00 minimum, none of these worked. I've also included lines like if (value = 0) then commission = 0, really can't remember everything, but you get the idea, I've thrown everything I know at it. To no avail! I also tried a switch for a value of 0, but cannot because I'm using float variables and not int variables.


Code:
//this update adds a loop to the program from chapter 4



#include <stdio.h>

/*calculates a brokers commission*/
main ()
{

	float commission, value ;
	
	printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
	
	
	do {
	printf ("Enter value of trade: ") ;
	scanf ("%f", &value) ;
	
        //if (value = 0) 
               // printf ("Thank you have a nice day"); again hoping to avoid the
               //part of the calculating program below.
            

	
	//if (value = 0) break ; tried this to break out and have 0 avoid the 
//commission structure part of the program
	
	if (value < 2500.00) //also tried if (value < 2500.00 && value !=0)
//and if (value < 2500.00) && (value != 0)
		commission = 30.00 + .017 * value ;
	else if (value < 6250.00) //also amended each of these to include != 0
     //because when I did it for the 2500, it kicks it up to the next commission
    //level, I do understand why it did that, so I changed all to != 0, but 
//because of the stmt below "comm < 39.00 is 39.00", it still charged 39.00
//then I commented out that part of the pgm and it still didn't work
		commission = 56.00 + .0066 * value ;
	else if (value < 20000.00)
		commission = 76.00 + .0034 * value ;
	else if (value < 50000.00)
		commission = 155.00 + .0011 * value ;
	else if (value > 49999.99)
		commission = 255.00 + .0009 * value ;
	else	
		commission = 0 ;
		
	//if (commission < 39.00)   this is in original pgm, but thought I'd 
//comment it out to get rid of the obvious violator, the flat rate. Didn't work
//still charged the commission for the above <2500, also changed that line
//to account for 0
	//	commission = 39.00;
		
		
		printf ("Commission: $%.2f\n", commission) ;
		
	}
	while (value > 0) ;
		if (value == 0)
	 	printf ("Thank You, this session has been terminated\n") ;
	
		
	
	
	return 0 ;

Now, I've also just experimented with this, still doesn't work!! How can it possibly keep printing commission? I've got it set to value = 0, commission is 0. I've made each part of the if, else, read as >0, and I've commented out the minimum commission of 39.00 portion!! What the heck!!


Code:
//if (value = 0) break ;
	
	if (0 < value < 2500.00) 
		commission = 30.00 + .017 * value ;
	else if (2500.00 < value < 6250.00)
		commission = 56.00 + .0066 * value ;
	else if (6250.00 < value < 20000.00)
		commission = 76.00 + .0034 * value ;
	else if (20000.00 < value < 50000.00)
		commission = 155.00 + .0011 * value ;
	else if (value > 49999.99)
		commission = 255.00 + .0009 * value ;
	else	
		commission = 0 ;
		
	//if (commission < 39.00)
	//	commission = 39.00;
		
		
		printf ("Commission: $%.2f\n", commission) ;
		
	}
	while (value > 0) ;
		if (value == 0)
	 	printf ("Thank You, this session has been terminated\n") ;
 
Code:
if (0 < value < 2500.00)

You can't do that. You need do do something more like

Code:
if ((0 < value) && (value < 2500.00))

EDIT:
Code:
if (0 < value < 2500.00)
turns into
Code:
if ((0 < value) < 2500.00)

So for value=2501 this will reduce to (1 < 2500.00) and will still be true (i.e. 1).

B
 
(0 < value < 2500.00) doesn't do what you think it does. You would need to have (0 < value && value < 2500.0)

Also, back to your original "fixed" program. You can avoid printing the commission using an if statement in the same way you only print the thanks message when the value is zero.
 
cybrscot, I think it is time you learned how to format a program! If you use indentation to differentiate the levels of program structure issues such as this one are easier to see.
.


I'm doing the best I can. I do indent as you can see. But as I add more lines of code, I'm not really sure what/which/ and how it should be indented relative to the rest of it. In the past, when I had code that even to me looked really horrible, I asked for help with the layout. But it was obvious to me the program looked messy. This current one, I don't think looks difficult to read in my "limited experience" opinion. So I wasn't sure and can't be sure if it's formatted correctly(because I don't know what I'm doing). I'm doing all this on my own, have nobody to compare with. You guys are the only ones helping me.
 
You can't do that. You need do do something more like

Code:
if ((0 < value) && (value < 2500.00))

EDIT:
Code:
if (0 < value < 2500.00)
turns into
Code:
if ((0 < value) < 2500.00)

So for value=2501 this will reduce to (1 < 2500.00) and will still be true (i.e. 1).

B

Yeah, I know i've never seen it anywhere like that before, it was just a shot in the dark. The program still compiled, and still printed the correct commissions, just didn't fix my 0 case. I've just exhausted about 15 different possibilities and was just trying stuff. I think trial and error is a practical way to learn, but it just gets too time consuming. I had the original loop working 3 hours ago, and here I am again, 4:21 a.m, to fix what's probably a simple bug. Very frustrating.


Code:
Scott-Deans-MacBook-Pro:documents scottdean$ ./a.out
Calculates the broker's commission, enter 0 to end program.
Enter value of trade: 4500.00
Commission: $106.50
Enter value of trade: 0
Commission: $30.00
Thank You, this session has been terminated
Scott-Deans-MacBook-Pro:documents scottdean$
 
just didn't fix my 0 case.

I think I've already mentioned input validation in some of your other threads.

If you put all of the stuff that shoudl happen for positive value in a
Code:
if (value>0) {
<STUFF THAT SHOULD HAPPEN FOR POSITIVE VALUES>
};
else { 
<PRINT ERROR>
);
you won't have a problem.

Proper code indentation will help you.

B
 
What editor are you using? That's something you can usually get your editor to handle for you. e.g. TextWrangler and Xcode you can Select All and re-indent.

B

I'm using Text Wrangler. I have no idea how to use it's various features because I don't know what they mean/do. I just use it because it was recommended for programming, and it color codes stuff so I can see errors a bit more easliy, etc.

Of course, I took a look at the instructions and the feature sets of wrangler, but keep in mind it was before my first line of code ever, and the help might have said, "if you want to do this, you should do this, and this software can do this, and it's powerful tools can do blah, blah, blah", but I didn't know what any of that terminology meant. Still, I'm only a month and a half in, Chapter 6, and if it hasn't been covered yet, I have no idea what it is. It's all French to me. Other than %g for trailing 0's, which was easy to pick up from you guys.
 
Indentation will make it obvious where the loop begins and ends since everything inside the loop has extra indenting. Compare my earlier reply with your initial program post.

And it certainly helps to use an editor that knows C syntax and will indent for you. Every editor that advertises it's for programming will do that.
 
(0 < value < 2500.00) doesn't do what you think it does. You would need to have (0 < value && value < 2500.0)

Also, back to your original "fixed" program. You can avoid printing the commission using an if statement in the same way you only print the thanks message when the value is zero.

Yeah I can see the problem with precedence (0 < value < 2500.00) now because it reads from left to right. But as you can see in my commented out code, I did try (value > 0 && value < 2500.00), and I also tried, (value > 0) && (value < 2500.00). Neither worked, Unless it was a combination of that and some other changes I made, that together negated each other? I don't know.

So I've got to delete all that crap, get back to my original fixed version, and try another if statement. I will do that. At least now that I know to pursue the "if statement" path, I don't have to experiment with anything else. I can probably try writing the if stmt and get it in the right place in a few attempts I'd imagine.
 
Code:
	float commission, value ;
	
	
	if (value == 0)
	 	printf ("Thank You, this session has been terminated\n") ;

Two comments about these lines of code.

1. Wouldn't you always want to print "Thank You, this session has been terminated" even if the user input -1? If so why the if(value == 0)?

2. In Java (more my area), you would never use the equality operator (==) with a float. You would always use <= or >= since floats are very precise but ultimately an approximation. I'm assuming C is the same.
 
2. In Java (more my area), you would never use the equality operator (==) with a float. You would always use <= or >= since floats are very precise but ultimately an approximation. I'm assuming C is the same.

A bit off topic reply. What you say is true about computational results, but in this case "value" is a user input and zero is very possible. In this specific case, what you say as point 1 is most pertinent!
 
(0 < value < 2500.00) doesn't do what you think it does. You would need to have (0 < value && value < 2500.0)

Also, back to your original "fixed" program. You can avoid printing the commission using an if statement in the same way you only print the thanks message when the value is zero.

Still doesn't work. It does ONLY print the "thank you, this session has been terminated", when the user enters 0, as you suggest, but still prints a commission.

If somebody wouldn't mind, "showing" me with the proper code what to do. I give up.

I appreciate all the help, and little clues here and there. I know you all want me to learn on my own (as do I, trust me) rather than just writing the code or showing me what to do. But hopefully it's obvious by now that I stay up until 5am trying to figure this out on my own, but can't. Maybe we can have a rule that if I'm still trying after 4:30 am, I've earned the right to be "shown" how, rather than another "hint"?

I'm not trying to be a smartass. Maybe a little funny, maybe a little "loopy" (pun intended) at this hour of the morning. So don't take my request the wrong way. I know beggars can't be choosers, and I do appreciate the time that all of you take out of your day to help me. I know you spend more time and energy trying to lead me in the right direction, and it would be easier for you just to write the code, post it, and forget about it. For that I thank you, sincerely. But would you mind showing me, inserting the proper code into my code, and we can call it a night?

Thanks



Code:
Scott-Deans-MacBook-Pro:documents scottdean$ ./a.out
Calculates the broker's commission, enter 0 to end program.
Enter value of trade: 300.00
Commission: $39.00
Enter value of trade: 2200.00
Commission: $67.40
Enter value of trade: 4500.00
Commission: $85.70
Enter value of trade: 14500.00
Commission: $125.30
Enter value of trade: 0
Commission: $39.00
Thank You, this session has been terminated







Code:
#include <stdio.h>

/*calculates a brokers commission*/
main ()
{

	float commission, value ;
	
	printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
	
	
	do {
	printf ("Enter value of trade: ") ;
	scanf ("%f", &value) ;
	
	

	
	if (value < 2500.00) 
		commission = 30.00 + .017 * value ;
	else if (value < 6250.00)
		commission = 56.00 + .0066 * value ;
	else if (value < 20000.00)
		commission = 76.00 + .0034 * value ;
	else if (value < 50000.00)
		commission = 155.00 + .0011 * value ;
	else
		commission = 255.00 + .0009 * value ;

		
	if (commission < 39.00)
		commission = 39.00;
		
		
		printf ("Commission: $%.2f\n", commission) ;
		
	}
	while (value > 0) ;
		if (value == 0)
	 	printf ("Thank You, this session has been terminated\n") ;
	
		
	
	
	return 0 ;
	
}

Two comments about these lines of code.

1. Wouldn't you always want to print "Thank You, this session has been terminated" even if the user input -1? If so why the if(value == 0)?

2. In Java (more my area), you would never use the equality operator (==) with a float. You would always use <= or >= since floats are very precise but ultimately an approximation. I'm assuming C is the same.



I just assumed that nobody should enter a trade in a negative dollar amount. It's not possible. I think if I entered a negative on my brokers website I'd get some screwy result/error.

Yes I can change it to value < 0, no problem. That's the least of my concerns, as I know how to do that. I'm just trying to not get a commission printed when it's 0. And if I can figure that out, and it works, then I can change it to be value < 0, and it will still work. I try to take things one step at a time sometimes when I have a problem I can't figure out. The simplest value is just to keep it at 0 for now, so I have only one possible user input to worry about, and get that working to my satisfaction. Then I can change it to the oft mentioned value < 0, so it includes negative numbers in the error/validation checking.
 
Last edited by a moderator:
Still doesn't work. It does ONLY print the "thank you, this session has been terminated", when the user enters 0, as you suggest, but still prints a commission.

If somebody wouldn't mind, "showing" me with the proper code what to do. I give up.

I appreciate all the help, and little clues here and there. I know you all want me to learn on my own (as do I, trust me) rather than just writing the code or showing me what to do. But hopefully it's obvious by now that I stay up until 5am trying to figure this out on my own, but can't. Maybe we can have a rule that if I'm still trying after 4:30 am, I've earned the right to be "shown" how, rather than another "hint"?

I'm not trying to be a smartass. Maybe a little funny, maybe a little "loopy" (pun intended) at this hour of the morning. So don't take my request the wrong way. I know beggars can't be choosers, and I do appreciate the time that all of you take out of your day to help me. I know you spend more time and energy trying to lead me in the right direction, and it would be easier for you just to write the code, post it, and forget about it. For that I thank you, sincerely. But would you mind showing me, inserting the proper code into my code, and we can call it a night?

Thanks



Code:
Scott-Deans-MacBook-Pro:documents scottdean$ ./a.out
Calculates the broker's commission, enter 0 to end program.
Enter value of trade: 300.00
Commission: $39.00
Enter value of trade: 2200.00
Commission: $67.40
Enter value of trade: 4500.00
Commission: $85.70
Enter value of trade: 14500.00
Commission: $125.30
Enter value of trade: 0
Commission: $39.00
Thank You, this session has been terminated







Code:
#include <stdio.h>

/*calculates a brokers commission*/
main ()
{

	float commission, value ;
	
	printf ("Calculates the broker's commission, enter 0 to end program.\n") ;
	
	
	do {
	printf ("Enter value of trade: ") ;
	scanf ("%f", &value) ;
	
	

	
	if (value < 2500.00) 
		commission = 30.00 + .017 * value ;
	else if (value < 6250.00)
		commission = 56.00 + .0066 * value ;
	else if (value < 20000.00)
		commission = 76.00 + .0034 * value ;
	else if (value < 50000.00)
		commission = 155.00 + .0011 * value ;
	else
		commission = 255.00 + .0009 * value ;

		
	if (commission < 39.00)
		commission = 39.00;
		
		
		printf ("Commission: $%.2f\n", commission) ;
		
	}
	while (value > 0) ;
		if (value == 0)
	 	printf ("Thank You, this session has been terminated\n") ;
	
		
	
	
	return 0 ;
	
}


I think no matter what I do as far as if stmts. If the value is 0, as the program stands, it will fall under the value < 2500, and print a commission of 39.00. Even if I say, if commission = 0, then printf (have a nice day) it still prints 39.00 because the code says that if commission is < 39.00, commission is 39.00. It just goes around and around. I can't get out of it.
 
I'm not trying to be a smartass. Maybe a little funny, maybe a little "loopy" (pun intended) at this hour of the morning. So don't take my request the wrong way. I know beggars can't be choosers, and I do appreciate the time that all of you take out of your day to help me. I know you spend more time and energy trying to lead me in the right direction, and it would be easier for you just to write the code, post it, and forget about it. For that I thank you, sincerely. But would you mind showing me, inserting the proper code into my code, and we can call it a night?

Offhand, I don't like to do it, but here it is with two repairs:

Code:
#include <stdio.h>

/*calculates a brokers commission*/
main ()
{

    float commission, value ;

    printf ("Calculates the broker's commission, enter 0 to end program.\n") ;


    do {
        printf ("Enter value of trade: ") ;
        scanf ("%f", &value) ;


        if (value < 2500.00) 
            commission = 30.00 + .017 * value ;
        else if (value < 6250.00)
            commission = 56.00 + .0066 * value ;
        else if (value < 20000.00)
            commission = 76.00 + .0034 * value ;
        else if (value < 50000.00)
            commission = 155.00 + .0011 * value ;
        else
            commission = 255.00 + .0009 * value ;


        if (commission < 39.00)
            commission = 39.00;


        if (value > 0)
            printf ("Commission: $%.2f\n", commission) ;

    } while (value > 0) ;

    printf ("Thank You, this session has been terminated\n") ;

    return 0 ;

}

Perhaps you didn't understand that when I was referring to "value" I meant the variable "value" not the value of the commission.
 
Instead of trying to write code, sketch out a basic overall plan first.

Example of a sketched out plan:
Loop with the following:
1. Prompt, then input a number representing a trade amount.
2. If the input is 0, break the loop.
3. Calculate the commision.
4. Print the commission.
5. Continue the loop.

After loop ends, say "Thank you".

In the code you keep writing and rewriting, it's like you're wandering around in the forest, and continuing to go in circles, because you don't have an overall plan for how to travel through the forest. The last step for getting out of the forest is to recognize when you're at the edge of the forest, at which time you then exit the forest instead of looping around and trying to reach some other edge of the forest.

Note that step 2 occurs BEFORE calculating or printing a commission. If you want something to happen BEFORE something else, then you need to put those things in the proper sequence. If that seems obvious, then maybe programming at 4 am isn't working out as well as you think it is, and you should change to a different time of day.
 
Add an if statement to print the commission only if value is greater than zero.

Oh my gosh, that was too easy. I can't believe it. I don't know now, whether to be relieved that it's finished and I can finally go to sleep? Or to be mad at myself for not getting that on my own. Really, I couldn't sleep anyway, if I get frustrated like this and deeply into thought, I can' t fall asleep, I'll just lay there thinking about my unfinished business. Now I can rest, because it's done. But I'm very disappointed in myself. You see all the crazy, difficult (or at least more time consuming) stuff I tried? And it was really pretty simple all along! :mad::(:mad::(

Thanks, I guess it's just another learning experience. Hope I remember it. This is hard to learn on my own from a book. Did any of you find programming easy to learn? Any whiz kid hackers out there? Am I maybe just not cut out for this? I hope not. I think my thought processes are usually right about what I want to do. I just don't yet know how to do it.

Thanks everybody! Good night, have a nice evening! I'll wake up at lunch time!

Add an if statement to print the commission only if value is greater than zero.

Yeah, once you told me this, I got it. It was there all along, easy, but I never saw it. I knew exactly what to do once you posted this note. Thanks! I owe you all a beer!

Instead of trying to write code, sketch out a basic overall plan first.

Example of a sketched out plan:
Loop with the following:
1. Prompt, then input a number representing a trade amount.
2. If the input is 0, break the loop.
3. Calculate the commision.
4. Print the commission.
5. Continue the loop.

After loop ends, say "Thank you".

In the code you keep writing and rewriting, it's like you're wandering around in the forest, and continuing to go in circles, because you don't have an overall plan for how to travel through the forest. The last step for getting out of the forest is to recognize when you're at the edge of the forest, at which time you then exit the forest instead of looping around and trying to reach some other edge of the forest.

Note that step 2 occurs BEFORE calculating or printing a commission. If you want something to happen BEFORE something else, then you need to put those things in the proper sequence. If that seems obvious, then maybe programming at 4 am isn't working out as well as you think it is, and you should change to a different time of day.

I have no choice, I must learn when I have time to learn. During the day I have to work and do stuff with my wife, just like everybody else. I wish I learned when I was younger, but I didn't. So now's the time. (They say the best time to plant a tree was 30 years ago, the second best time is now, I'm in the "now" mode) I'm motivated to be successful at anything I do, and I don't quit. When I can't figure something out, I can't stop. Even if I tried, my mind keeps going and going. That's why I finally had to give up and ask for the code, so I could decompress. Definition of insanity: doing the same thing over and over and expecting a different result. That's where I was going with this, insane...kept getting the same result, doing the wrong thing over and over again.

As for my code, and your steps above, it makes perfect sense to exit near the top if the user enters a 0, and thus, "avoid" the commission structure. But in this case we still didn't do that, and it's now working.



Code:
	if (value > 0)	//this is the line that made it finally work and stop
                                //printing commission for 0, this is in the loop and
                               //after the commission block, rather than before it.
                              //is there still another way to exit before the loop if 
                              //the user enters 0??
		printf ("Commission: $%.2f\n", commission) ;
		
	}
	while (value > 0) ;
		if (value == 0)
	 	printf ("Thank You, this session has been terminated\n") ;
 
Last edited by a moderator:
As for my code, and your steps above, it makes perfect sense to exit near the top if the user enters a 0, and thus, "avoid" the commission structure. But in this case we still didn't do that, and it's now working.
You're still calculating the commission even though the value is 0, you're just not printing it. Sometimes not printing it is good enough. Sometimes it isn't.

If you know loops, then you should know about the break statement. You might want to look at it again, and consider what this code would do, and where a sensible place to put it would be:
Code:
 if (value == 0)
    break;
If you're not sure, re-read the overall plan I sketched out.
 
I just assumed that nobody should enter a trade in a negative dollar amount. It's not possible.

Never assume that about a user. They will always surprise you. (Including when the user is you yourself).

It's good practice to process only valid data. http://en.wikipedia.org/wiki/Data_validation

EDIT:
You're still calculating the commission even though the value is 0, you're just not printing it. Sometimes not printing it is good enough. Sometimes it isn't.

QFT. This is why it's good to enforce the rules. Don't assume, because computers will only do what you tell them to.

B
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.