I am new at this but I think I may have it. This is the assignment:

The program should ask for the Customers First Name and Last Name (Use Initials only for now)

-Use 2 different variables, one for the first name and one for the last

The program should ask for the prices of 5 different items

-Use 5 different input statements and 5 different variables

The program should add the 5 products together giving the subtotal price.

Find the Tax by multiplying the Subtotal by a Constant called TaxRate

For the Total, add the Tax to the subtotal

Output the First initial, a period and space, last initial, a period and space, a message to the user and the total


Example:

Please enter First initial

Please enter Last initial

Please enter cost of Item1

Please enter cost of Item2

Please enter cost of Item3

Please enter cost of Item4

Please enter cost of Item5

SubTotal = Item1 + Item2 + Item3 + Item4 + Item5

Tax = SubTotal * TaxRate

Total = SubTotal + Tax

OutPut=

B. S. 

Your total cost is:

Here is my code:

include <stdio.h> 

#define TaxRate 10.0f

/* global variable*/ 
char firstname[20]; 
char lastname[20]; 
    float Tax; 
    float st; 
    float Item1,Item2,Item3,Item4,Item5; 
    float total; 

/*functions protypes*/ 
void enterItemPrice(void); 
void getUserName(void); 
float subTotal(void); 
float calculatetax(void); 
float totalprice(void); 
void output(void); 

int main(void) 
{ 

    getUserName(); 
    enterItemPrice(); 
    st = subTotal(); 
    calculatetax(); 
    totalprice(); 
    output(); 

    return 0;     
} 

void getUserName(void) 
{ 
    printf("Enter your first name:"); 
    scanf("%s", firstname); 
    printf("\nEnter your last name:"); 
    scanf("%s", lastname); 
} 

void enterItemPrice(void) 
{ 
    printf("\nEnter price of item1:"); 
    scanf("%f", &Item1); 
    
    printf("\nEnter price of item2:"); 
    scanf("%f", &Item2); 

    printf("\nEnter price of item3:"); 
    scanf("%f", &Item3); 

    printf("\nEnter price of item4:"); 
    scanf("%f", &Item4); 
    
    printf("\nEnter price of item5:"); 
    scanf("%f", &Item5); 
    
}     

float subTotal(void) 
{ 
    float localst;
    localst = Item1 + Item2 + Item3 + Item4; 
    return localst; 
    
} 

float calculatetax(void) 
{ 
    Tax = st * TaxRate; 
}     

float totalprice(void) 
{ 
    total= Tax + st; 
} 

void output(void) 
{ 
    printf("the costumer's first name is %s", firstname); 
    printf("\nthe costumer's last name is %s", lastname); 
    printf("\nThe price of the first item is %f", Item1); 
    printf("\nThe price of the first item is %f", Item2); 
    printf("\nThe price of the first item is %f", Item3); 
    printf("\nThe price of the first item is %f", Item4); 
    printf("\nThe price of the first item is %f", Item5); 
    printf("\nthe subtotal is %f", st); 
    printf("\nthe taxable price is %f", Tax); 
    printf("\n the total price is %f", total); 

}

Recommended Answers

All 3 Replies

What is the question? Use code tags the next time.

Ok in subtotal function you are not adding the value of the fifth item.
Now if you can, try to avoid globals, use them only if you must . Read this link for string input (don't get me wrong your prog is working with scanf but read the link).
PS: I hope that you are not taxed like this :) .

include <stdio.h> 

#define TaxRate 10.0f

/* global variable*/ 
char firstname[20]; 
char lastname[20]; 
    float Tax; 
    float st; 
    float Item1,Item2,Item3,Item4,Item5; 
    float total; 

/*functions protypes*/ 
void enterItemPrice(void); 
void getUserName(void); 
float subTotal(void); 
float calculatetax(void); 
float totalprice(void); 
void output(void); 

int main(void) 
{ 

    getUserName(); 
    enterItemPrice(); 
    st = subTotal(); 
    calculatetax(); 
    totalprice(); 
    output(); 

    return 0;     
} 

void getUserName(void) 
{ 
    printf("Enter your first name:"); 
    scanf("%s", firstname); 
    printf("\nEnter your last name:"); 
    scanf("%s", lastname); 
} 

void enterItemPrice(void) 
{ 
    printf("\nEnter price of item1:"); 
    scanf("%f", &Item1); 
    
    printf("\nEnter price of item2:"); 
    scanf("%f", &Item2); 

    printf("\nEnter price of item3:"); 
    scanf("%f", &Item3); 

    printf("\nEnter price of item4:"); 
    scanf("%f", &Item4); 
    
    printf("\nEnter price of item5:"); 
    scanf("%f", &Item5); 
    
}     

float subTotal(void) 
{ 
    float localst;
    localst = Item1 + Item2 + Item3 + Item4; 
    return localst; 
    
} 

float calculatetax(void) 
{ 
    Tax = st * TaxRate; 
}     

float totalprice(void) 
{ 
    total= Tax + st; 
} 

void output(void) 
{ 
    printf("the costumer's first name is %s", firstname); 
    printf("\nthe costumer's last name is %s", lastname); 
    printf("\nThe price of the first item is %f", Item1); 
    printf("\nThe price of the first item is %f", Item2); 
    printf("\nThe price of the first item is %f", Item3); 
    printf("\nThe price of the first item is %f", Item4); 
    printf("\nThe price of the first item is %f", Item5); 
    printf("\nthe subtotal is %f", st); 
    printf("\nthe taxable price is %f", Tax); 
    printf("\n the total price is %f", total); 

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.