Monday, May 24, 2010

Pelase help me with this C program?

How can i do this program in C format the one that starts like this


#include%26lt;stdio.h%26gt;


main()


{


printf


scanf








}





This program will simulate a grocery store check out.





You will ask the user how many items they are purchasing today.


Then read in the individual prices one by one and add them together to get a total.


Tell the user the total of their purchases and ask them how much money they are giving you to pay for their purchase.


If they give you enough money, compute and display their change.


If they give you the exact amount display a message that they get no change.


If they do not give you enough money, tell them that and ask them to try again. Make sure they give you enough and don't compute their change until they do give you enough money.

Pelase help me with this C program?
Wow, that's a bit long.





First of all the app will ask the user for an integer input and store it in an int variable.





int count;


printf("How many groceries do you wish to buy?");


scanf("%d",%26amp;count);





Once you've done that, the app will need to use a for loop in order to sum the total cost. You need to declare first another variable (call it "sum"). Also declare a temporary variable to store the input given each time (call it "temp").





for(int i = 0 ; i %26lt; count ; i++)


{


printf("What's the next price?");


scanf("%d",%26amp;temp);


sum += temp;


}





Now print the total amount (I bet you can write it yourself). After that, you need to get the amount of cash given by the customer (let's store it in a variable called "pay"), and then write 3 if statements that deal with each and every one of the following options: Not enough money, the exact sum, and too much.





Not Enough: (Notice the use of a while loop)


while(pay %26lt; sum)


{


printf("You didn't give enough money! You need %d more",sum-pay);


scanf("%d", %26amp;temp); //Since we don't need temp anymore we can use it here


pay += temp;


}





Exact sum:


if(sum == pay)


printf("No change");





Too much:


if(sum %26gt; pay)


printf("Thank you. Your change is %d",pay-sum);
Reply:Your missing 2 main functions, actually 3, but that complicates things

bridal flowers

No comments:

Post a Comment