Abbas Rangwala
Abbas Rangwala

Reputation: 1

Why are test cases not passing in this c program?

Create a function that passes the Item’s Name, Quantity, and Price. And function should calculate the total amount (Quantity* Price) and print it. My Code-

#include <stdio.h> 
void displayString(char str[]); 

int main() 
{ 
    int cal(float,int);
    char str[50];
    float pri,c;
    int quan;
    scanf("%s",str);
    scanf("%d",&quan);
    scanf("%f",&pri); 
    c = cal(pri,quan);
    printf("Item name: %s, Price: %.2f, Quantity: %d\n",str,pri,quan);
    printf("Total Amount: %.2f",c);
}

int cal(float x,int y)
{
    float z;
    z = x * y;
    return(z);
}

Test case : output Item name: muruga, Price: 2.50, Quantity: 5
Total Amount: 12.50

My Output Item name: muruga, Price: 2.50, Quantity: 5 Total Amount: 12.00

Upvotes: 0

Views: 96

Answers (1)

Mohit Sharma
Mohit Sharma

Reputation: 348

The return type of cal function should be float and also function cal prototype should be outside main and it is better to have a printf statement giving an idea about input to program.



#include <stdio.h> 
void displayString(char str[]); 

float cal(float,int);  /*Function prototype should be outside main and
this function should have a return type of float and not int */

int main() 
{ 

    char str[50];
    float pri,c;
    int quan;
    printf("Enter name\n");
    scanf("%s",str);
    printf("Enter Quantity\n" );
    scanf("%d",&quan);
    printf("Enter price\n");
    scanf("%f",&pri); 
    c=cal(pri,quan);
    printf("Item name: %s, Price: %.2f, Quantity: %d\n",str,pri,quan);
    printf("Total Amount: %.2f\n",c);
}

float cal(float x,int y)  //Changed return type
{
    float z;
    z = x * y;
    return(z);
}

Upvotes: 0

Related Questions