Sales Calculator Problem (Visual C++ express)

Good Day All,

I'm new to the forum and new to coding (period.), and was looking for some help.
This question pops up alot around the web, but because C++ language a bit different from modern, in VS C++ 2008, i'm having a bit of trouble.

Question:

One large chemical company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of chemicals in a week receives $200 plus 9% of $5000, or a total of $650. Develop a program that will input each salesperson's gross sales for last week and will calculate and display that salesperson's earnings. Process one salesperson's figures at a time.

Here is a sample input/output dialog:
Enter sales in dollars (-1 to end): 5000.00
Salary is: $650.00
Enter sales in dollars (-1 to end): 1234.56
Salary is: $311.11
Enter sales in dollars (-1 to end): 1088.89
Salary is: $298.00
Enter sales in dollars (-1 to end): -1

//Sales Calculator Problem //

#include<stdio.h>


int main (void)
{

int base = 200;
double salary;
int week;
float sales;
double commission;
week = 1;



printf ("Enter Sales, -1 to end: \n", sales);
scanf ("%f", &sales);

//begin loop while sentinel value not yet read //
while (sales != -1)
{
commission = 0.09 * sales;
salary = commission + base;
++week;

printf ("Salary is \n", salary);

}


return 0;

} /*end function main */


When I run the program in the win32 console, I get

"Enter Sales, -1 to end:"

1 (input)


(output)
Salary is
Salary is
Salary is

(nonstop repeat, with no calculations performed).


**So it doesn't stop looping, and the calculations are not being accounted for.

Thanks for your help; new to this.



closed account (o3hC5Di1)
Hi there,

As a kind remark, please try to wrap your code in [ code ]-tags next time, it makes it more readable.

As to your problem, let's have a look at this while loop:

1
2
3
4
5
6
7
8
9
10
//begin loop while sentinel value not yet read //
while (sales != -1)
{
commission = 0.09 * sales;
salary = commission + base;
++week;

printf ("Salary is \n", salary);

}


You are telling the program to loop while the sales variable is not equal to minus one.
If you ever want to stop the loop, the sales variable will have to be set to minus one at some point, when you want to break the loop, or it will never change into -1, repeating the loop indefinitely.

Hope that gets you on your way, do let us know if you need any further help.

All the best,
NwN

Thank you. I have made some revisions, but unable to run the program since i'm on a different computer. Does the "if" statement within the "while" suffice? Thanks again for your help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Sales Calculator Problem //

#include<stdio.h>
int main ()

{

float base; 
flat salary;
float sales;


base = 200;


while (sales >=1)
{  if (sales == -1)
break; 

printf ("Enter Sales, -1 to end: \n", sales);
scanf ("%f", &sales);

salary =base + 0.09 * sales; 


printf ("Salary is %f. \n", salary);

}


return 0;
I have drafted a final solution that ran successfully. Thanks again
Topic archived. No new replies allowed.