I've started to learn about adding "prototype" into my code in my Computer Science coarse. I was wondering if i did it right.
I also need help with a piece of code that after the user for my code decides to say no to "are there any more employees" question, it displays how many people have logged their info to find about their weekly salary.
This program is suppose to calculate a persons weeks salary.
I'm not sure what you mean by prototyping in this situation, but you definitely have some problems.
First of all, in line 12 you declare a variable flag and assign it the value 'y'||'Y'. When you "or" the character values 'y' and 'Y' together, you get the value 'true', which is 1. Instead, you probably want to read in the value for flag with cin or some such.
In line 14 you have an empty loop (semi-colon instead of a statement or compound statement) that executes as long as flag is 'y' or the value 'Y' (which will evaluate to non-zero, so true). So, you have an infinite loop doing nothing. Instead, you probably want the line to be: while(flag=='y'|| flag=='Y'){
Lines 30 - 45 appear to be a function defined in the middle of main(). You can't do that. You should define weeklypay outside of main() and call it where you need to.
i've added this a line here:on line 62-65. Does that line make any sense? What it suppose to do is if they do not wish to continue, it's tells them how many people have used the the program to check their salary. And i can't get this piece to work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
withheld=.20*(salary-600-exemption*100);
cout<<"your salary is $"<<salary;
cout<<"The withheld is $"<<withheld;
cout<<"Are there other employees?y,Y for yes, anything else is no";
cin>>flag;
i=1;
i++;
if (flag=='y'!||flag=='Y'!)
cout<<"the number of employees are "<<i;
system("pause");
return 0;
}
Lines 30-45, by themselves, are fine*, and do match the declaration, or prototype, in line 6. However, lines 46-62 are apparently the completion of the main() function. You cannot stick the definition of a function (weeklypay() in this case) inside the definition of another function (main() in this case). Move weeklypay() to after line 62.
In your latest snippet, this line should not compile: if (flag=='y'!||flag=='Y'!)
The exclamation points are extraneous and semantically incorrect.
You also haven't shown how you are populating the flag value.
*When you calculate overtime pay, time-and-a-half is generally calculated only on the hours above 40, not the entire time worked during the week. Also, you can replace line 37 with a simple else statement rather than anelseif.
When you've made changes, post your entire code again.
Upon closer inspection, it looks like you intend for main() to end in line 26. If that's the case, what are line 49 - 67? I think what you wanted to do was make 49 - 67 part of main. If that's the case, then 29 - 45 (THE WHOLE THING) needs to move to line 67. You also need to remove line 24 so main() doesn't return early.
If you really do want main to end in 26, then you need to figure out what you're trying to do with lines 49 - 67. They don't make sense to me.
Your while loop in line 15 is still an infinite loop. The semi-colon at the end or the statement is an empty statement that will be executed over and over and over again because flag is never being set to anything other than 'y'. You need to replace the ';' with a '{' like I said in my first post.
There is just too much wrong with your code right now to itemize everything. What you need to do is design your main() function in psuedo code. I think it will be something like this:
1 2 3 4 5 6 7 8 9 10
Initialize flag = 'y'
Initialize counter = 0
Begin Loop While flag == 'y'
Input salary information
Call weekly pay
Print out results
Increment Counter
Ask for additional employees
End Loop
Print out counter
When you use a loop, only the first statement following the loop is governed by the loop condition. If you want more than one statement to be governed by the loop condition, you must use a compound statement (a block of statements delimited by curly brackets {}.) But, you should note the loop condition will always be true, currently, even with the addition of the curly brackets. flag never changes.
#include <iostream>
#include <cmath>
#include <stdlib.h>
usingnamespace std;
double weeklypay(double hourlyrate,double hours); //prototype
double amount(double withheld,double exemption);
int main()
{
double hourlyrate,hours,exemption,withheld,salary;
char flag= 'y';
while(flag=='y');
{
cout<<"Hourly Salary: ";
cin>> hourlyrate;
cout<<"Hours: ";
cin>> hours;
cout<<"Number of Exemptions: ";
cin>> exemption;
weeklypay(hourlyrate,hours);
amount(withheld,exemption);
withheld =0;
salary=0;
return 0;
}
double weeklypay(double hourlyrate,double hours);
{
double salary;
if(hours>40)
{
salary=hours*hourlyrate*1.5;
}
else
{
salary=hours*hourlyrate;
}
return salary;
}
double amount(double withheld,double exemption);
{
double salary,i;
withheld =.20*(salary-600-exemption*100);
cout<<"your salary is $"<<salary;
cout<<"The withheld is $"<<withheld;
cout<<"Are there other employees? y for yes, anything else is no";
cin>>flag;
i=1;
i++;
if (flag==!'y')
cout<<"the number of employees are "<<i;
system("pause");
return 0;
}
}
i can't get it to compile. I get " undefined reference to weekpay(double,double)" "undefined reference to amount(double,double)". For the last function, i don't what to return to. Do i return "amount"?