Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Entering data in C

fflush

  • Please log in to reply
5 replies to this topic

#1 teensicle

teensicle

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 93 posts
  • Location:Jamaica
  • Learning:C, JavaScript, PL/SQL, Others

Posted 01 April 2012 - 01:42 PM

So i stated writing my final program for school however im at a tight spot, when ever i try entering the data that is required the program freezes, i know that i can solve this issue with "fflush(stdin)" but im told that it isn't good coding what could help? Doing the program in steps so its a bit untidy.

prob.jpg


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




void new_user();
void e_user();
void id_gen(void);


typedef struct
{
    int age; //Student Age
    char fName[10];// Student's First Name
    char lName[10];// Student's Last Name
    int stud_id; // Student's ID#
} STUDENT_REC; //Stores Basic Student Information






void id_gen ()
{


    STUDENT_REC student;


    srand(time(NULL));
    student.stud_id = rand();


    return new_user(student.stud_id); // Randomly Generates Student ID and Returns It To The New User Function


}




int main(void)
{
    int option;


    printf( "[1] - Register New User\n" );
    printf( "[2] - Existing User\n" );
    printf( "[3] - Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &option );
        fflush(stdin);


    switch (option)
    {
        case 1:            
            new_user();
            break;
        case 2:
            e_user();
            break;
        case 3:
            exit;
            break;
        default:
            printf( "Bad input, quitting!\n" );
            break;
    }
    getchar();


}


void new_user()
{
   STUDENT_REC student;


    printf("Welcome To The new User Menu\n\n\n");


    printf ("What is the first name of the user? ");
        scanf("%c", &student.fName[10]);




    printf ("\nWhat is the last name of the user? ");
        scanf("%c", &student.lName[10]);




    printf ("\nWhat is the age of the user? ");
        scanf("%d", &student.age);
            fflush(stdin);


    id_gen();


    printf("Your ID# is: %d", student.stud_id);


}
void e_user()
{
    printf( "Load game called" );
}





Edited by teensicle, 01 April 2012 - 05:52 PM.

  • 0
PePe===> BLACKSPADE EMPIRE<===PePe

www.blackspade-ent.com

#2 Talimere

Talimere

    CC Newcomer

  • Member
  • PipPip
  • 12 posts

Posted 01 April 2012 - 04:20 PM

I'm quite new to c, but have you tried: scanf("%d", &option);
  • 0

#3 teensicle

teensicle

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 93 posts
  • Location:Jamaica
  • Learning:C, JavaScript, PL/SQL, Others

Posted 01 April 2012 - 05:53 PM

not too sure how that happened but thats what i had before thanks :)
  • 0
PePe===> BLACKSPADE EMPIRE<===PePe

www.blackspade-ent.com

#4 Talimere

Talimere

    CC Newcomer

  • Member
  • PipPip
  • 12 posts

Posted 01 April 2012 - 07:22 PM

It also looks like you have a "getchar()" there, doing nothing. ;)
  • 0

#5 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 02 April 2012 - 01:51 PM

scanf("%c", &student.fName[10]);
This will only read 1 character, and you're storing it outside your char array. Remember, indexes go from 0 to n-1. Names are (usually) more than 1 character, so you will probably want to use %s format specifier.

case 3:
	   exit;
	   break;
You probably meant function call; you're mission parenthesis and return code.

fflush(stdin);
This causes undefined behaviour and you can learn why here.
  • 0

The roots of education are bitter, but the fruit is sweet.


#6 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 02 April 2012 - 02:01 PM

Here's how I like to simplify console reading in C, to avoid having to flush the input or include a dummy getchar() to remove the newline left in the buffer after a read:
  • Declare a 'buffer' char* around 4k in size or so. This will be where I copy the user's input while working on it.
  • When it comes time to read from the console, just use fgets(buffer, 4096, stdin) to grab a whole line at a time. This gets everything out of the stream: newlines, extra input, trailing spaces, etc. Now that the stream is clear, I can begin to work on the input from my buffer.
  • Instead of using scanf() on the standard input stream, I use sscanf() and read from my buffer string.
This makes all console reads cleaner by only using one unified method for every read. The behavior is predictable and everything gets cleared out of the stream on each read, eliminating the need for me to manually double check this.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: fflush

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download