Thread: Problems reading in from keyboard

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    Problems reading in from keyboard

    When running my program it is not reading anything in for the Student's name when I try to change the information in the database, I've tried several things to trying and alter the readin, but nothing seems to work.

    I'm hoping that someone might be able to point me in the right direction.

    Below is a snippet of the code I have been working on, and the porting that appears to be failing. Below that is the output that I and receiving. If you notice, it is not letting me input the name for the user.

    Code:
     char temp[50];
                            int len;
                            found = 1;
                            printf("the value of sema_set=%d\n", sema_set);
                            Wait(sema_set,1);
                            printf("Name: %s\nStudentID: %s\nAddress: %s\n",
                                    infoptr->name,infoptr->studentID,infoptr->$
                            printf("Telephone: %s\n\n", infoptr->telephone);
                            sleep(2);
                            Signal(sema_set,1);
    //WORKING
    //                      sema_set=semget(SEMA_KEY, 0,0);
                            Wait(sema_set,1);
                            printf("Enter the new name for the student.\n");
                            printf("Name: ");
    
    //                      scanf("%s", temp);
    //                      printf("%s", temp);
    
    //                      fgets(temp, 100, stdin);
                            gets("%s", temp);
    ./Load
    Database - LOAD
    Enter a file to be loaded.
    Filename: dataFile.txt
    dataFile.txt has been loaded into the database with 11 entries.
    ./Change
    Database - CHANGE
    Enter the Student ID to modify
    Student ID: 111223344
    the value of sema_set=-1078661352
    Name: Paul S Blair
    StudentID: 111223344
    Address: 3197 Trinity Rd. Lexington, KY 40533
    Telephone: 8591112234

    Enter the new name for the student.
    Name: Enter the new ID for the student.
    Student ID:

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Line 20, which you have commented out is the right idea... but your tem buffer is only 50 characters so you need to change the call to match.
    fgets() will also add the carriage return to the string so you need to get rid of it...
    Code:
    char * ptr;
    char temp[50];
    
    
    fgets(temp,49,stdin);
    do
      {
         ptr = strchr(temp,'\n');
         if (ptr)
           *ptr = 0;
      }
    while (ptr);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a string from keyboard
    By Mona777 in forum C++ Programming
    Replies: 11
    Last Post: 02-19-2009, 11:30 AM
  2. Reading an input from the keyboard
    By thetinman in forum C Programming
    Replies: 4
    Last Post: 01-21-2006, 03:31 PM
  3. keyboard problems
    By Sentaku senshi in forum Tech Board
    Replies: 7
    Last Post: 09-07-2002, 03:17 AM
  4. Replies: 4
    Last Post: 08-16-2002, 08:22 PM
  5. reading the keyboard
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 04:11 PM