Simple program 
Author Message
 Simple program

I am new to C.

I have have doing okay to now.
I wrote a program to calcluate commission based on sales.
However, when I run the program it ask me for the sales twice.

Can somebody please help.

Gino Pepe

/*    Eugene Pepe     */
/*    3.19 page 93    */

#include <stdio.h>

int main(void)
{ /*start*/

/*    Declare varibles   */
float commish;
int sales;

/*    Processing Phase   */
 printf("Enter sales in dollars, -1 to end: ");
 scanf("%d", &sales);

while (sales != -1){
 printf("Enter sales in dollars, -1 to end: ");
 scanf("%d", &sales);
 commish = sales*.09;
 printf("Commishion is %.2f\n", commish);
 }
if (sales == -1){
 printf("END");
 return -1;
 }

/*    Termination phase   */

return 0;

Quote:
}

--



Sun, 24 Mar 2002 03:00:00 GMT  
 Simple program


Quote:
>I am new to C.

>I have have doing okay to now.
>I wrote a program to calcluate commission based on sales.
>However, when I run the program it ask me for the sales twice.

>Can somebody please help.

>Gino Pepe

>/*    Eugene Pepe     */
>/*    3.19 page 93    */

>#include <stdio.h>

>int main(void)
>{ /*start*/

>/*    Declare varibles   */
>float commish;
>int sales;

>/*    Processing Phase   */
> printf("Enter sales in dollars, -1 to end: ");
> scanf("%d", &sales);

But you ask for it here outside your loop.
Quote:

>while (sales != -1){
> printf("Enter sales in dollars, -1 to end: ");
> scanf("%d", &sales);

and again here, so naturally the first time it will ask twice.  Consider
re-ordering your loop.  There are also other alternatives such as using
do {    } while(true); and using an if(sales == -1) break; to test and
exit from inside the loop.

Quote:
> commish = sales*.09;
> printf("Commishion is %.2f\n", commish);
> }
>if (sales == -1){
> printf("END");
> return -1;

But with the logic of your program sales will always be -1 at this
point.

Quote:
> }

>/*    Termination phase   */

>return 0;

and so it will never reach the above line.

Quote:
>}

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Mon, 25 Mar 2002 03:00:00 GMT  
 Simple program

Quote:

> /*    Processing Phase   */
>  printf("Enter sales in dollars, -1 to end: ");
>  scanf("%d", &sales);

This block of code above here is redundant with what you have below. I
would just delete this part and keep what you have in the while loop.

Quote:

> while (sales != -1){
>  printf("Enter sales in dollars, -1 to end: ");
>  scanf("%d", &sales);

....

--
--------------------------
Cody Caughlan

--



Mon, 25 Mar 2002 03:00:00 GMT  
 Simple program

Quote:

> This block of code above here is redundant with what you have below. I
> would just delete this part and keep what you have in the while loop.

In that case, you'd also have to initialize `sales' so that it doesn't
start up as -1 by accident.
--



Tue, 26 Mar 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. C++ .Net error on simple program PLEASE HELP!!

2. Simple program fails w/ debugger but not cmd line

3. simple program

4. Simple Program

5. a simple program

6. Help !! (urgent) a simple program

7. calculating "e", simple program.

8. Please help me with this simple program.....

9. Simple programs, Help!!

10. Problems with scanf() in simple program

11. Problem with a simple program

12. Problem with simple program.

 

 
Powered by phpBB® Forum Software