2
  • By using a for loop. I want to let the user input the amount of lemonade that they want to order. Then my program will calculate the tax, subtotal, and total. After it should let them continue by either entering 'y' or 'n'.

  • When I compile these codes. All I get are blanks. What is wrong with my codes?

So I made some changes. But I don't know how to have it stop the program if I enter 'n' if they ask to continue.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void main()
{
    int NumCups;
    float SubTot, Tot = 0, Tax;
    char more;

    printf("Enter the number of cups:");
    scanf("%d", &NumCups);
    SubTot = NumCups*1.29;
    Tax = SubTot*0.0825;
    Tot = SubTot + Tax;

    printf("Subtotal:%0.2f\nTax:%0.2f\nTotal:%0.2f\n", SubTot, Tax, Tot);
    fflush(stdin);
    printf("Thank you.\nWould you like to order more lemonade?\n");

    for (; scanf("%c", &more);)
    {
        printf("Enter the number of cups:");
        scanf("%d", &NumCups);

        SubTot = NumCups*1.29;
        Tax = SubTot*0.0825;
        Tot += SubTot + Tax;
        fflush(stdin);
        printf("Subtotal:%0.2f\nTax:%0.2f\nTotal:%0.2f\n", SubTot, Tax, Tot);
        printf("Thank you.\nWould you like to order more lemonade?\n");
    }
    system("pause");
}
10
  • you're expecting that some symbol should be entered first only then you will ask for number of cups Commented Apr 25, 2015 at 0:06
  • 1
    Not related to your problem, but note that you should use int main() instead of void main(). Also, fflush(stdin) is undefined behaviour. (This might be related to your problem.) Also, please post a compilable example, stating the expected output and actual output explicitly. Commented Apr 25, 2015 at 0:09
  • @ace I made some changes. Commented Apr 25, 2015 at 0:17
  • What's the point of count? I don't see it used anywhere other than being incremented (for no apparent reason?). Why not just use a while loop? I think it would be more appropriate for your program. Commented Apr 25, 2015 at 0:23
  • @digitalninja the assignment was to use a for loop. Commented Apr 25, 2015 at 0:24

2 Answers 2

1

Try this:

 for (; scanf("%c", &more)&&more=='y';)

This change will let you continue only when you use the y character.

Sign up to request clarification or add additional context in comments.

Comments

0
do {
    printf("Enter the number of cups:");
    scanf("%d", &NumCups);

    SubTot = NumCups*1.29;
    Tax = SubTot*0.0825;
    Tot += SubTot + Tax;
    fflush(stdin);
    printf("Subtotal:%0.2f\nTax:%0.2f\nTotal:%0.2f\n", SubTot, Tax, Tot);
    printf("Thank you.\nWould you like to order more lemonade?\n");
} while(1 == scanf(" %c", &more) && 'y' == more);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.