I am new to programming and I was wondering if someone could help me out. My error says
[Error] cannot convert 'float**' to 'float*' for argument '1' to 'void acceptDailyandConvert(float*, float*, float*)'
and this is my code.
Thanks so much in advance.
void acceptDailyandConvert(float*, float*, float*);
int
main()
{
// VARIABLE DECLARATION
float *dailyRate, *hourlyRate, *minRate;
// FUNCTION CALLING
acceptDailyandConvert(&dailyRate, &hourlyRate, &minRate);
}
void
acceptDailyandConvert(float *dailyRate, float *hourlyRate, float *minRate){
// accepts daily rate, converts into hourly rate and rate per minute (pointer variables)
printf("Enter your daily rate: ");
scanf("%f", &dailyRate);
// hourly rate is 1/8th of daily rate
hourlyRate = dailyRate * 0.125;
// minute rate is 1/60th of hourly rate
minRate = hourlyRate * 0.02;
printf("\n \t Hourly Rate: %.2f ", hourlyRate);
printf("\n \t Rate per minute: %.2f ", minRate);
}
Im not really sure how to go about it, ive tried mixing around the ampersand and the asterisk (Totally clueless).
Ahhh yes, the pain of learning c pointers!!
This is one way to do it...
#include <stdio.h>
void acceptDailyandConvert(float*, float*, float*);
int
main()
{
// VARIABLE DECLARATION
float dailyRate, hourlyRate, minRate;
// FUNCTION CALLING
acceptDailyandConvert(&dailyRate, &hourlyRate, &minRate);
}
void acceptDailyandConvert(float *dailyRate, float *hourlyRate, float *minRate){
// accepts daily rate, converts into hourly rate and rate per minute (pointer variables)
printf("Enter your daily rate: ");
scanf("%f", dailyRate);
// hourly rate is 1/8th of daily rate
hourlyRate[0] = dailyRate[0] * 0.125;
// minute rate is 1/60th of hourly rate
minRate[0] = hourlyRate[0] * 0.02;
printf("\n \t Hourly Rate: %.2f ", hourlyRate[0]);
printf("\n \t Rate per minute: %.2f ", minRate[0]);
}
If you want to use pointers, initialize them first, then pass the pointers to the function not the reference (remove the &). To access the pointer value use *, eg. *hourlyRate.
Related
i wanted to ask is my coding have something missing or i did wrong? I was doing function to calculate the employee salary. it can compile and run, but not showing the output that should produce.
I'm trying to get the out like this: (example the value of the output)
Please enter your salary: 1200
Please enter the persentage of epf deduction:11
Your epf deduction is $132.00
Your bonus (half of salary) is $600.00
Your total pay is $1200.00-$132.00+$6000.00=$1668.00
My coding is like this:
void BasicSalary(); //Ask user to enter basic salary.
void EPF(double); //To calculate the amount of employees evident fund (EPF) deduction from basic salary.
void CalculateBonus(double); //To calculate the amount of bonus. Assume bonus is half of basic salary.
double DisplayTotalPay(double,double); //To calculate the total pay after EPF deduction and bonus reward.
double salary,epf,totalepf,totalbonus;
int main()
{
void BasicSalary();
void EPF(double);
printf("\nYour EPF deduction is $%f ",totalepf);
printf("\nYour bonus (half of salary) is $%f ");
void CalculateBonus(double);
double DisplayTotalPay(double,double);
}
void BasicSalary()
{
printf("\nPlease enter your salary: ");
scanf("%f",&salary);
}
void EPF(double)
{
printf("\n\nPlease enter the percentage of EPF deduction: ");
scanf("%f",epf);
totalepf= ( epf / 100 ) * salary;
}
void CalculateBonus(double)
{
totalbonus = salary / 2;
}
double DisplayTotalPay(double,double)
{
printf("\nYour total pay is $%f - $%f + $%f = $%f ", salary, totalepf, totalbonus, salary-totalepf+totalbonus);
}
you have kept everything in your main under comment. How it will work .?
Also your functions argument variables are not named. Since all your variable are global, you don't require any argument. Better remove those data type declaration.
Another point is, use "%lf" with Double when scanning. "%f" is for float.
it's my first post here so please be gentle to me. I've recently started a C programming class on my university and I got really interested in it. Since I'm working with audio (mixing / mastering music) I decided to try and make a simple program that would calculate the delay times in ms for the user defined BPM (Beats per minute).
What I am currently stuck with is the following: I want the program to return to the start and ask the user to input again if he typed in the wrong BPM (0 in this case).
I tried a do while loop however it didn't work quite right, my program would still calculate everything as if a user just typed in 0 and if I typed a correct value it would just loop endlessly.
If I do an if else statement it gives the user a message but I'd like to prompt the user for input again together with the message.
I know that this is a pretty simple and basic question but any help would be greatly appreciated.
Here is my code so far:
int main(){
float BPM;
printf("Please input the BPM: ");
scanf(" %f", &BPM);
do{
float HZ = 1000;
float HZ_Result;
float BPM_QuarterNote=(60/BPM)*1000;
float BPM_WholeNote=BPM_QuarterNote*4;
printf("\n\nDelay time for whole note is: %.2f ms or %.2f Hz", BPM_WholeNote, 1000/BPM_WholeNote);
float BPM_HalfNote=BPM_QuarterNote*2.0;
printf("\n\nDelay time for 1/2 note is: %.2f ms or %.2f Hz", BPM_HalfNote, 1000/BPM_HalfNote);
printf("\n\nDelay time for 1/4 note is: %.2f ms or %.2f Hz", BPM_QuarterNote, 1000/BPM_QuarterNote);
float BPM_EightNote=BPM_QuarterNote*0.5;
printf("\n\nDelay time for 1/8 note is: %.2f ms or %.2f Hz", BPM_EightNote, 1000/BPM_EightNote);
float BPM_SixteenthNote=BPM_QuarterNote*0.25;
printf("\n\nDelay time for 1/16 note is: %.2f ms or %.2f Hz", BPM_SixteenthNote, 1000/BPM_SixteenthNote);
float BPM_32ndNote=BPM_QuarterNote*0.125;
printf("\n\nDelay time for 1/32 note is: %.2f ms or %.2f Hz", BPM_32ndNote, 1000/BPM_32ndNote);
}while(BPM > 0);
return 0;
}
You can use a while loop instead of the if to test for your condition
eg
/* while loop execution */
while( BPM == 0 ) {
/* get my input values */
}
Add this code below you BPM declaration.
do{
printf("Please input the BPM: ");
scanf(" %f", &BPM);
}while(BPM==0.00);
A simple example of a user input loop that exits only when user is satisfied:
int main(void)
{
float fNum= 5.2;
double dpNum= 5.2;
long double ldFloat;
char quit[]={" "};
while(quit[0] != 'q')
{
printf("\n\nEnter a float number: ");
scanf("%f", &fNum);
printf("Enter a double precision number: ");
scanf("%Lf", &ldFloat);
... other stuff as needed
printf("Enter any key to continue or 'q' to exit.");
scanf("%s", quit);
}
return 0;
}
do {
printf("Please input the BPM: ");
} while (scanf("%f", &bpm) == 0 || bpm < 0);
This loop prints the question until the user inputted a valid floating point number and that said number is at least 0.
Why am I getting this warning while trying to compile my program?
%lf' expects argument of type 'double', but argument 2 has type 'double *'
I'm using CodeBlocks IDE, But these lines give a huge number:
double calculate1 = mealPrice * (double)(percentage)/100;
printf("The tip that you should leave is %lf \n", &calculate1);
I'm new to C programming and still learning stuff.
// CS 262, Lab Section <208>
// Lab 2
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("Enter the price of the meal: \n");
double mealPrice = 0;
scanf("%lf\n", &mealPrice);
printf("Now enter the tip percentage: \n");
int percentage = 0;
scanf("%d\n", &percentage);
//Calculates tip amount in double, int, and float types
double calculate1 = mealPrice * (double)(percentage)/100;
printf("The tip that you should leave is %lf \n", &calculate1);
int calculate2 = (int)mealPrice * (int)(percentage/100);
printf("The tip that you should leave is %d\n", &calculate2);
float calculate3 = (float)mealPrice * (float)(percentage/100);
printf("The tip that you should leave is &f\n", &calculate3);
//Add tip to meal price
double total = calculate1 + mealPrice;
printf("The total price including tips is %lf\n", total);
printf("The meal cost is %f\nThe tip percentage is %d\nThe tip amount is%lf\nThe total cost is %lf\n", &mealPrice, &percentage, &calculate1, &total);
return 0;
}
The problem is that you should not use pointers with printf (unless you are using pointer format specifiers). You generally pass things by pointer to scanf (because it needs to change them) and by value to printf (because it's not supposed to change them). That's why you are getting huge numbers.
It should look like:
printf("The tip that you should leave is %lf \n", calculate1);
and
scanf("%lf\n", &mealPrice);
not
printf("The tip that you should leave is %lf \n", &calculate1);
or
scanf("%lf\n", mealPrice);
Also in the future, don't show us compiler warnings unless they are specifically connected to the code that you posted, or you are going to get confused responses.
I am making a program to convert weight and height from metric to US and vice versa. I did the height part successfully, but the part with the weight is giving me a runtime error that stack around variable was corrupted.
I know that happens with arrays because pretty much thats all I get when I google the issue, but this is happening with a regular integer variable in 2 different functions.
This is the function that calls other functions to convert weight, one is for input, one is to convert and one is for output:
void weight_to_metric(void){
int kilograms, pounds;
double grams, ounces;
int * pkilograms= &kilograms, *ppounds=£s;
double * pgrams=&grams, *pounces=&ounces;
input_us_weight(ppounds, pounces);
weight_us_to_metric(ppounds, pounces, pkilograms, pgrams);
output_metric_weight(pkilograms, pgrams);
}
this is the function that inputs
void input_us_weight(int* feet, double * inches){
printf("enter the number of pounds you want to convert: ");
scanf(" %lf", feet, "\n");
printf("enter the number of ounces you want to convert: ");
scanf(" %lf", inches, "\n");
}
this is the function that converts
void weight_us_to_metric(int* pounds, double* ounces, int* kilograms, double * grams){
double temp_kilograms;
*kilograms = *pounds / 2.2046 + ((*ounces / 16) / 2.2046);
temp_kilograms = *pounds / 2.2046 + ((*ounces / 16) / 2.2046);
*ounces = ((temp_kilograms - *kilograms) *2.2046)*16;
*grams = ((*ounces / 16.0)/2.2046) * 1000;
}
The output function doesn't even deal with the variable that corrupts. The variable that corrupts is pounds. The integer pounds declared in the initial variable.
How do i fix this?
You are using wrong format specifiers in one of your scanfs. Use %d when scanning an int and %lf when scanning a double. Change
scanf(" %lf", feet, "\n");
to
scanf("%d", feet);
Also, remove the third argument("\n") that you pass to the scanfs. It makes no sense.
Need some help with calculating the fixed monthly payment (P) required to fully amortize a loan of L dollars over a term of n months at a monthly interest rate of i. The given formula is: P = L[i(1 + i)n]/[(1 + i)n - 1]. I wrote a code but it didn't calculate Payment. I'm wondering if it is because I use double type together with int (for number of months) or the problem with formula?! Please help.
#include<stdio.h>
#include <math.h>
double calculatePayments(double rate, double loan, int payments);
int main() {
double principal, i, monthlyP;
int month;
printf ("Enter the principal amount: ");
scanf ("%f", &principal);
printf ("Enter the interest amount: ");
scanf ("%f", &i);
printf ("Enter the term in months: ");
scanf ("%d", &month);
monthlyP = calculatePayments (i, principal, month);
printf ("The monthly payment amount is %.2f: ", monthlyP);
return 0;
}
double calculatePayments(double rate, double loan, int payments) {
double mPayments;
mPayments = loan*(rate*(1 + rate)*payments)/((1 + rate)*payments - 1);
return mPayments;
}
Your scanf() requests %f format for a double; it should be %lf.
In addition to the need to fix the input (%lf instead of %f for doubles), I think your payment formula is wrong: since the future value of money grows exponentially, the formula should feature raising numbers to a certain power, which it does not.
The correct formula looks as follows (from here):
Since the loan needs to be paid off completely, FV is equal to zero.
Since pow(i+1, n) is used twice, it's a good idea to compute it once, and use the resultant variable in two places. The final version of this computation looks like this:
double calculatePayments(double rate, double loan, int payments) {
double mul = pow(1+rate, payments);
return (loan * mul * rate) / (mul - 1);
}
Demo on ideone computes the payment on $100,000 at 0.004% per month for 30 years at $524.67, which is the same value that excel's PMT function returns.
Note : When you enter the rate of 5.6% in your formula and in another calculator, don't forget that your formula takes the rate per month, not per year. Therefore, the rate that you plug into outside calculators must be 12 times what you enter into your calculator (for 5.6% annually you should enter 0.00466666
One of the first rules of debugging is to make sure you print the inputs to ensure that the program got the values you expected. It didn't, because you wrote:
scanf ("%f", &principal);
Since principal is a double, the format needs to be "%lf". Repeat for the interest rate. You should also check that the inputs succeeded:
if (scanf("%lf", &principal) != 1)
{
fprintf(stderr, "Failed to read principal\n");
return(1);
}
If you'd printed out the input values, you'd have known what was wrong immediately.
Read what dasblinkenlight said.
Also,
Fix your declarations and the variables you are using scanf() for.
principal should be loan. month should be payments.
i is okay. You need to calculate the monthly decimal number of the percentage given.
For instance,
interest = (i/100) /12;
Do that before your function call. Then just basically use dasblinkenlight's function at the bottom of your main.
hope you score a "10" ;)