0

I have this program and during its first loop it works as intended, but from the 2nd loop on it doesn't execute the first scanf. We haven't been taught pointers, so I don't know if they can solve the problem. Should I try the problem with another loop syntax like for? Any help is appreciated.

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

int main(int argc, char *argv[])
{
    char type[20];
    int amount;
    double price;
    double priceP;
    double priceG;
    double priceA;
    double pricePL;
    double priceC;
    price=0;
    char frouro[4]= "add";
    char payment[5];
    double refund;

    do{

       printf("What kind of material do you want to recycle today?. It has to be either paper , glass, aluminium, plastic or cloth\n");

       scanf(" %s" , &type);

       printf("how many materials do you want to recycle today? It has to be more than 1 and a maximum of 10\n");

       scanf(" %d" , &amount);

       if (strcmp(type, "paper") == 0)
          { 

           priceP = priceP + amount * 0.10;

          }   
       if (strcmp(type, "glass") == 0)
          { 

           priceG =priceP + amount * 0.20;

          }
       if (strcmp(type, "aluminium") == 0)
          { 

          priceA =  priceA + amount * 0.15;

          }
       if (strcmp(type, "plastic") == 0)
          { 

          pricePL =  pricePL +amount * 0.05;

          }  
       if (strcmp(type, "cloth") == 0)
          {

           priceC =  priceC + amount * 0.50;

          }  
      printf("do you want to do anything else?\n");

      scanf(" %c", &frouro);
      }while(strcmp(frouro,"add")==0 && strcmp(frouro,"end")!=0);

      return 0;
}
Ckrielle
  • 54
  • 9
  • May happen if the second scanf failed to convert to decimal integer: The %d conversion specifier expects the input text to be formatted as a decimal integer. If it isn't, the conversion fails and the character that caused the conversion to fail is left in the input stream. Further calls to scanf() with the %d conversion specifier will choke on the same character. – christian mini Nov 24 '19 at 17:01

1 Answers1

1

Change scanf(" %c", &frouro); to scanf("%s", &frouro);.

As you are storing 1 char, during while conditional check it will be always false.

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

int main(int argc, char *argv[])
{
    char type[20];
    int amount;
    double price;
    double priceP;
    double priceG;
    double priceA;
    double pricePL;
    double priceC;
    price=0;
    char frouro[4]= "add";
    char payment[5];
    double refund;

    do{

       printf("What kind of material do you want to recycle today?. It has to be either paper , glass, aluminium, plastic or cloth\n");

       scanf(" %s" , &type);

       printf("how many materials do you want to recycle today? It has to be more than 1 and a maximum of 10\n");

       scanf(" %d" , &amount);

       if (strcmp(type, "paper") == 0)
          { 

           priceP = priceP + amount * 0.10;

          }   
       if (strcmp(type, "glass") == 0)
          { 

           priceG =priceP + amount * 0.20;

          }
       if (strcmp(type, "aluminium") == 0)
          { 

          priceA =  priceA + amount * 0.15;

          }
       if (strcmp(type, "plastic") == 0)
          { 

          pricePL =  pricePL +amount * 0.05;

          }  
       if (strcmp(type, "cloth") == 0)
          {

           priceC =  priceC + amount * 0.50;

          }  
      printf("do you want to do anything else?\n");

      scanf("%s", &frouro);
      }while(strcmp(frouro,"add")==0 && strcmp(frouro,"end")!=0);

      return 0;
}
DevesH
  • 486
  • 4
  • 18
  • 1
    *Change `scanf(" %c", &frouro);` to `scanf("%s", &frouro);`.* And then the user enters `qoweitjglaknvoefmaafnmqvmaoijorpijfvlaksdmcvopaheakfmoaijfomlakjewoifja;vkaOIJERFOAMOGJAOMEWRVOPAWNMVM,FNZKJH`, causing a buffer overflow. – Andrew Henle Nov 24 '19 at 18:07
  • 1
    No, because the programmer himself will never be able to allocate a buffer size large enough. You also need to limit the input read by `scanf`, or better yet: [*don't use `scanf`*](https://stackoverflow.com/questions/2430303/disadvantages-of-scanf). – jamesdlin Nov 24 '19 at 18:15