Thread: Structures Introduction. Error

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    Structures Introduction. Error

    Hi, I am a newbie in C, and I have just moved to the Structures Chapter in my text. This is the first program here.
    Code:
    #include<stdio.h>
    int main() 
    {
      struct book
      {
        char name;
        float price;
        int pages;
      };
    
      struct book b1,b2,b3;
      
      printf("\nEnter names, prices, and no. of pages of 3 books.\n\n");
      
      scanf("%c %f %d",&b1.name,&b1.price,&b1.pages);
      scanf("%c %f %d",&b2.name,&b2.price,&b2.pages);
      scanf("%c %f %d",&[b3.name,&b3.price,&b3.pages);
    
      printf("\n\nAnd this is what you have entered.\n\n");
    
      printf("\n%c %f %d",b1.name,b1.price,b1.pages);
      printf("\n%c %f %d",b2.name,b2.price,b2.pages);
      printf("\n%c %f %d",b3.name,b3.price,b3.pages);
      printf("\n\n\n");
      return 0;
    }
    And the output:
    Code:
    Enter names, prices, and no. of pages of 3 books.
    
    A 100 100
    B 100 100
    
    
    And this is what you have entered.
    
    
    A 100.000000 100
    
     -0.000029 134518560
    B 100.000000 100
    The compiler(GCC here) takes in the values of only two struct variables b1 and b3. I don't think anything is wrong with my code as I have copied it from the text.

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Well, you only give it two books, but the code is scanning for three. Also, you have a typo in there

    Code:
    scanf("&#37;c %f %d",&[b3.name,&b3.price,&b3.pages);

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Ok I fixed the typo but still doesn't work, the compiler takes in only two values. It prints the output when I hit the return key after entering "B 100 100".

  4. #4
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Try this

    Code:
    #include<stdio.h>
    int main() 
    {
      struct book
      {
        char name;
        float price;
        int pages;
      };
    
      struct book b1,b2,b3;
      
      printf("\nEnter names, prices, and no. of pages of 3 books.\n\n");
      
      scanf("&#37;c %f %d\n",&b1.name,&b1.price,&b1.pages);
      scanf("%c %f %d\n",&b2.name,&b2.price,&b2.pages);
      scanf("%c %f %d",&b3.name,&b3.price,&b3.pages);
    
      printf("\n\nAnd this is what you have entered.\n\n");
    
      printf("\n%c %f %d",b1.name,b1.price,b1.pages);
      printf("\n%c %f %d",b2.name,b2.price,b2.pages);
      printf("\n%c %f %d",b3.name,b3.price,b3.pages);
      printf("\n\n\n");
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Cool. Thank You Sir, Can you explain me the whole problem and how you rectified it. Please.

  6. #6
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by shabbirhussain View Post
    Cool. Thank You Sir, Can you explain me the whole problem and how you rectified it. Please.
    C I/O is one of my weak points, but I looked at the man page of scanf then played with it a bit. I figured that not having \n messes up what it's able to match, so I added newlines to match the pattern of a single line perfectly. However, once I did that it read all the input correctly, but I was forced to input an extra character (which had no effect) after the third read. I took out the last newline and it worked

    Somebody else can probably explain what's really going on, but as I said I'm bad at C I/O. I almost always have to refer to man pages when doing anything other than printf

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Hmmm. Well I did get a picture of it. Inserting the '\n' to make sure the pattern of single line perfectly. Well Thanks dude. Will ask someone else too.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Well the reason you were getting weird results is because the %c specifier tells scanf() to read the next character... period. It doesn't ignore whitespace.

    So when you entered:

    A_100_100\n (where '_' is a space and '\n' is the newline created when you hit the enter key)

    scanf() read in the character 'A', the floating point value 100, and the decimal value 100. Unfortunately, that trailing \n got left in the input buffer. So, the next time you called scanf() and told it to read a character, it read in the newline.

    Kernel Sanders' solution works of course, but don't be fooled. Using a format string of "%c %f %d\n" won't just remove a trailing newline. If you read the scanf() documentation, any whitespace (including newlines) that you include in the format string will cause scanf() to ignore all whitespace until the next non-whitespace character. This is why, if you'll notice, he didn't include the '\n' at the end of the third format string. Try adding it and see what it does. Even after you press enter several times, scanf() will still block waiting for more input, until you type a non-whitespace character and then press enter.

  9. #9
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by arpsmack View Post
    Well the reason you were getting weird results is because the %c specifier tells scanf() to read the next character... period. It doesn't ignore whitespace.

    So when you entered:

    A_100_100\n (where '_' is a space and '\n' is the newline created when you hit the enter key)

    scanf() read in the character 'A', the floating point value 100, and the decimal value 100. Unfortunately, that trailing \n got left in the input buffer. So, the next time you called scanf() and told it to read a character, it read in the newline.

    Kernel Sanders' solution works of course, but don't be fooled. Using a format string of "%c %f %d\n" won't just remove a trailing newline. If you read the scanf() documentation, any whitespace (including newlines) that you include in the format string will cause scanf() to ignore all whitespace until the next non-whitespace character. This is why, if you'll notice, he didn't include the '\n' at the end of the third format string. Try adding it and see what it does. Even after you press enter several times, scanf() will still block waiting for more input, until you type a non-whitespace character and then press enter.
    Interesting. Is there a better solution that allows all three format strings to be identical?

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    I've never given it thought. I usually just call getchar() if I want to get rid of a trailing newline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM