0

I'm trying to read a string including spaces so scanf wouldn't work so I'm trying to use fgets. When I run it and it hits the if statement what prints on screen is:

Please enter the course name.
You entered the course: 


Please enter the course ID.

=======================

if(coursetotal==0)/*start of 1 course*/
    {

        printf("Please enter the course name.\n");
        fgets(course[0].name,sizeof(course[0].name),stdin);
        printf("You entered the course name: %s\n",course[0].name);


        printf("\nPlease enter the four digit course ID.\n");
        int temp=0,temp1=0,count=0; /*Variables used to check if 4 digits*/
        scanf("%d",&temp);
        temp1=temp;
        while(temp1!=0)
        {
            temp1/=10;
            count++;
        }
        if(count==4)/*start of is 4 digits*/
        {
            course[0].id=temp;
            coursetotal+=1;
            printf("You entered the course ID: %d\n",course[0].id);
        }/*end of is 4 digits*/
        else
        {
            printf("The course ID you input was not 4 digits.\n");
            return;
        }

        printf("You have successfully added the course: %s. The ID is : %d, and you now have a total of %d course.\n",course[0].name,course[0].id,coursetotal);

    } /*end 1 course*/
Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
  • because in starting sizeof(course[0].name) is 0 so it's not taking any character......... read the fgets() function from the following location http://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm – Jatin Khurana Mar 24 '14 at 02:45
  • 3
    You may have left a newline character in the input queue from some previous user interaction. Try calling `getchar` ahead of `fgets` as a test. The real solution depends on how you process that previous transaction. – user3386109 Mar 24 '14 at 02:46
  • @JatinKhurana How could `sizeof()` return a zero? – Lee Duhem Mar 24 '14 at 02:50
  • 1
    how about you start with checking your api function results. [The Sixth Commandment](http://www.lysator.liu.se/c/ten-commandments.html) of C programming should be taken seriously from a young age, and the most common errors in beginner-code are all-too-often attributed to the naive assumption that if something usually works, it always will. – WhozCraig Mar 24 '14 at 02:55
  • 2
    Avoid mixing `scanf()` with `fgets()`. As @user3386109, says, you have a left-over `\n` from a `scanf()` or such. Use `fgets()` and `sscanf()` or `strtol()` to read the integer. – chux - Reinstate Monica Mar 24 '14 at 03:05
  • Really? `scanf` won't read spaces? [Sorry, but it does read past spaces, you just need the right options](http://stackoverflow.com/a/13728449/1348709) – Mike Mar 24 '14 at 03:06

1 Answers1

1

First I have to address the pet peeve I see here:

I'm trying to read a string including spaces so scanf wouldn't work

That's not true at all. There's something called a negated scanset you can use it to read past the white space characters (such as space) that normally terminate scanf()s input for a string.

That said. You should really pick just one input mechanism scanf() or fgets() and use that exclusively. When you intermix, things get weird and missed. The fact that you've done it here tells me you've done it other places and you probably used scanf() prior to this leaving yourself an "unclean" stdin buffer. This will fix your issue.


Now just a quick example for you, given a int (num) and a char * (`string):

scanf("%d", &num);
fgets(string, sizeof(string), stdin);
printf("%d\n%s\n", num, string); 

You'll seemingly skip the ability to enter anything for the fgets as it really just took int the newline character leftover from the scanf()'s number entry. You'll see on the output something like:

5
5
               // <-- and a couple
              //  <-- of blank lines

Indicating that you picked up a newline character. Even more obvious if you were to look at the ASCII value of the string's first (and only) character:

printf("%d\n", string[0]);   // this would yield 10 the ASCII value of \n
Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177