In: Computer Science
Hey guys,
So it seems like I made a slight error in my program. So one of the requirement for this program is not to show any negatives shown as a output, however my program showed some negative numbers. I was wondering if someone could look over my program and find the error and maybe give it a tweak. I don't want to change my program because I worked so hard for it. Anyway here the assignment at the bottom is my code. Thanks in advance:
In this program, you are to store information about sales people and their individual sales from a company. You are to store the first name and quantity sold for each transaction.
You are to allow records to be stored for sales transactions. In fact, you need to ask the user for the total possible number of sales.
You are to allow data entry of 0 to maximum number of transaction. You are to store in the computer the income for each person for each transaction. No negative sales permitted. Here are the rules for calculating income:
The income for a person is progressive. He or she makes money for the first 5 items sold + money from the next 5 items sold + money for the next 500 items sold + the money for any other sale.
The program will have three options: * Record a new sale, * Show income from each existing sales transaction, * Quit
Example output
Name Total Items Sold Total Income
Tom 1 $5.00
Mary 5 $25.00
Willie 7 $35.00
Jack 511 $12,610
My code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
float incomePower(int itemSold){
float incomes;
int numbers = 4;
int breakpoints[] = {0, 5, 5+5, 5+5+500};
float rates[] = {5.0, 10.0, 25.0, 35.0};
for (int roc = 0; roc < numbers; roc++) {
if (itemSold <= breakpoints[roc]) {
break;
}
if ( roc < numbers - 1 && itemSold >= breakpoints[roc + 1]) {
incomes += rates[roc] * (breakpoints[roc + 1] - breakpoints[roc]);
} else {
incomes += rates[roc] *(itemSold - breakpoints[roc]);
}
}
return incomes;
}
int main(){
int megaSale = 0, roc = 0, arrayPointer = 0;
printf("\nThe total income of sales of each Person\n");
printf("\nEnter the maximum number of sales: \n");
scanf("%d", &megaSale);
char names[megaSale][100];
int itemsold[megaSale];
float totalincome[megaSale];
while (1) {
int choice;
printf("Choose:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the name of sales person");
scanf("%s", names[arrayPointer]);
printf("Enter the number of sale of %s: ", names[arrayPointer]);
scanf("%d", &itemsold[arrayPointer]);
totalincome[arrayPointer] = incomePower(itemsold[arrayPointer]);
arrayPointer += 1;
break;
case 2:
printf("\nName\t\tTotal Items Sold\t\tTotal Income");
for (roc = 0; roc <arrayPointer; roc++) {
printf("\n%s\t\t\t%d\t\t\t%.2f\n", names[roc], itemsold[roc], totalincome[roc]);
}
break;
case 3:
exit(0);
break;
default:
printf("Invalid entry");
}
}
return 0;
}
Hi,
Please find the answer below:
-----------------------------
Fixes:
This is to increase the readability of the program:
Added the option menu with options 1,2 and 3 below the choose
statement.
Fix:
do{
printf("Enter the number of sale of %s: ",
names[arrayPointer]);
scanf("%d", &itemsold[arrayPointer]);
} while (itemsold[arrayPointer] < 0)
After fix the system will prompt again
The total income of sales of each Person
Enter the maximum number of sales:
4
Choose:
1. Record a new sale:
2. Show income from each existing sales transaction:
3. Quit.
1
Enter the name of sales person:Tom
Enter the number of sale of Tom( No negative sales
permitted.):-5
Enter the number of sale of Tom( No negative sales
permitted.):5
Choose:
1. Record a new sale:
2. Show income from each existing sales transaction:
3. Quit.
Complete Code:
-------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
float incomePower(int itemSold)
{
float incomes;
int numbers = 4;
int breakpoints[] = {0, 5, 5+5, 5+5+500};
float rates[] = {5.0, 10.0, 25.0,
35.0};
int roc;
for (roc = 0; roc < numbers; roc++)
{
if (itemSold <=
breakpoints[roc])
{
break;
}
if ( roc < numbers -
1 && itemSold >= breakpoints[roc + 1])
{
incomes += rates[roc] * (breakpoints[roc + 1] - breakpoints[roc]);
}
else
{
incomes += rates[roc] *(itemSold - breakpoints[roc]);
}
}
return incomes;
}
int main()
{
int megaSale = 0, roc = 0, arrayPointer = 0;
printf("\nThe total income of sales of each
Person\n");
printf("\nEnter the maximum number of sales:
\n");
scanf("%d", &megaSale);
char names[megaSale][100];
int itemsold[megaSale];
float totalincome[megaSale];
while (1)
{
int choice;
printf("Choose:\n");
printf("1.:Record a new
sale\n");
printf("2.:Show income
from each existing sales transaction\n");
printf("3.:Quit\n");
scanf("%d",
&choice);
switch (choice)
{
case 1:
printf("Enter the name of sales person:");
scanf("%s", names[arrayPointer]);
do
{
printf("Enter the number of sale of %s(No negative sales
permitted.): ", names[arrayPointer]);
scanf("%d", &itemsold[arrayPointer]);
}
while (itemsold[arrayPointer] < 0);
totalincome[arrayPointer] = incomePower(itemsold[arrayPointer]);
arrayPointer += 1;
break;
case 2:
printf("\nName\t\tTotal Items Sold\t\tTotal Income");
for (roc = 0; roc <arrayPointer; roc++)
{
printf("\n%s\t\t\t%d\t\t\t $ %.2f\n", names[roc], itemsold[roc], totalincome[roc]);
}
break;
case 3:
exit(0);
break;
default:
printf("Invalid entry");
}
}
return 0;
}
------------------------------------------------------
Run Output:
---------------------------------------------
The total income of sales of each Person
Enter the maximum number of sales:
4
Choose:
1.:Record a new sale
2.:Show income from each existing sales transaction
3.:Quit
1
Enter the name of sales person:Tom
Enter the number of sale of Tom(No negative sales permitted.):
-1
Enter the number of sale of Tom(No negative sales permitted.):
1
Choose:
1.:Record a new sale
2.:Show income from each existing sales transaction
3.:Quit
1
Enter the name of sales person:Mary
Enter the number of sale of Mary(No negative sales permitted.):
5
Choose:
1.:Record a new sale
2.:Show income from each existing sales transaction
3.:Quit
1
Enter the name of sales person:Willie
Enter the number of sale of Willie(No negative sales permitted.):
7
Choose:
1.:Record a new sale
2.:Show income from each existing sales transaction
3.:Quit
1
Enter the name of sales person:Jack
Enter the number of sale of Jack(No negative sales permitted.):
511
Choose:
1.:Record a new sale
2.:Show income from each existing sales transaction
3.:Quit
2
Name
Total Items
Sold
Total Income
Tom
1
$ 5.00
Mary 5 $ 25.00
Willie 7 $ 45.00
Jack
511
$ 12610.00
Choose:
1.:Record a new sale
2.:Show income from each existing sales transaction
3.:Quit
---------------------------------------------
Screenshot:
Hope this helps.