I wrote this C code to find the total number of tbles waited on in a week. For s
ID: 669044 • Letter: I
Question
I wrote this C code to find the total number of tbles waited on in a week. For some reason I can not get it to work. Does anyone see a problem that I am missing
// C code //
// This program will loop through 3 waiters and allow the user to input their daily hours. //
// It will then inform the user if the waiter is projected bonus pay. //
// Developer: John Doe //
// Date: 20 September 2015 //
#include
int main ()
{
/* variable definition: */
char waitersName[100];
float tableswaitedOn, sum, bonusPay;
int waiters, count;
// Create For Loop
for (waiters=0; waiters<3; waiters++)
{
/* Initialize count and sum */
count = 0;
sum = 0;
/* Input Waiters Name */
printf(“Waiters Name? ”);
scanf(“%s”, waiterName);
// Loop through to input values
while (count < 7)
{
printf(“Enter Tables Waited On Daily: ”);
scanf(“%f”, &tableswaitedOn);
sum=sum+tableswaitedOn;
printf(“Total tables waited on this week: %.02f ”, sum);
count=count+1;
}
printf(“%s tables waited on this week: %.02fn”, waiterName, sum);
if (sum>70)
{
bonuspay=sum-70;
printf(“Projected bonus pay for %s is: %.02f tables ”, waiterName, bonuspay);
}
}
return 0;
}
Stand In
Stephan 6 12 12 8 15 3 0
Tameka 0 10 8 0 6 17 0
Spike 0 0 0 19 20 16 16
Explanation / Answer
Nothing is wrong with your code Mr.David.
The thing is may be you copied this code from somewhere and pasted it in your editor.
The minute errors in your code I observed once I copied and pasted it in my geditor include:
1. The string quotes (") which you used in printf() and scanf() functions which looks very much the same as double quotes are not double quotes. So, I retyped those double quotes in all the printf() and scanf() function calls.
2. In the scanf() function where you scanned the waiterName is not declared. Its a typo. You declared it as waitersName and tried to access it as waiterName. So, I added s to that variable. The same happened also in the printf() function where you tried to print the waiterName.
3. In the last if block where you calculated the bonuspay, its not declared. Its a typo again. You declared it as bonusPay and you tried to access it as bonuspay. The same happened also in the last printff() statement.
Logical Errors:
1. You tried to display the statement: "Total tables waited on this week" for everyday, which should be done at the end of the week. So, this statement is removed as you already placed another statement for the same purpose outside that 7-day loop.
2. Instead of prompting a display statement: "Enter tables waited on daily: ", it will be better to prompt it like "Enter tables waited on day 1 (or 2 or 3 so on...) by Stephan(or Tameka or Spike etc...). So I modified that statement accordingly. If you don't want the prompt for every day, you just place that printf statement before the loop stating "Enter tables waited on daily (7 values for the week one for each day): "
3. It will be better to display a prompt like: "No bonus for @name this week. ". This is just a suggestion. If you want this to happen, just add an else to the last if statment, which prints this statment.
4. I didn't understood why you tried to declare the variables tableswaitedOn, sum, bonusPay as float type. It will be good idea to declare tableswaitedOn and sum as integer variables, as usally, a person will not waited on fractional tables, like 3.4, 5.38, 9.0 etc... Obviously if tableswaitedOn is integer, sum should also be integer. bonusPay variable is an arithmetic expression assignment, As this expression does not include any division operation, and as you are operating on integer variables, it will be better to declare also this variable as integer. I didn't modified that. If you wish to follow my suggestion, just update the code in the declaration from float to int, and the printf, scanf statements from %f to %i or %d.
After all those modifications, now the code looks like this:
Now the modified code goes here:
/* C code //
This program will loop through 3 waiters and allow the user to input their daily hours.
It will then inform the user if the waiter is projected bonus pay.
Developer: David Yurchak
Date: 20 September 2015 */
#include <stdio.h>
int main ()
{
/* variable definition: */
char waitersName[100];
int tableswaitedOn, sum, bonusPay;
int waiters, count;
// Create For Loop
for (waiters=0; waiters<3; waiters++)
{
/* Initialize count and sum */
count = 0;
sum = 0;
/* Input Waiters Name */
printf("Waiters Name? ");
scanf("%s", waitersName);
// Loop through to input values
while (count < 7)
{
printf("Enter Tables Waited On Day %i by %s: ",count+1, waitersName);
scanf("%i", &tableswaitedOn);
sum=sum+tableswaitedOn;
count=count+1;
}
//wprintf("Total tables waited on this week: %.02f ", sum);
printf("%s tables waited on this week: %i ", waitersName, sum);
if (sum>70)
{
bonusPay=sum-70;
printf("Projected bonus pay for %s is: %i tables ", waitersName, bonusPay);
}
}
return 0;
}
Hopefully, this code will work as per your requirement.