Thread: Unable to get output..........

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    45

    Unable to get output..........

    Code to accept the information of 5 students and display the information of first 3 students......
    Code:
    #include<stdio.h>
    main()
    {
     struct stud
     {
      char name[20];
      int rollno;
      float per;
      char grade;
     };
     struct stud s[5];
     int i;
     printf("enter the information of 5 students\n");
     for(i=0;i<=4;i++)
     {
      scanf("%c%d%",&s[i].name,&s[i].rollno);
      scanf("%f%c%",&s[i].per,&s[i].grade);
     }
     printf("the information of first three students is\n");
     for(i=0;i<=2;i++)
     {
      printf("%c%d%\n",s[i].name,s[i].rollno);
      printf("%f%c%",s[i].per,s[i].grade);
     }
     getch();
    }
    C enlightened

  2. #2
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    Quote Originally Posted by sameertelkar View Post
    Code to accept the information of 5 students and display the information of first 3 students......
    Code:
    #include<stdio.h>
    main()
    {
     struct stud
     {
      char name[20];
      int rollno;
      float per;
      char grade;
     };
     struct stud s[5];
     int i;
     printf("enter the information of 5 students\n");
     for(i=0;i<=4;i++)
     {
      scanf("%c%d%",&s[i].name,&s[i].rollno);
      scanf("%f%c%",&s[i].per,&s[i].grade);
     }
     printf("the information of first three students is\n");
     for(i=0;i<=2;i++)
     {
      printf("%c%d%\n",s[i].name,s[i].rollno);
      printf("%f%c%",s[i].per,s[i].grade);
     }
     getch();
    }
    %c is for char you are scanning an array/string so use %s same for printf plus there is no need for three % symbol in scanf and printf.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your code has quite a few problems, given that it is a fairly small sample. And you've only asked about the FIRST!

    1) Use the %s format to read and write strings (arrays of characters, such as you might expect in the name of a student), not %c.

    2) When passing &s[i].name to scanf, lose the ampersand (not required for arrays, and changes the meaning of the code as well).

    3) When looping over an array of 5 elements, valid indices are 0 to 4, not 0 to 5. So don't loop while i <= 4 in the first loop.

    4) In each of the format strings, the last % sign is misplaced. Remove it.

    5) Be aware that the scanf() %s format stops when it encounters whitespace. If you enter Peter Jones 2, the %s will only pick up Peter, not Jones.

    6) Check the return values from scanf() to see how many arguments it has successfully read.

    7) Main returns int, so type that explicitly. Beginners, and a few too many teachers, think it is smart to leave the "int" off. They are wrong.

    7) Don't mix formatted I/O (like scanf() calls) with character oriented I/O (like getch()). scanf() often stops when it encounters whitespace, but leaves that whitespace character waiting to be read. getch() will then encounter than whitespace, and return it without pausing.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    oops,I was careless to add that meaningless % over there
    @Amitesh,I changed the scanf of name to %s but still ain't getting output
    @grumpy,can you please correct the code?I am a beginner to C and cannot comprehend the nomenclature.Thanks.
    C enlightened

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by sameertelkar View Post
    @grumpy,can you please correct the code?I am a beginner to C and cannot comprehend the nomenclature.Thanks.
    I can, obviously, or I couldn't have spotted the problems. But you will learn more by trying to fix each concern yourself. Work through the concerns, one at a time. If you don't understand the nomenclature, feel free to ask - if it is obvious you are applying a genuine effort, you might even get an answer.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    ^^^What is the meaning of typing main explicitly?Does it mean int main()?Also I omitted the ampersand and getch sign as you said,here is the updated code,I have shortened it to 3 students.......
    Code:
    #include<stdio.h>
    int main()
    {
     struct stud
     {
      char name[20];
      int rollno;
      float per;
      char grade;
     };
     struct stud s[3];
     int i;
     printf("enter the information of 3 students\n");
     for(i=0;i<=2;i++)
     {
      scanf("%s%d",s[i].name,&s[i].rollno);
      scanf("%f%c",&s[i].per,&s[i].grade);
     }
     printf("the information of three students is\n");
     for(i=0;i<=2;i++)
     {
      printf("%s%d\n",s[i].name,s[i].rollno);
      printf("%f%c",s[i].per,s[i].grade);
     }
    }
    Last edited by sameertelkar; 08-15-2013 at 08:20 AM.
    C enlightened

  7. #7
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    Quote Originally Posted by sameertelkar View Post
    ^^^What is the meaning of typing main explicitly?Does it mean int main()?
    writting explicitly means to write it fully in the code and it not being an implicit declaration.

    Code:
    for(i=0;i<=4;i++) {
      scanf("%c%d%",&s[i].name,&s[i].rollno);
    
      scanf("%f%c%",&s[i].per,&s[i].grade);
     }
    your error is basically in the scanning first of try to write code with more clarity ,like say
    Code:
    for(i=0;i<5;i++) {
    
      printf("Enter name\n");
      scanf("%s",s[i].name);
      printf("Enter roll no\n");
      scanf("%d",&s[i].rollno);
     }
    by this you can check wether each step is processing properly or not.
    Last edited by Amitesh93; 08-15-2013 at 08:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to get output..........
    By sameertelkar in forum C Programming
    Replies: 15
    Last Post: 07-18-2013, 02:58 PM
  2. Unable to get output..........
    By sameertelkar in forum C Programming
    Replies: 7
    Last Post: 07-03-2013, 10:53 AM
  3. Replies: 25
    Last Post: 12-27-2012, 11:19 PM
  4. Unable to print the output of a conversion problem
    By abhishekcoder in forum C Programming
    Replies: 1
    Last Post: 04-13-2012, 11:16 AM
  5. Seem unable to use EOF?
    By black_stallion in forum C Programming
    Replies: 18
    Last Post: 01-02-2012, 08:23 PM