Too few arguments in function to call [C program] - c

So, I have an assignment due tomorrow night for my online C programming class, and I am having some problems with my coding at the moment. I have brought the code to my teacher, but she doesn't seem to understand that she is being paid to teach me, not tell me that something is wrong with my code. I would appreciate if someone could take a look at the code and help me fix it. Code is located below. The location where I get my error is in main when calling the printtripSummary.
#include <stdio.h>
void welcomeMessage();
void askuserForInput();
void printtripSummary(float avgMiles, float minCost, float maxCost, float travelMiles);
int main()
{
/* Call the functions */
welcomeMessage();
askuserForInput();
printtripSummary();
printf("\nThank you, please drive safely and have a nice trip!\n");
return 0;
}
void welcomeMessage()
{
printf("Welcome to the Trip Planner!\n");
printf("So you are ready to take a trip? Let me help you plan for\n");
printf("your fuels costs and required stops to fill up your tank.\n");
printf("============================================================\n");
printf("Please provide answers to the prompts below and I will\n");
printf("display a summary for you when I have computed the results.\n");
printf("============================================================\n");
}
void askuserForInput()
{
float avgMiles, minCost, maxCost, travelMiles;
do{
printf("Please input your car's average miles per gallon (enter 0 to quit)>> ");
scanf_s("%f", &avgMiles);
if (avgMiles == 0)
break;
printf("Please tell me the range of fuel costs you expect to pay (per gallon>>)\n");
printf("The lowest per gallon cost of fuel is>> ");
scanf_s("%f", &minCost);
printf("The highest per gallon cost of fuel is>> ");
scanf_s("%f", &maxCost);
printf("Please tell me how many miles you plan to travel>> ");
scanf_s("%f", &travelMiles);
printtripSummary(avgMiles, minCost, maxCost, travelMiles);
} while (avgMiles != 0);
}
void printtripSummary(float avgMiles, float minCost, float maxCost, float travelMiles)
{
float avgGal, mingasPrice, maxgasPrice;
do{
avgGal = travelMiles / avgMiles;
mingasPrice = avgGal * minCost;
maxgasPrice = avgGal * maxCost;
printf("You will be required to purchase %.2f gallons of fuel.\n", avgGal);
printf("The price will range between %2f and $%.2f.\n", mingasPrice, maxgasPrice);
} while (avgMiles != 0);
}

Comment out the function call in main like this (Line 13):
//printtripSummary();
Because you call the function already in askuserForInput(); and this function get called in main
OR if you want to call the function also in main your have to pass the required arguments which are:
(float avgMiles, float minCost, float maxCost, float travelMiles)
Also you have a endless loop in the function printtripSummary(); Because you have a do...while loop which checks if avgMiles != 0, but since your not changing the value of avgMiles in this loop it's endless!

On line 13 you're calling the printtripSummary function without passing any arguments to it. You have to provide the 4 arguments the function definition specifies (avgMiles, minCost, maxCost, travelMiles).

Related

In C Scanf not grabbing inputs past the first instance of scanf

Coding in visual studio code
#include<stdio.h>
int main(){
char firstLetterofName;
int numOfVisits;
float priceOfDrink; //Creating the variables leaving them empty for now
float total;
printf("Hello! What is your name?\n");
scanf("%c", &firstLetterofName); //Asking for an inputting the first letter of users name
printf("How many times have you visited Starbucks in a month\n");
scanf("%d", &numOfVisits); //Asking and inputting number of visits
printf("What is the price of the drink you order?\n");
scanf("%f", &priceOfDrink); //Asking and inputting price of drink
total = priceOfDrink * numOfVisits;
printf("Wow %c! You have spent $%d on Starbuck!", firstLetterofName, total);
return 0;
}
First attempt using full name Terminal outputs
PS C:\C Cpp> cd "c:\C Cpp\" ; if ($?) { gcc test.c -o test } ; if ($?) { .\test }
Hello! What is your name?
Logan
How many times have you visited Starbucks in a month
What is the price of the drink you order?
Wow L! You have spent $0 on Starbuck!
Second attempt using only first letter
PS C:\C Cpp> cd "c:\C Cpp\" ; if ($?) { gcc test.c -o test } ; if ($?) { .\test }
Hello! What is your name?
L
How many times have you visited Starbucks in a month
5
What is the price of the drink you order?
2.0
Wow L! You have spent $0 on Starbuck!
PS C:\C Cpp>
Expected output is Wow L! You have spent $10.0 on starbucks
For the name part it is suppose to only take the first letter.
In C Scanf not grabbing inputs past the first instance of scanf
With input "Logan\n", code scans in the 'L' as the firstLetterofName with the "ogan\n" reamaining in stdin for the next input function.
scanf("%c", &firstLetterofName);
With input "ogan\n", scanf() fails and returns 0. numOfVisits was not changed. Code unfortunately did not not check the return value of scanf() to test success.
scanf("%d", &numOfVisits); // FAILED
To quickly discern scanf() troubles, check scanf() return values for success.
Do not rely on the user to enter compliant text. Robust code watches out for too much data, not enough data, non-numeric data, etc. User input is evil.
Consider using fgets() to read a line of user input and then parse the resultant string.
#include<stdio.h>
int main(){
char firstLetterofName;
int numOfVisits;
float priceOfDrink; //Creating the variables leaving them empty for now
float total;
printf("Hello! What is your name?\n");
scanf("%c", &firstLetterofName); //Asking for an inputting the first letter of users name
//clear the input stream
fflush(stdin);
printf("How many times have you visited Starbucks in a month\n");
scanf("%d", &numOfVisits); //Asking and inputting number of visits
printf("What is the price of the drink you order?\n");
scanf("%f", &priceOfDrink); //Asking and inputting price of drink
total = priceOfDrink * numOfVisits;
printf("Wow %c! You have spent $%f on Starbuck!", firstLetterofName, total);
return 0;
}
Use fflush(stdin) to flush the input stream. Availabe in gcc

Can anyone point out what mistake I made in my code?

Pooja would like to withdraw X $US from an ATM. The cash machine will
only accept the transaction if X is a multiple of 5, and Pooja's
account balance has enough cash to perform the withdrawal transaction
(including bank charges). For each successful withdrawal the bank
charges 0.50 $US. Calculate Pooja's account balance after an attempted
transaction.
#include <stdio.h>
int main(void)
{
float initialamt;
int deductamt =0;
// taking input of the account balance and money to be withdrawn
scanf("%f", &initialamt);
scanf("%d", &deductamt);
// Checking if the mat is a multiple of 5 and less than act balance
if(deductamt%5 ==0 && deductamt<= initialamt-0.50)
{
initialamt = initialamt - deductamt -0.50;
printf("%2f", initialamt);
}
else
{
printf("%2f", initialamt);
}
return 0;
}
I found the question on CodeChef, is your question https://www.codechef.com/problems/HS08TEST?
Then what the question input is deduct - init balance in order, but not init balance - deduct in order, so just switch the scanf.
scanf("%d", &deductamt);
scanf("%f", &initialamt);

Code is ignoring IF command in C, Issues with FOR

I had posted on here before, but I was never able to get the help I needed.
I'm working on a school project and I can not get my program to work properly.
The program should prompt the user to enter the number of gallons used and
the number of miles driven for each of the 3 tanks of gas. The program should
then calculate and display the miles per gallon obtained for each tank. Once
processing is complete for the 3 tanks, the program will calculate the overall
mileage(total gallons / total miles) and display a friendly "Goodbye" message.
The issue i am having is that I can not get it to display to OVERALL Millage. it ends after looping 3 times.
I know different loop statements need conditions to be met, but I cant get the FOR loop to work properly. Im getting really frustrated, cause I know this should not be this hard.
Code
#include <stdio.h>
int main(void)
{
int miles,i=3;
float gallons, mg, overall = 0, avg = 0;
while(i>0)
{
printf("Enter the gallons used: ");
scanf("%f", &gallons);
printf("Enter miles driven: ");
scanf("%d", &miles);
mg = miles/gallons;
printf("The miles/gallon for this tank was : %f\n", mg);
overall += miles;
avg += gallons;i--;
}
if(gallons == 0)
{
printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
exit(0);
}
return 0;
}
If I read your code correctly, then what is preventing the overall mileage from being printed is the following final if statement:
if (gallons == 0)
If you remove it, then the overall mileage should print. Use this pattern:
while (i > 0)
{
// your while loop here
}
printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
exit(0);
This if (if (gallons == 0) {})block is out of while loop.
First, you need to move the if loop inside while loop.
and this if condition should be for variable i as follow and not for gallons.
if (i == 0)
{
printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
}
In this case, after 3 iteration, value of i will be 0 so it will enter into the if block and calculate and print the overall miles/gallon.
Adding to Tim Biegeleisen's answer:
mg = miles/gallons;
What if gallons equals to 0? e.g. 0 miles for 0 gallons
This will lead to floating point exception.
A simple if-else can solve this problem!
if(!gallons)
mg = 0;
else
mg = miles/gallons;

