Thread: plz help me out......the code is correct but it's not running well.....

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    plz help me out......the code is correct but it's not running well.....

    hi
    could u plz help me out with this array program that i have written ....there is NO error in this program while compiling...but it's not running well .the output is not coming our right as it should.i don't know why........i have checked it 100 times and i still can't figure out why is it not running well.........?
    this program is about.....A TEST CONSISTING OF 8 MULTIPLE CHOICE ITEMS(QUESTIONS) IS ADMINISTERED TO A BATCH OF 3 STUDENTS.
    so i have done like this:-
    Code:
    #include<stdio.h>
    #include<conio.h>
    #define STUDENTS 3
    #define ITEMS 8
    void main()
    {
      char key[ITEMS+1],response[ITEMS+1];
    int i,student,count,n,correct[ITEMS+1];
    /* reading of correct answers into an array*/
    printf("input key to the items");
    for(i=0;i<ITEMS;i++)
    scanf("%c",&key[i]);
    /*evaluation begins*/
    for(student=1;student<=STUDENTS;student++)
    {
    /*reading student responses and counting correct ones*/
    count=0;
    printf("input response of student:%d",student);
    for(i=0;i<ITEMS;i++)
    scanf("%c",&response[i]);
    for(i=0;i<ITEMS;i++)
    correct[i]=0;
    for(i=0;i<ITEMS;i++)
    if(response[i]==key[i])
    {
    count=count+1;
    correct[i]=1;
    }
    /*printing of results*/
    printf("student:%d",student)
    printf("score is %d out of %d\n",count,ITEMS);
    printf("response to the following items are wrong\n");
    n=0;
    for(i=0;i<ITEMS;i++)
    if(correct[i]==0)
    {
    printf("%d",i+1);
    n=n+1;
    }
    if(n==0)
    {
    printf("nil\n");
    }
    getch();
    }
    Last edited by Salem; 10-18-2004 at 12:42 PM. Reason: Code tags added by a miffed Salem - but it didn't do any good, there is no indentation to start with!!!

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    Yeah, this is kinda a tough one, but don't let it get to ya because its one of those things that really isn't a programmers fault

    after each scanf() loop, put a fflush(stdin)

    for(....)
    scanf(...);
    fflush(stdin);

    Let me know this helps,

    Ryan

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    With a 16 th post you should know how to use tags!!!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    hi ryan
    i have tries using fflush(stdin)here but still it is not running well..........
    when i type abcdba in the :Input to key items//
    and then the :



    Response of a student for example.....
    abcdab ,
    then it should print score is 4 out of 6.....and the response to the items below are wrong:
    5
    6
    isn't it?but it's not doing that at all........
    so plz help me out making it run successfully.........plz

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    plz help me out guys!!!!!!!!!!with this code////////what 's the problem in that code anyway?
    it's absolutely right as far as compiling is concerned.........it's just that it's not running well...........plz help me out...........plz

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by PrimeTime00
    after each scanf() loop, put a fflush(stdin)
    Please visit the FAQ where you can find Why fflush(stdin) is wrong.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    And please dont post twice .

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by galmca
    plz help me out guys!!!!!!!!!!with this code////////what 's the problem in that code anyway?
    it's absolutely right as far as compiling is concerned.........it's just that it's not running well...........plz help me out...........plz

    quit begging....geez
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this (changes to the original have been commented).
    Code:
    #include<stdio.h>
    #include<conio.h>
    #define STUDENTS 3
    #define ITEMS 8
    //void main()
    int main(void)
    {
       char key[ITEMS+1],response[ITEMS+1];
       int i,student,count,n,correct[ITEMS+1];
       /* reading of correct answers into an array*/
       printf("input key to the items");
       for(i=0;i<ITEMS;i++)
          scanf("%c",&key[i]);
       for(i=0;i<ITEMS;i++)
          printf("key[%d]=%c\n",i,key[i]);
       while (getchar() != '\n');
       /*evaluation begins*/
       for(student=1;student<=STUDENTS;student++)
       {
          /*reading student responses and counting correct ones*/
          count=0;
          printf("input response of student:%d",student);
          for(i=0;i<ITEMS;i++)
             scanf("%c",&response[i]);
          for(i=0;i<ITEMS;i++)
             correct[i]=0;
          for(i=0;i<ITEMS;i++)
             if(response[i]==key[i])
             {
                count=count+1;
                correct[i]=1;
             }
          /*printing of results*/
          printf("student:%d",student);
          printf("score is %d out of %d\n",count,ITEMS);
          printf("response to the following items are wrong\n");
          n=0;
          for(i=0;i<ITEMS;i++)
             if(correct[i]==0)
             {
                printf("%d",i+1);
                n=n+1;
             }
          if(n==0)
          {
             printf("nil\n");
          }
          //getch();
          getchar();
       }
       getchar();
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plz help wid this tic tac toe code
    By sudoku in forum C Programming
    Replies: 2
    Last Post: 10-27-2008, 02:15 PM
  2. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Is this code correct?
    By Sridar in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2004, 03:45 PM