Thread: Error displaying all the infos entered by the user

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    5

    Error displaying all the infos entered by the user

    Code:
    #include<stdio.h>
    
    
    main()
    {
          int item_code, quantity;
          char ans, y;
          
          //Display Welcome
          printf("\t\t\t\t***Welcome Guest***");
          printf("\n\nPlease seletct your order in the list of items below: ");
          printf("\n\nPrice \t\tItem Code \t\tName of Item");
          printf("\n\n349.00 \t\t   1 \t\t\tDream Pillow");
          printf("\n\n349.00 \t\t   2 \t\t\tBolster Pillow");
          printf("\n\n549.00 \t\t   3 \t\t\tFantasy Pillow");
          printf("\n\n599.00 \t\t   4 \t\t\tHealth Pillow");
          
          //Enter item Code 
          1;
          printf("\n\n\nEnter the item code: ");
          scanf("%d", &item_code);
          
           if ((item_code>0)&&(item_code<5)){
              
           
               printf("\nEnter Item Quantity: ");
               scanf("%d", &quantity);}
               
                else {
                printf("Invalid Input!");
                getch();
                exit(0);
                }
           //Prompt Do you wish to continue
               printf("\n\nDo you wish to continue?: y/n\n");
               scanf("%s", &ans);
                
               if (ans=='y')
               
                             
                  { system("cls"); } 
                   
                 else  
               
               if (ans=='n') 
                  
                  { exit(0);}
                  
                 else 
                   
                  { printf("\nInvalid Input! program will now exit");
                  exit(0);} 
                           
           //Display Personal Info                   
                   char name, address, pcode, contact, e_add;
                   char mop, date, a;
                   
                   printf("Please fill up your personal information");
                   printf("\n\nComplete Name: ");
                   scanf("%s", &name);
                   printf("Complete Address: ");
                   scanf("%s", &address);
                   printf("Postal Code: ");
                   scanf("%s", &pcode);
                   printf("Contact No: ");
                   scanf("%s", &contact);
                   printf("E-mail address: ");
                   scanf("%s", &e_add);
           
          // Enter Code
                  
                   printf("\n*********************************************");
                   printf("\n\nMode of Payment \tCode");
                   printf("\n\nBank Deposit \t\tA");
                   printf("\nMoney Transfer \t\tB");
                   printf("\n\n*********************************************");
                   printf("\nEnter the code of your chosen mode of payment:\n");
                   scanf("%s", &mop);
                   
                      if((mop=='A')||(mop=='a')){
                      
                      printf("\nMode of Payment: Bank Deposit");}
                      
                      else
                      
                      if((mop=='B')||(mop=='b')){
                                  
                      printf("\nMode of Payment: Money Transfer");}
                      
                      
                      else
                      {
                      printf("Invalid Input!");
                      getch();
                      exit(0);}
                   
                   
                   printf("\n\nDate of Payment: ");
                   scanf("%s", &date);   
                
                
                   
           // Prompt do you wish to continue
                    printf("\nDo you wish to continue?: y/n\n");
               scanf("%s", &ans);
                
               if (ans=='y')
               
                             
                  { system("cls"); } 
                   
                 else  
               
               if (ans=='n') 
                  
                  { exit(0);}
                  
                 else 
                   
                   { printf("\nInvalid Input! program will now exit");
                  exit(0);} 
                  
           
           //Display Claim Order
          char code;
          
          printf("How to claim your order \tCode");
          printf("\n\nPick-up \t\t\tA");
          printf("\nDelivery \t\t\tB");
    
    
           //Enter DeliveryCode
          printf("\n\nEnter code here: ");
          scanf("%s", &code);
          
                      if((code=='A')||(code=='a')){
                      
                      printf("\nOrder by: Pick-up");}
                      
                      else
                      
                      if((code=='B')||(code=='b')){
                                  
                      printf("\nOrder by: Delivery");}
                      
                      
                      else
                      {
                      printf("Invalid Input!");
                      getch();
                      exit(0);}
                   
           
           // Prompt do you wish to continue
            printf("\n\nDo you wish to continue?: y/n\n");
               scanf("%s", &ans);
                
               if (ans=='y')
               
                             
                  { system("cls"); } 
                   
                 else  
               
               if (ans=='n') 
                  
                  { exit(0);}
                  
                 else 
                   
                    { printf("\nInvalid Input! program will now exit");
                  exit(0);} 
                             
           
          printf("\n________________________________________________________________________________");
          printf("\n\t\t\t\tORDER VERIFICATION\n");
          printf("\n\nOrdered item/s: %d", item_code);
           //Prompt 1 or 2
          
          
          printf("Complete name: %s",name);
          printf("Complete Address: %s\n",address);
          printf("Postal Code: %s\n",pcode);
          printf("Contact Number: %s\n",contact);
          printf("E-mail Address: %s\n",e_add);
    
    
           //Display Merchant Details
          printf("\n________________________________________________________________________________");
          printf("\nPlease copy the merchant's details for sending your payments  \n");
          printf("\nFor Bank Deposit: \n");
          printf("\tAccount name: Chin Go\n\tAccount number: 4444-8888-48\n\tName of bank: Banco de Oro\n");
          printf("\n\tFor Money Transfer: \n");
          printf("\tName: Chin Go\tContact No.: 888-8888\tAddress: Blk-88 Lot -01 Lucky Village, Pasig City\n");
          printf("\n________________________________________________________________________________");
          
          
           
           // Press any key 
           
           //Display tranasaction complete
           
          
          getch();
          exit(0);
                     
          getch();
    
    
    }
    Last edited by primeG; 03-04-2013 at 07:35 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Matticus View Post
    Welcome to the forum. Post whatever attempt you have made (be sure to use code tags), and we can assist you from there.
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    char name, address, pcode, contact, e_add;
    You are trying to put a C-String in nearly all of the above variables.
    They can ONLY hold a single char.

    I suggest doing this instead

    Add near top of program.
    Code:
    #define MAX_NAME_SIZE 128
    Code:
    char name[MAX_NAME_SIZE];
    Name is now a array and you do not need & symbol to get array address.
    Code:
    scanf("%s", name);
    You need to fix all of the variables.

    Tim S.

    PS: I stopped looking after finding this mistake; you could and likely do have several more.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The indentation needs work.

    The program lacks any structure - 200+ line main and nothing else. Use some functions!

    Abuse of %s in scanf calls leads to buffer overruns.
    Code:
                   char name, address, pcode, contact, e_add;
                   char mop, date, a;
                    
                   printf("Please fill up your personal information");
                   printf("\n\nComplete Name: ");
                   scanf("%s", &name);
                   printf("Complete Address: ");
                   scanf("%s", &address);
    Most (if not all, you check for yourself) of your single chars need to be char arrays.

    Eg.
    char name[50];
    scanf("%s", name);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    5
    Thanks..I'll try to fix it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error while counting the number of words entered by user
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 05-04-2012, 03:13 AM
  2. turns the name entered by the user
    By Magic-Man-fci in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2011, 12:14 AM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  4. How to hide what a user has entered with *'s
    By stuartg123 in forum C++ Programming
    Replies: 14
    Last Post: 04-16-2007, 01:29 PM
  5. use user entered formula to calculate something
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 12-07-2001, 02:21 AM