Like Tree2Likes
  • 1 Post By c99tutorial
  • 1 Post By c99tutorial

stuck with scanf %s

This is a discussion on stuck with scanf %s within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> char rent(char car); char terms(char read); int choice; int main() { int car_type, selection,contact; char mainmenu,read; char ...

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

    stuck with scanf %s

    Code:
    #include <stdio.h>
    
    char rent(char car);
    char terms(char read);
    int choice;
    
    int main()
    {
          int car_type, selection,contact;
          char mainmenu,read;
          char name[50],address[100];
          
          do{
          printf("\n\n\t\t\t\t*** ABCDEFG RENT-A-CAR ***\n\n\n");
          printf("\n\n\t\t1 - Price List\n\n\t\t2 - RENT-A-CAR\n\n\t\t3 - TERMS (Please READ!!!)");
          printf("\n\n\tEnter ( 1 - 5 ) for you selection: ");
          scanf("%d", &selection);
          if(selection == 1){
                       system("cls");
                       printf("\n\n\t\t\t\t*** Price List ***\n\n\n");
                       printf("\tCar Type\t\tPrice per Day\t\tPrice per Week\n\n");
                       printf("\tSedan\t\t\t1,500\t\t\t10,500\n");
                       printf("\tSUV\t\t\t2,700\t\t\t18,900\n");
                       printf("\tVAN\t\t\t2,500\t\t\t17,500\n");
                       printf("\tPick-Up\t\t\t2,000\t\t\t14,000\n");
                       printf("\n\t* A .02%% discount is given if renting for 1 week.\n");}
          else if(selection == 2) {
                       system("cls");     
                       printf("\n\n\t\t1 - Sedan\n\t\t2 - SUV\n\t\t3 - VAN\n\t\t4 - Pick-Up\n\t\t");
                       printf("\n\tCar of your choice (1-4): ");
                       scanf("%d",&choice);
                       printf("\n\nPlease provide the information below...");
                       printf("\n\nName: ");
                       scanf("%s",&name);
                       printf("\nCurrent address: ");
                       scanf("%s",&address);
                       printf("\nContact number: ");
                       scanf("%d",&contact);
                       rent(choice); }
          else if(selection == 3) {
                       terms(read);}
          else {}
                       printf("\n\nGo back to main menu (Y / N): ");
                       scanf("  %c",&mainmenu);
                       system("cls");
          }
          while(mainmenu=='y' || mainmenu=='Y');     
          printf("\n\n\n\n\t\t\tThank you... Drive safely!!!\n\n\n");
          
          
          
          system("pause");
    }
    
    char rent(char car)
    {
         if(choice==1){    
         
         printf("\nYour car of choice: Sedan\n\n");
         system("pause");
         }
    }
    char terms(char read)
    {
         
         printf("\n\n\n\t\t\t\t ABCDEFG Rental TERMS and CONDITIONS");
         printf("\n\t1. Fuel");
         printf("\n\n\t\tAll vehicles are supplied with a full tank of fuel at the time ");
         printf("\n\t\tof rental.");
         printf("\n\n\t\tRenters must return the vehicle with a full tank at the end of ");
         printf("\n\t\tthe rental.");
         printf("\n\n\t2. Rental Responsibility");
         printf("\n\n\t\tABCDEFG Rent-A-Car holds the renter responsible for the safety ");
         printf("\n\t\tof the vehicle until the time the vehicle is returned.");
         printf("\n\n\t\tA renter maybe held responsible for the loss of the vehicle, ");
         printf("\n\t\tvehicle damaged in an accident, or any damages the vehicle may ");
         printf("\n\t\tsustain during the rental period.");
         printf("\n\n\t3.Driver's License and Valid ID");
         printf("\n\n\t\tRenter must present a valid Driver's License (Professional or ");
         printf("\n\t\tNon-Professional)and a valid ID(SSS/Passport/and any Government");
         printf("\n\t\tissued ID)\n\n");
         system("pause");
         }
    Code is still under construction...just a basic c program
    I wanted a user to input some basic info, but having problems using scanf("%s"), any hints would be much appreciated.

    thank you.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    You should compile with warnings. It will tell you about these problems:

    Code:
    printf("\n\nName: ");
    scanf("%s",&name);
    printf("\nCurrent address: ");
    scanf("%s",&address);
    printf("\nContact number: ");
    scanf("%d",&contact);
    Take the & off name and address.

    Other problem you'll have is that you have the loop `do...while(mainmenu=='y' || mainmenu=='Y');', but you aren't necessarily giving mainmenu an initial value. For example this only happens if the user enters something other than 1...3 in the menu.

    Other problem is that you're using system as part of your program logic. Sometimes people use system("pause") as a debugging tool at the end, but using it in the logic of your program is a bad idea. Use the following functions instead:

    Code:
    void pause(void) {
        printf("Press enter to continue...");
        int c;
        while ((c = getchar()) != '\n' && c != EOF)
            ;
    }
    
    void clear(void) {
        printf("\n\
    -------------------------------------------------------------------------\n");
    }

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    and for name, and other information where there will be spaces, i would use the following

    Code:
    fgets (name, 50, stdin);
    where name in the variable, 50 is the max characters


    //edit//

    also have phone number as int, may cause probs if user uses () for area code, or - between the numbers.
    Last edited by Crossfire; 01-20-2013 at 02:09 AM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    i tried fgets but it skips directly to address

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Look at the first part of your code:

    scanf("%d", &selection);

    Suppose you type in " 123\n", where \n represents you pressing return.
    After the call is done, there is the following in the input buffer:

    "\n"

    So if you do fgets at that point, it will just read the blank line.

    As solutions you could use fgets in all situations and then use sscanf on the resulting string buffer, or you can clear out the input buffer.For an example to clear out the input buffer, look at my previous post above.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    I resolve my first issue, thanks for the help guys.

    Now, I wanted to put an option under Selection 2, like a 5th option, where it will only go back to choosing a vehicle, not entirely go back to main menu. Do I have to create a do, while, for that?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    These are your choices:

    1) A bit of a hack, and frowned on for good reason, but you could use a line label just before the start of choosing the vehicle, and then have an
    Code:
    if(youHateTheCar)
       goto lineLabel;
    In rare cases this is a very intuitive bit of code - but don't make it a habit. Some teachers and bosses won't accept it

    2) You could put the car entry into it's own function, then just use something like:
    Code:
    do {
       carEntry();
    }while(youHateTheCar);
    3) You have a do while loop, for the car entry, inside the rest of the entry details.

    So, short answer - yes, you have to have a logical loop back. As per norm, there is more than one way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Plz help stuck
    By kennny2004 in forum C++ Programming
    Replies: 7
    Last Post: 09-27-2005, 01:27 AM
  2. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  3. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  4. help me please, im stuck
    By Leeman_s in forum Game Programming
    Replies: 2
    Last Post: 04-19-2002, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21