C Segmentation Fault in while loop-cannot resolve issue despite debugging

This is the piece of code I have which prints my diffused density matrices to a file after every nth time step of the simulation time given by fdparam_1.t_domain. t and fdparam_1.Dt are variables of the type double. All variables are declared and defined either with user input or with pre-defined values in the code.
Please note that the last time I posted the code for the segmentation fault, I modified the code as per the suggestions and this piece of code below is the modified one, although the operations are obviously the same.
int main(void)
{
int i,j,size,sz;
double *ux, *vy, *ux0, *vy0, *r, *r0, t, sum, sum1;
struct fdparam fdparam_1;
printf("Enter the number of grid points: \t");
scanf("%d", &fdparam_1.N);
printf("Enter the maximum number of iterations: \t");
scanf("%d", &fdparam_1.MAXIT);
printf("Enter the value for time domain and the time interval: \t");
scanf("%d\t%d", &fdparam_1.t_domain, &fdparam_1.Del_t);
printf("Enter the time step, number of molecules: \t \t");
scanf("%lf\t%lf", &fdparam_1.Dt, &fdparam_1.dens);
printf("Enter the volume of the fluid: \t");
scanf("%lf", &fdparam_1.V);
printf("Enter the diffusion coefficient and viscosity and angular velocity(in rad/s): \t \t");
scanf("%lf\t%lf\t%lf",&fdparam_1.diff, &fdparam_1.mu, &fdparam_1.wv);
size=(fdparam_1.N+2)*(fdparam_1.N+2);
sz=fdparam_1.t_domain/fdparam_1.Del_t;
double map[fdparam_1.N+2][fdparam_1.N+2],map_init[fdparam_1.N+2][fdparam_1.N+2],n_calc, time[sz+1];
r = (double*) calloc (size,sizeof(double));
r0 = (double*) calloc (size,sizeof(double));
ux = (double*) calloc (size,sizeof(double));
vy = (double*) calloc (size,sizeof(double));
ux0 = (double*)calloc (size,sizeof(double));
vy0 = (double*)calloc (size,sizeof(double));
double vol = fdparam_1.V;
FILE *fp1[sz+1];
var_init(fdparam_1.N,r0,ux0,vy0,fdparam_1.dens);
sum1=r0[0];
for (i=0;i<=fdparam_1.N+1;i++){
for (j=0;j<=fdparam_1.N+1;j++){
sum1+=r0[(i)+((fdparam_1.N+2)*j)];
}
}
double n_act = sum1*vol;
printf("Time = %lf \t Initial Nr. of Molecules is: %e \n",t,n_act);
int l = 0;
add_source(fdparam_1.N,r,r0,fdparam_1.Dt);
t=0;
int k=0;
while(t<=fdparam_1.t_domain){
swap(r0,r);
density_solve(fdparam_1.N,r,r0,ux0,vy0,fdparam_1.Dt,fdparam_1.diff,fdparam_1.MAXIT); //uses ux and vy calculated from Navier Stokes in the velocity solver to calculate density
// creating multiple files to store the density values during the simulation at every Del_t time interval
if(((int)(t*100))%fdparam_1.t_domain==0){
char filename[sz+1];
sprintf(filename,"density%d.txt",l);
fp1[l]=fopen(filename,"w");
for (i=0;i<=fdparam_1.N+1;i++){
for (j=0;j<=fdparam_1.N+1;j++){
map[i][j]=r[(i)+(fdparam_1.N+2)*j];
fprintf(fp1[l],"%lf \t",map[i][j]);
}
fprintf(fp1[l],"\n");
}
fclose(fp1[l]);
sum=r[0];
for (i=0;i<=fdparam_1.N+1;i++){
for (j=0;j<=fdparam_1.N+1;j++){
sum+=r[(i)+((fdparam_1.N+2)*j)];
}
}
n_calc=sum*vol;
printf("Time = %lf \t Calculated Nr. of Molecules = %e \n",t,n_act);
}
l++;
t+=fdparam_1.Dt;
}
void add_source(int n, double *x, double *s, double dt)
{
int i, size;
size = (n+2)*(n+2);
for (i=0; i<size; i++)
{
x[i]+=s[i]; //add source terms to the density
}
}
I am sorry the code is divided into numerous functions and header files and it is really difficult for me to prepare a minimal working code out of it. The above is my complete main function but here is what is happening now when I run the gdb debugger again without supplying any breakpoint, it seems to be executing the step where it is supposed to print t and n_act because this is the actual expected output which I am supposed to get but I get segmentation fault instead,
Printing source densities now:
Time = 0.000000 Initial Nr. of Molecules is: 8.820000e+06
Time = 0.000000 Calculated Nr. of Molecules = 8.820000e+06
Time = 10.000000 Calculated Nr. of Molecules = 8.820000e+06
Time = 20.000000 Calculated Nr. of Molecules = 8.820000e+06
... and so on till Time=1000
Where the issue is:
Based on your previous post, Segmentation fault - Two functions don't run simultaneously, where N is the number of points, it looks like your indexes are going of of bounds.
How to resolve it:
Revise your loop comparisons for i and for j where used for map.

How to make a table in C using loops

How do you make a table like the pic (you are using the ideal gas law). The volume values should start on the left-most column with the start volume and increase in equal steps
such that the right-most column's volume is the stop volume. For each entry in the table, compute the pressure at the given temperature and volume, Please help.
Volume: 10.00 --- 18.89 --- 27.78 --- 36.67
Temp:
300.00 - 24.94 --- 13.20 --- 8.98 --- 6.80
400.00 - 33.26 --- 17.61 ---11.97--- 9.07
500.00 - 41.57 --- 22.01 ---14.97--- 11.34
#include <stdio.h>
int main(void)
{
float vol1, vol2;
float temp1, temp2;
float R = 8.314;
int mole;
int rows;
int columns = 8;
printf("Enter the starting volume (in meters cubed):");
scanf("%f",&vol1);
while(vol1<0)
{
printf("Error: Enter a number that is positive:");
scanf("%f",&vol1);
}
printf("Enter the ending volume (in meters cubed):");
scanf("%f",&vol2);
while(vol2<0)
{
printf("Error: Enter a number that is positive:");
scanf("%f",&vol2);
}
printf("Next enter the starting temperature (in kelvin):");
scanf("%f",&temp1);
while(temp1<0)
{
printf("Error: Enter a number that is positive:");
scanf("%f",&temp1);
}
printf("Enter the ending temperature (in kelvin):");
scanf("%f",&temp2);
while(temp2<0)
{
printf("Error: Enter a number that is positive:");
scanf("%f",&temp2);
}
printf("Enter the number of moles:");
scanf("%f",&mole);
while(mole<0)
{
printf("Error: Enter a number that is positive:");
scanf("%f",&mole);
}
printf("How many rows should the temperature value have?\n");
scanf("%d",&rows);
while(rows<1)
{
printf("Error: Enter a number that is positive:");
scanf("%d",&rows);
}
return 0;
}
I think I know what you're asking, so I'll reword your question so it is clear. You are trying to print out a 2D table with values that change in equal steps in the x direction (volume in this case) and equal values in the y direction (temperature in this case). I think that vol1 is starting volume and vol 2 is ending volume? and same for tmeperature?
The key to doing this is to use nested for loops
So something like this
for (x=vol1; x<vol2; x + volstepsize)
{
for(y=temp1; y<temp2; y + tempstepsize)
{
compute gas law equation here using x and y and do a print statement
}
perform a blank print line statement here to indent/start the next row
}
By having nested for loops you will print out row and column sequentially; this is a fundamental concept for accessing and writing 2D tables.
Hope that helps!

Resources