Thread: Problem with directing output to text file

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    9

    Problem with directing output to text file

    I have this program below, it works fine until it comes to the while loop, it asks me if I want to direct the output to a text file and the program doesnt let me enter a character in this case either yes or no. Can someone tell me whats wrong. Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define carrots 1.09
    #define artichokes 2.05
    #define onions 1.15
    
    
    int main(void)
    {
        char ans;
        char result;
        float nocarrots, noartichokes, noonions, totalcarrots = 0, totalartichokes = 0, totalonions = 0, totalall = 0, onionsweight = 0, artichokesweight = 0, carrotsweight = 0, weight = 0;
        float discount = 0;
        float shippingCost = 0;
        printf("Welcome to YourGreens.com!\n\n");
        printf("Our choices & prices for today are as follows:\n\n");
        printf("A.Carrots: 1.09 EUROS per KG\n");
        printf("B.Artichokes: 2.05 EUROS per KG\n");
        printf("C.Onions: 1.15 EUROS per KG\n\n");
        do
        {
        printf("Enter 'a' for carrots, 'b' for artichokes, 'c' for Onions or 'q' to Checkout:\n");
        scanf("%c",&ans);
        switch(ans)
            {
                case 'a': printf("Enter amount of Carrots in KG:\n");
                scanf("%f",&nocarrots);
                totalcarrots = totalcarrots + (nocarrots * carrots);
                carrotsweight = carrotsweight + (nocarrots);
                break;
    
    
                case 'b': printf("Enter amount of Artichokes in KG:\n");
                scanf("%f",&noartichokes);
                totalartichokes = totalartichokes + ( noartichokes * artichokes);
                artichokesweight = artichokesweight + (noartichokes);
                break;
    
    
                case 'c': printf("Enter amount of Onions in KG:\n");
                scanf("%f",&noonions);
                totalonions = totalonions + ( noonions * onions);
                onionsweight = onionsweight + (noonions);
                break;
            }
    
    
        }
        while ( ans != 'Q' && ans != 'q');
    
    
    
    
        weight = weight + (carrotsweight + artichokesweight + onionsweight);
        totalall = totalall + (totalcarrots + totalartichokes + totalonions);
        printf("THE TOTAL PRICE OF GROCERIES IS: %.2f EUROS\n",totalall);
        printf("TOTAL WEIGHT OF GROCERIES IS: %.2f KG\n\n",weight);
    
    
        if ( totalall >= 100 )
        {
            discount = totalall*0.05;
            totalall = totalall - ( totalall * 0.05);
            printf("For orders over 100 euros a 5% discount is given\n");
            printf("Your new total is: %.2f EUROS",totalall);
        }
    
    
        if ( weight <= 5 )
        {
            shippingCost = 6.5;
            totalall = totalall + 6.50;
            printf("For orders under 5 KG, the shipping costs come around 6.50 EUROS\n");
            printf("Your new total is: %.2f EUROS",totalall);
        }
    
    
        if ( weight > 5 && weight < 20 )
        {
            shippingCost = 14.0;
            totalall = totalall + 14.00;
            printf("For orders over 5KG & less than 20KG, the shipping costs will come around 14.00 EUROS\n");
            printf("Your new total is: %.2f EUROS",totalall);
        }
    
    
        if ( weight >= 20 )
        {
            shippingCost = 14.00 + (weight/2.0);
            totalall = totalall + ( 14.00 ) + ( weight/2 );
            printf("For orders of 20KG and exceeding, the shipping costs will be 14.00 EUROS and an added 0.50 EUROS per KG\n");
            printf("Your new total is: %.2f EUROS\n\n",totalall);
        }
    
    
        printf("\n\n");
        printf("Do you want to transfer output to a text file? y/n? \n");
        scanf("%c",&result);
    
    
        while ( result != 'n')
        {
            FILE* fout;
    
    
            fout = fopen("checkout.txt", "w");
    
    
            fprintf(fout, "bill,\tdiscount,\tshipping cost\n");
    
    
            fprintf(fout, "$%.2f,\t$%.2f,\t$%.2f\n", totalall, discount, shippingCost);
    
    
            fclose(fout);
        }
    
    
    
    
         return 0;
    
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    scanf() tends to leave the newline character into the stream. scanf("%c") reads that newline and the user never gets the chance to give his answer. To work around that, use scanf(" %c"), note the extra space before the percent sign. This tells scanf to ignore all whitespace characters.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    9
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++: Output An Entire Text File- Can It Be Done?
    By lusciousleanne in forum C++ Programming
    Replies: 6
    Last Post: 01-30-2008, 09:27 AM
  2. Text File Output
    By FJ8II in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2007, 05:03 AM
  3. Output Text File
    By ne0x in forum C Programming
    Replies: 9
    Last Post: 06-11-2005, 02:55 PM
  4. redefine cout to directing one file, is impossible?
    By toysoldier in forum C++ Programming
    Replies: 8
    Last Post: 08-19-2004, 11:27 AM
  5. C++ text file output...specifics
    By paranoidandroid in forum C++ Programming
    Replies: 3
    Last Post: 12-31-2001, 10:57 AM

Tags for this Thread