I've been over this code several times but still cannont figure out why after the first time, it always prints "Add in sale? Y/N:" twice. Any ideas?

void PrintHeader();
int TotalSales();
float OverallProfit(float totalvalue, float drinkcost, int runningcost);
float CalculateCosts();//Not used yet
int IncentiveCheck();  //Not used yet

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

int main()
{
    int teasold=0, coffeesold=0, hotchocsold=0, lattesold=0;
    int flatwhitesold=0, totalsales=0, drinktype=0;
    float tea=1.00, coffee=1.50, hotchoc=2.00, chailatte=1.75, flatwhite=2.20;
    float totalvalue=0, runningcost=10.00, totalprofit=0, drinkcost, incentive, overallprof;
    float teacost=0.20, coffeecost=0.25, hotchoccost=0.50, lattecost=0.25, flatwhitecost=0.50;
    char yesno, dollar='$';

    PrintHeader();

    while (totalsales<500)
    {
          printf("\nAdd In Sale? Y/N: ");
          scanf("%c", &yesno);

          if ((yesno=='Y') || (yesno=='y'))
          {
               totalsales=TotalSales(totalsales);
               printf("\nDrink type?\n\n");

               printf("1.    Tea\n");
               printf("2.    Coffee\n");
               printf("3.    Hot Chocolate\n");
               printf("4.    Chai Latte\n");
               printf("5.    Flat White\n\n");

               printf("You choose: ");
               scanf("%d", &drinktype);

               switch (drinktype)
               {  case (1): teasold=teasold+1;
                            break;
                  case (2): coffeesold=coffeesold+1;
                            break;
                  case (3): hotchocsold=hotchocsold+1;
                            break;
                  case (4): lattesold=lattesold+1;
                            break;
                  case (5): flatwhitesold=flatwhitesold+1;
                            break;
                  default: printf("Please choise a valid option");
                           break;
               }
          }
          else if ((yesno=='N') || (yesno=='n'))
          {
               printf("\nTotal Sales: %d\n\n", totalsales);
               printf("Total Tea Sales: %d\n", teasold);
               printf("Total Coffee Sales: %d\n", coffeesold);
               printf("Total Hot Chocolate Sales: %d\n", hotchocsold);
               printf("Total Chai Latte Sales: %d\n", lattesold);
               printf("Total Flat While Sales: %d\n\n", flatwhitesold);

               totalvalue=tea*teasold+coffee*coffeesold+hotchoc*hotchocsold+chailatte*lattesold+flatwhite*flatwhitesold;
               printf("Total value of days sales: %.2f\n", totalvalue);

               drinkcost=teasold*teacost+coffeesold*coffeecost+hotchocsold*hotchoccost+lattesold*lattecost+flatwhitesold*flatwhitecost;
               printf("Running cost on drinks: %.2f\n\n", drinkcost);

               overallprof = OverallProfit(totalvalue, drinkcost, runningcost);
               printf("Total profit on days sales: %.2f\n\n", overallprof);

               if ((lattesold>20) || (flatwhitesold>20))
               {
                   incentive=totalprofit/100*2;
                   printf("Incentive target met\n");
                   printf("Incentive payout %c%.2f", dollar, incentive);
               }
                   else
                   {
                       printf("No incentive targets met\n");
                       printf("Incentive payout %c0.00", dollar);
                   }
               getch();
          }
    }

    getch();
}

void PrintHeader()
{
     printf("-------------------\n");
     printf("CHEAPO DRINKS CO cU\n");
     printf("      Evesham\n");
     printf("-------------------\n\n");
}

int TotalSales(totalsales)
{
    totalsales=totalsales+1;
    return totalsales;
}

float OverallProfit (float totalvalue, float drinkcost, int runningcost)
{
      int temp;
      temp=totalvalue-drinkcost-runningcost;
      return temp;
}                      

Most common problem in C - the hidden newline when dealing with a char

Add one space in scanf(" %c", etc.), and all will be well. The space in the format string, tells scanf() to go past any whitespace, including a newline.

It is because of this line:

scanf("%c", &yesno);

This will also read '\n' when pressing return in addition to whatever you entered. On the next iteration this will still be in the buffer so that statement will read that directly. It fails the input check so the next iteration is started which once again asks for input. This time the buffer would be empty. (if you only entered 1 character)

Replace that line with this (for example..):

scanf("%c", &yesno);

// Clear the input buffer, if needed.
if (yesno != '\n') while (getchar() != '\n');

-edit-

Add one space in scanf(" %c", etc.), and all will be well. The space in the format string, tells scanf() to go past any whitespace, including a newline.

While that works when inputting what's needed it will still cause undesired behaviour when entering "hello world" for example. It's not directly your question I guess, kieranrea, but it's something to keep in mind.

Edited 4 Years Ago by Gonbe

I am not sure of this, but i think it is because the stream is not cleared.
add

fflush(stdin);

before the printf("\nAdd In Sale? Y/N: ");

Edited 4 Years Ago by necrovore

it's the classic stream garbage problem when mixing formatted and unformatted input.

is what one of the morderators, deceptikon, had this to say to me when i was having similar problems some time back. you can read about it more here

so back to ur code, i modified it a little. here it is:

void PrintHeader();
int TotalSales();
float OverallProfit(float totalvalue, float drinkcost, int runningcost);
float CalculateCosts();//Not used yet
int IncentiveCheck();  //Not used yet
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int teasold=0, coffeesold=0, hotchocsold=0, lattesold=0,bool=0;
    int flatwhitesold=0, totalsales=0, drinktype=0;
    float tea=1.00, coffee=1.50, hotchoc=2.00, chailatte=1.75, flatwhite=2.20;
    float totalvalue=0, runningcost=10.00, totalprofit=0, drinkcost, incentive, overallprof;
    float teacost=0.20, coffeecost=0.25, hotchoccost=0.50, lattecost=0.25, flatwhitecost=0.50;
    char yesno, dollar='$';
    PrintHeader();

    /*this loop runs for taking input, and the sales figures are shown only after all entries are made*/
    do{
          if(totalsales==500) 
            break;        
          if(bool==0){       // i declared a new variable bool, for controlling the while loop
              printf("\nAdd In Sale? Y/N: ");
              yesno=getchar();
              printf("entered value: %c\n\n",yesno);
            }  
          if ((yesno=='Y') || (yesno=='y'))
          {
               totalsales=TotalSales(totalsales);
               printf("\nDrink type?\n\n");
               printf("1.    Tea\n");
               printf("2.    Coffee\n");
               printf("3.    Hot Chocolate\n");
               printf("4.    Chai Latte\n");
               printf("5.    Flat White\n\n");
               printf("You choose: ");
               scanf("%d", &drinktype);
               switch (drinktype)
               {  case 1: teasold=teasold+1;
                            break;
                  case 2: coffeesold=coffeesold+1;
                            break;
                  case 3: hotchocsold=hotchocsold+1;
                            break;
                  case 4: lattesold=lattesold+1;
                            break;
                  case 5: flatwhitesold=flatwhitesold+1;
                            break;
                  default: printf("Please choise a valid option");
                           break;
               }
            while(getchar()!='\n'){}   
          }
          else if ((yesno=='N') || (yesno=='n'))
          {
            break;  
          }

        printf("Add more? 1 for yes, 0 if no:  ");
        scanf("%d",&bool);
        while(getchar()!='\n'){} 
    }while(bool==1);

    printf("\nTotal Sales: %d\n\n", totalsales);
    printf("Total Tea Sales: %d\n", teasold);
    printf("Total Coffee Sales: %d\n", coffeesold);
    printf("Total Hot Chocolate Sales: %d\n", hotchocsold);
    printf("Total Chai Latte Sales: %d\n", lattesold);
    printf("Total Flat While Sales: %d\n\n", flatwhitesold);
    totalvalue=tea*teasold+coffee*coffeesold+hotchoc*hotchocsold+chailatte*lattesold+flatwhite*flatwhitesold;
    printf("Total value of days sales: %.2f\n", totalvalue);
    drinkcost=teasold*teacost+coffeesold*coffeecost+hotchocsold*hotchoccost+lattesold*lattecost+flatwhitesold*flatwhitecost;
    printf("Running cost on drinks: %.2f\n\n", drinkcost);
    overallprof = OverallProfit(totalvalue, drinkcost, runningcost);
    printf("Total profit on days sales: %.2f\n\n", overallprof);
    if ((lattesold>20) || (flatwhitesold>20))
    {
        incentive=totalprofit/100*2;
        printf("Incentive target met\n");
        printf("Incentive payout %c%.2f", dollar, incentive);
    }
    else
    {
        printf("No incentive targets met\n");
        printf("Incentive payout %c0.00", dollar);
    }
    while(getchar()!='\n'){}    
    return 0;
    getch();
}
void PrintHeader()
{
     printf("-------------------\n");
     printf("CHEAPO DRINKS CO cU\n");
     printf("      Evesham\n");
     printf("-------------------\n\n");
}
int TotalSales(totalsales)
{
    totalsales=totalsales+1;
    return totalsales;
}
float OverallProfit (float totalvalue, float drinkcost, int runningcost)
{
      int temp;
      temp=totalvalue-drinkcost-runningcost;
      return temp;
}

you will see that at a few places i put in a while(getchar()!='\n'){} line. this cleans up the input stream after scanf() polluted it. eliminating your repeated printing problem.

ps: using scanf() to read a character is like using a jackhammer to open ur sodacan. (bad analogy ? maybe, but you get the idea..) this place tells all about it!
also regarding

add
fflush(stdin);
before the printf("\nAdd In Sale? Y/N: ");

u should never use fflush(stdin). fflush() is designed for output buffer, not input. if you read the link i gave, why fflush(stdin) isnt ideal was also explained there.

Edited 4 Years Ago by somjit{}

This article has been dead for over six months. Start a new discussion instead.