Thread: How to do the option for Yes and No?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    How to do the option for Yes and No?

    Hello. I’m new to C-programming. Can anyone help me out? I can’t find the code to solve this-

    • Display a WELCOME MENU.
    • Ask the user if they would like to continue. ‘Y’ to continue and ‘N’ to stop.
    • As long as the choice is ‘Y’ then :

    o Ask the user to enter the customer’s name.
    o Ask the user to enter the weight of the groceries.


    Code:
    #include<stdio.h>
    #include<string.h>
     
    
    void main()
    
    {
         
        char customer_name[30] , ans;
        float weight;
         
        
        printf("\n***********************************************");
        printf("\\n*         WELCOME TO THE GROCERIES SHOP             *");
        printf("\n***********************************************");
         
      
        printf("Would you like to continue (Y/N)?");
        scanf("%c", ans);
        
        if(ans == 'y' || ans == 'Y')
        {
        printf("\nEnter customer’s name: ");
        gets(customer_name);
        printf("\nEnter weight of groceries[KG]: ");
        scanf("f" ,&weight);
        }    
        
        else(ans == 'n' || ans == 'N');
        break;
       {
     
        
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on whether you want to require 'y', 'Y', 'n' and 'N' as valid input. Either way, you would use at least one loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    use a while loop

    Code:
      printf("Would you like to continue (Y/N)?");
      scanf("%c", &ans);
    
      while (ans == 'y' || ans == 'Y'){
        /*code to be repeted*/
        
        printf("Would you like to continue (Y/N)?");
        scanf("%c", &ans);
      }
    edit:

    also
    Code:
        printf("\\n*         WELCOME TO THE GROCERIES SHOP             *");
    will output
    Code:
    "\n*         WELCOME TO THE GROCERIES SHOP             *"
    you have a extra '\'


    (from post below mine)
    Quote Originally Posted by itCbitC View Post
    scanf() requires pointers to the conversion format.
    the '&' symbol in front of the variable (you used it inside the if statement but left it out when you asked for ans)
    Last edited by ಠ_ಠ; 03-14-2009 at 12:21 AM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    scanf() requires pointers to the conversion format.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf("%c"

    will leave \n char in the buffer, which will be read by gets

    you need to clear the buffer before reading string - read FAQ how

    And while you'll be there - read why you should not use gets

    And why you should not use void main
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    I cannot get gets() to work in while loop, any get arounds?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Vast87 View Post
    I cannot get gets() to work in while loop, any get arounds?
    do not use gets - use fgets

    do not mix it with scanf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Code:
    #include <stdio.h>
    
    int main()
    {
    	
    	int choice;
    	char name[30];
    
    	printf("Would you like to continue? Type 0 to stop or 1 to continue: ");
    	scanf("%d", &choice);
    
    	while(choice == 1)
    	{
    		printf("Enter customer name: ");
    		gets(name);	
    	
    	fflush(stdin);
    	printf("Would you like to continue? Type 0 to stop or 1 to continue: ");
    	scanf("%d", &choice);
    	}
    	printf("Customer Name: %s", name);
    
    getche();
    }
    How to enter customer name?

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    
    void myflush(void);
    
    int main()
    {
    	
    	int choice;
    	char name[30];
    
    	printf("Would you like to continue? Type 0 to stop or 1 to continue: ");
    	;
    
    	while(scanf("%d", &choice) == 1 && choice == 1)
    	{
    		printf("Enter customer name: ");
    		scanf(" %29[^\n]",name);	
    		printf("Customer Name: <%s>\n", name);
    		myflush(); /* if name is longer then buffer - need to get rid of the rest of chars */
    
    		printf("Would you like to continue? Type 0 to stop or 1 to continue: ");
    	}
    
    }
    
    void myflush(void)
    {
    	int c;
    	while((c=getchar()) != EOF && c != '\n');
    }
    fflush for stdin undefined - read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    Thank you for your help people. I think i manage to do the YES and NO.
    but there is some questions -

    1- how can i display total customer?
    2- where should i put the fflush?


    thank you again

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    void main()
    {
         char customer_name[30], x, y, n, choice;
         float weight, price, payment, total =0;
        
        
        
            printf("\n********************************");
            printf("\n**  WELCOME TO GROCERIES SHOP   **");
            printf("\n********************************");
        
         
             x=1;
             while(x<=3)
            {
               printf("\nWould you like to continue [Y/N] : ");
               scanf("%c",&choice);
            
             while(choice=='y' || choice=='Y')
                  {
                     printf("\nEnter customer name: ");
                     scanf("%s",&customer_name);
                     
                     printf("\nEnter weight [KG]    :  ");
                     scanf("%f",&weight);
                    
            
                     printf("\n-------------------------------");
                     printf("\n-     BILLING RECEIPT         -");
                     printf("\n-------------------------------");
                    
                     printf("\nCustomer name     :%s",&customer_name);
                     printf("\nLaundy weight [KG]:%.2f",weight);
                     
                       //nested if
                      
                   if (weight<=3.0)
                        {
                         printf("\nPrice equals to :$ 1.20");
                         price==1.20;
                         }
                   
                   else if (weight>3.0 && weight<6.0)
                         {
                         printf("\nPrice equals to :$ 1.00");
                         price==1.00;
                         }
                        
                    else 
                        {
                        printf("\nPrice equals to  :$ 0.80");
                        price==0.80;
                        }
                       
                                     
                       payment = price * weight;
                       
                       printf("\n-------------------------------");
                       printf("\n-    TODAY'S SUMMARY          -");
                       printf("\n-------------------------------");
                       printf("\nPayment :%.2f"); //why can't i put ,payment at the end?
                       //How can i display Total Customer? 
                
                     printf("\nWould you like to continue [Y/N] : ");
                     scanf("%c",&choice);
                      
                   
                       
                   x++ ;
                    }
                  
                     
                        printf("\n----------------------------");
                        printf("\n-       Thank you          -");
                        printf("\n----------------------------");
                       
                   
                   
                   
             }
             getch();
         
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by karipap View Post
    Thank you for your help people. I think i manage to do the YES and NO.
    but there is some questions -

    1- how can i display total customer?
    2- where should i put the fflush?


    thank you again
    What is the purpose of the variable x?

    The customer can continue to shop as long as they want, right?

    We need a touch up, here.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would change it to something like this:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    int main()
    {
         char customer_name[30], x, y, n, choice;
         float weight, price, payment, total =0;
        
        
         do 
              {
        
              printf("\n********************************");
              printf("\n**  WELCOME TO GROCERIES SHOP   **");
              printf("\n********************************");
        
         
              printf("\nEnter customer name: ");
              scanf(" %s",&customer_name);
                 
              printf("\nEnter weight [KG]    :  ");
              scanf(" %f",&weight);
                    
            
              printf("\n-------------------------------");
              printf("\n-     BILLING RECEIPT         -");
              printf("\n-------------------------------");
                    
              printf("\nCustomer name     :%s", customer_name);
              printf("\nLaundy weight [KG]:%.2f",weight);
                     
              //nested if
                      
              if (weight<=3.0)
                 {
                 printf("\nPrice equals to :$ 1.20");
                 price==1.20;
                 }
                   
              else if (weight>3.0 && weight<6.0)
                 {
                 printf("\nPrice equals to :$ 1.00");
                 price==1.00;
                 }
                        
              else 
                 {
                 printf("\nPrice equals to  :$ 0.80");
                 price==0.80;
                 }
                       
              payment = price * weight;
              total += payment;
              payment = 0;
                       
                
              printf("\nWould you like to continue [Y/N] : ");
              scanf(" %c",&choice);
         }while(choice=='y' || choice=='Y')
     
         printf("\n-------------------------------");
         printf("\n-    TODAY'S SUMMARY          -");
         printf("\n-------------------------------");
         printf("\nTotal:%.2f" total); 
                    
         printf("\n----------------------------");
         printf("\n-       Thank you          -");
         printf("\n----------------------------");
                       
                   
         getch();
         return 0;
    }
    Last edited by Adak; 03-16-2009 at 10:24 AM.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    You are right Adak. I think the variable x should be out. I did it base on the output example that shown only 3 customers.

    I noticed there's something wrong -
    The 'Thank You' will appear 2 times.

    and also there is no condition for 'N==NO'

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What would no mean, to a customer? Early exit before going through the do loop?

    My "Thank You" is outside the do while loop. You will only see it once.

    Oh yeah. No fflush is needed in this version.
    Last edited by Adak; 03-16-2009 at 10:42 AM.

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    Adak - The Yes No at the start is to ask and check whether there is any customer at that time. Thank you Adak for helping me out!

    This is the latest code i've been working on. Everything looks ok, but if the user enter name that have 2 or more such as - Jackie Chan or Edward Scissor Hands , the Weight per [KG] will be skip.

    No problem for single name such as Jack, Donald, Fatimah.

    How can i fix this?

    thank you




    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    void main()
    {
         char customer_name[30], y, n, choice;
         float weight, price, payment, total=0;
         int customer=0;
        
             printf("\n******************************************");
             printf("\n**        WELCOME TO GROCERIES SHOP      **");
             printf("\n******************************************");
             
            
             printf("\nWould you like to continue? [Y/N]:  ");
             scanf("%c",&choice);   
             
             while(choice=='y' || choice=='Y')
             
              {
                  
             printf("\nEnter customer name              :  ");
             scanf("%s",&customer_name);
             printf("\nEnter weight [KG]                :  ");
             scanf("%f",&weight); 
                    
            
             printf("\n------------------------------------------");
             printf("\n-            BILLING RECEIPT             -");
             printf("\n------------------------------------------");
                    
             printf("\nCustomer name                    : %s  ",&customer_name);
             printf("\nLaundy weight [KG]               : %.2f",weight);
                    
                                   
             //nested if
                      
             if (weight<=3.0)
             {
             printf("\nPrice per [KG]                   :$ 1.20");
             price=1.20;
             }
                   
             else if (weight>3.0 && weight<6.0)
             {
             printf("\nPrice per [KG]                   :$ 1.00");
             price=1.00;
             }
                        
             else 
             {
             printf("\nPrice per [KG]                   :$ 0.80");
             price=0.80;
             }
                       
             payment = price*weight;
             total = total + payment;
             
             printf("\nPayment                          :$ %.2f ",total);
                           
            
            
             fflush(stdin);
             printf("\nWould you like to continue [Y/N] : ");
             scanf("%c",&choice);
             
             customer++;
             
                     
             
          }
             printf("\n------------------------------------------");
             printf("\n-            TODAY'S SUMMARY             -");
             printf("\n------------------------------------------");
             printf("\nTotal Payment  :$ %.2f " ,total); 
             printf("\nTotal Customer : %d  " ,customer); 
                    
             printf("\n------------------------------------------");
             printf("\n-               THANK YOU                -");
             printf("\n------------------------------------------");
                       
                   
          getch();
         
    }
    Last edited by karipap; 03-17-2009 at 08:14 AM.

Popular pages Recent additions subscribe to a feed