Program Error

This is a discussion on Program Error within the C Programming forums, part of the General Programming Boards category; I used to make programs in C before but i got a little addicted into web design now im trying ...

  1. #1
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204

    Program Error

    I used to make programs in C before but i got a little addicted into web design now im trying to relearn/remember the stuffs i knew before by making simple dos apps... I can feel my mind working again with C and h8 the feeling LOL .. here i tried making 2 programs one was where i input all the quizzes, the second is the quiz program itself. Whenever i compile/run this program using MVC++ at the end of the program i get this Dialog box saying Some problem Encountered.. Can someone tell me what i did wrong here? I didnt get any Errors or warning by the way.


    PHP Code:
             #include<stdio.h>
        #include<time.h>
        #include<stdlib.h>
        #include<string.h>

        #define MAX_QUIZ 5
        #define MAX_ARR 100

        
    typedef struct quiz {
            
    char question[MAX_ARR];
            
    char answer[MAX_ARR];
        }  
    Quiz;

        
    int initQuizQuiz * );



        
    int main()
        {
            
    Quiz quiz[MAX_QUIZ] = { """" };
            
    int correct 0wrong 0qindexquizCtr 0;
            
    int answer;
            
    char inputText[MAX_ARR] = {0};

            
    srandtimeNULL ) );

            if( !
    initQuizquiz ) )
                
    printf"Error: Quiz Initialization Failed !" );
            else {

                for( 
    quizCtr 0quizCtr != MAX_QUIZquizCtr++ ) 
                {
                    
    qindex rand() % MAX_QUIZ;
                    
    printf"%s\nAnswer: "quiz[qindex].question );
                    
    getsinputText );
                    
    answer strnicmpinputText,
                    
    quiz[qindex].answer,
                    
    strlenquiz[qindex].answer ) - );

                    switch( 
    answer ) {

                        case 
    0:
                            
    correct++;
                            break;

                        default:
                            
    wrong++;
                            break;
                    }

                }
            }

            
    printf"Total Questions: %d\nCorrects: %d\nWrongs: %d"
            
    MAX_QUIZcorrectwrong );
            return 
    0;
        }



        
    int initQuizQuiz *)
        {
            
    FILE *file NULL;
            
    int ctr 0;
            
    char lineStr[MAX_ARR] = {0};

            if( !( 
    file fopen"quiz.txt""r" ) ) )
                    return 
    0;
            else {

                 while( 

                 {
                    
    fgetslineStrsizeoflineStr ), file );
                    if( 
    feoffile ) ) break;
                    
    sprintfq[ctr].questionstrtoklineStr"::" ) );
                    
    sprintfq[ctr].answerstrtokNULL"::" ) );
                    
    ctr++;
                 }

          }
            
    fclosefile );
            return 
    1;
        } 
    Last edited by loko; 07-02-2005 at 08:12 AM. Reason: trouble attaching file

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    This...
    Code:
    gets( inputText );
    ...is bad. Very bad.

    See this thread for some explantations about gets() throughout.

    Code:
    answer = strnicmp( inputText,... /* Did you #include the proper header for strnicmp()? */
    Last edited by kermit; 07-02-2005 at 08:39 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > gets( inputText );
    Never use gets(), see the FAQ

    > while( 1 )
    > {
    > fgets( lineStr, sizeof( lineStr ), file );
    > if( feof( file ) ) break;
    This should really be written as
    Code:
    while ( ctr < MAX_QUIZ &&
            fgets( lineStr, sizeof( lineStr ), file ) != NULL ) {
    > sprintf( q[ctr].question, strtok( lineStr, "::" ) );
    > sprintf( q[ctr].answer, strtok( NULL, "::" ) );
    1. You should really check that strtok() returned non-NULL before using the return value.
    2. "::" is NOT pairs of colons, it's only a single ":"

    > strlen( quiz[qindex].answer ) - 0
    I'm not sure what you're hoping to acheive here by subtracting zero...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Salem
    > gets( inputText );
    Never use gets(), see the FAQ

    > while( 1 )
    > {
    > fgets( lineStr, sizeof( lineStr ), file );
    > if( feof( file ) ) break;
    This should really be written as
    Code:
    while ( ctr < MAX_QUIZ &&
            fgets( lineStr, sizeof( lineStr ), file ) != NULL ) {
    > sprintf( q[ctr].question, strtok( lineStr, "::" ) );
    > sprintf( q[ctr].answer, strtok( NULL, "::" ) );
    1. You should really check that strtok() returned non-NULL before using the return value.
    2. "::" is NOT pairs of colons, it's only a single ":"

    > strlen( quiz[qindex].answer ) - 0
    I'm not sure what you're hoping to acheive here by subtracting zero...
    the 0 is really 1. Im trying to not include the "\n" at the end of the string in the comparison. So what do i use aside from gets? whenever i use fscanf or scanf whenever it encounters a terminating chars it just stop even with spaces.

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    i tried using fgets instead of gets still the same i got an abnormal termination and an error dialog box at the end of the program.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Code:
    sprintf( q[ctr].answer, strtok( NULL, "::" ) );
    I'd start by commenting this line out. It seems the most likely area for concern. Then you can figure out what the problem is, probably some formatting oversight in quiz.txt

    Regards,
    Brian

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Can you attach a copy of the text file you're using - quiz.txt
    Or at least paste a few lines from it here (paste it in [code][/code] tags)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Heres Text File
    PHP Code:
    What is your name?::Manu
    Who is your crush
    ?::IDK
    What is your age
    ?::21
    Who is the greates boxer of all time
    ?::Muhammad Ali
    Who is the first man in space
    ?::Yuri Gagarin
    What is the fastest animal on land
    ?::Cheetah 

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    How about
    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define MAX_QUIZ 5
    #define MAX_ARR 100
    
    typedef struct quiz {
        char question[MAX_ARR];
        char answer[MAX_ARR];
    } Quiz;
    
    int initQuiz(Quiz *);
    
    int main()
    {
        Quiz quiz[MAX_QUIZ] = { "", "" };
        int correct = 0, wrong = 0, qindex, quizCtr = 0;
        int answer;
        char inputText[MAX_ARR] = { 0 };
    
        srand(time(NULL));
    
        if (!initQuiz(quiz))
            printf("Error: Quiz Initialization Failed !");
        else {
            for (quizCtr = 0; quizCtr != MAX_QUIZ; quizCtr++) {
                qindex = rand() % MAX_QUIZ;
                printf("%s\nAnswer: ", quiz[qindex].question);
                fgets(inputText, sizeof(inputText), stdin);
                answer = strncmp(inputText,
                                  quiz[qindex].answer, strlen(inputText) - 1);
                switch (answer) {
                case 0:
                    correct++;
                    break;
                default:
                    wrong++;
                    break;
                }
            }
        }
    
        printf("Total Questions: %d\nCorrects: %d\nWrongs: %d\n",
               MAX_QUIZ, correct, wrong);
        return 0;
    }
    
    int initQuiz(Quiz * q)
    {
        FILE *file = NULL;
        int ctr = 0;
        char lineStr[MAX_ARR] = { 0 };
    
        if (!(file = fopen("quiz.txt", "r")))
            return 0;
        else {
            while (ctr < MAX_QUIZ &&
                   fgets(lineStr, sizeof(lineStr), file) != NULL) {
                char *seppos = strstr(lineStr, "::"); /* find the separator */
                if (seppos != NULL) {
                  int f1Len = seppos - lineStr; /* from start of line to :: */
                  int f2Len = strlen(seppos)-3; /* :: and a \n to omit from the length */
                  strncpy( q[ctr].question, lineStr, f1Len );
                  q[ctr].question[f1Len] = '\0';
                  strncpy( q[ctr].answer, seppos+2, f2Len );
                  q[ctr].answer[f2Len] = '\0';
                  ctr++;
                }
            }
        }
        fclose(file);
        return 1;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    It looks like your original code was nearly correct After seeing quiz.txt I have a couple of suggestions. Here's another way you could change your code and quiz.txt to make things right:

    Code:
    while( ctr < MAX_QUIZ )
    Code:
    What is your name?::Manu::
    Who is your crush?::IDK::
    What is your age?::21::
    Who is the greates boxer of all time?::Muhammad Ali::
    Who is the first man in space?::Yuri Gagarin::
    What is the fastest animal on land?::Cheetah::
    Regards,
    Brian

  11. #11
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Salem
    How about
    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define MAX_QUIZ 5
    #define MAX_ARR 100
    
    typedef struct quiz {
        char question[MAX_ARR];
        char answer[MAX_ARR];
    } Quiz;
    
    int initQuiz(Quiz *);
    
    int main()
    {
        Quiz quiz[MAX_QUIZ] = { "", "" };
        int correct = 0, wrong = 0, qindex, quizCtr = 0;
        int answer;
        char inputText[MAX_ARR] = { 0 };
    
        srand(time(NULL));
    
        if (!initQuiz(quiz))
            printf("Error: Quiz Initialization Failed !");
        else {
            for (quizCtr = 0; quizCtr != MAX_QUIZ; quizCtr++) {
                qindex = rand() % MAX_QUIZ;
                printf("%s\nAnswer: ", quiz[qindex].question);
                fgets(inputText, sizeof(inputText), stdin);
                answer = strncmp(inputText,
                                  quiz[qindex].answer, strlen(inputText) - 1);
                switch (answer) {
                case 0:
                    correct++;
                    break;
                default:
                    wrong++;
                    break;
                }
            }
        }
    
        printf("Total Questions: %d\nCorrects: %d\nWrongs: %d\n",
               MAX_QUIZ, correct, wrong);
        return 0;
    }
    
    int initQuiz(Quiz * q)
    {
        FILE *file = NULL;
        int ctr = 0;
        char lineStr[MAX_ARR] = { 0 };
    
        if (!(file = fopen("quiz.txt", "r")))
            return 0;
        else {
            while (ctr < MAX_QUIZ &&
                   fgets(lineStr, sizeof(lineStr), file) != NULL) {
                char *seppos = strstr(lineStr, "::"); /* find the separator */
                if (seppos != NULL) {
                  int f1Len = seppos - lineStr; /* from start of line to :: */
                  int f2Len = strlen(seppos)-3; /* :: and a \n to omit from the length */
                  strncpy( q[ctr].question, lineStr, f1Len );
                  q[ctr].question[f1Len] = '\0';
                  strncpy( q[ctr].answer, seppos+2, f2Len );
                  q[ctr].answer[f2Len] = '\0';
                  ctr++;
                }
            }
        }
        fclose(file);
        return 1;
    }
    It works but.. There must be something in the original code that is wrong.. i tried making Quiz quiz a global variable and dont pass it as an argument and the program terminated normally must be the way im passing quiz to function initQuiz. Did i did something wrong there probably? Or probably its just strtok i had lotsa troubles using this function even before..

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    One of the main problems in your original code is:

    Code:
    while( 1 ) 
                 { 
                    fgets( lineStr, sizeof( lineStr ), file ); 
                    if( feof( file ) ) break; 
                    sprintf( q[ctr].question, strtok( lineStr, "::" ) ); 
                    sprintf( q[ctr].answer, strtok( NULL, "::" ) ); 
                    ctr++; 
                 }
    With more than 5 lines in quiz.txt it was easy for you to go out of bounds in q[ctr]... Moving quiz to global space probably just let you overflow in a less tragic way.

    Regards,
    Brian

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by loko
    So what do i use aside from gets?
    The FAQ - here.
    If you want to get rid of the newline on the end, I usually do something like this:
    Code:
    char s[100];
    fgets(s, 100, stdin);
    if(s[strlen(s)-1] == '\n') s[strlen(s)-1] = 0;
    There was a thread on it somewhere - try searching.
    Last edited by dwks; 07-04-2005 at 01:52 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *p;
    
    p = strchr( s, '\n' );
    if( p )
        *p = '\0';

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Br5an
    One of the main problems in your original code is:

    Code:
    while( 1 ) 
                 { 
                    fgets( lineStr, sizeof( lineStr ), file ); 
                    if( feof( file ) ) break; 
                    sprintf( q[ctr].question, strtok( lineStr, "::" ) ); 
                    sprintf( q[ctr].answer, strtok( NULL, "::" ) ); 
                    ctr++; 
                 }
    With more than 5 lines in quiz.txt it was easy for you to go out of bounds in q[ctr]... Moving quiz to global space probably just let you overflow in a less tragic way.

    Regards,
    Brian

    Ah i see.. Now its working

    PHP Code:
    #include<stdio.h>
        #include<time.h>
        #include<stdlib.h>
        #include<string.h>

        #define MAX_QUIZ 5
        #define MAX_ARR 100

        
    typedef struct quiz {
            
    char question[MAX_ARR];
            
    char answer[MAX_ARR];
        }  
    Quiz;

        
    int initQuizQuiz * );



        
    int main()
        {
            
    Quiz quiz[MAX_QUIZ] = { """" };
            
    int correct 0wrong 0qindexquizCtr 0;
            
    int answer;
            
    char inputText[MAX_ARR] = {0};

            
    srandtimeNULL ) );

            if( !
    initQuizquiz ) )
                
    printf"Error: Quiz Initialization Failed !" );
            else {

                for( 
    quizCtr 0quizCtr != MAX_QUIZquizCtr++ )
                {
                    
    qindex rand() % MAX_QUIZ;
                    
    printf"%s\nAnswer: "quiz[qindex].question );
            
    fgetsinputTextsizeofinputText ), stdin );
                    
    answer strnicmpinputText,
                    
    quiz[qindex].answer,
            
    strlenquiz[qindex].answer ) - );

                    switch( 
    answer ) {

                        case 
    0:
                            
    correct++;
                            break;

                        default:
                            
    wrong++;
                            break;
                    }

                }
            }

            
    printf"Total Questions: %d\nCorrects: %d\nWrongs: %d",
            
    MAX_QUIZcorrectwrong );

            return 
    0;
        }



        
    int initQuizQuiz *
        { 
            
    FILE *file NULL
            
    int ctr 0
            
    char lineStr[MAX_ARR] = {0}; 

            if( !( 
    file fopen"quiz.txt""r" ) ) ) 
                    return 
    0
            else { 

                 while( 
    ctr != MAX_QUIZ 
                 {    
               
    fgetslineStrsizeoflineStr ), file );
                    
    sprintfq[ctr].questionstrtoklineStr"::" ) ); 
                    
    sprintfq[ctr].answerstrtokNULL"::" ) ); 
                    
    ctr++; 
                 } 

          } 
            
    fclosefile ); 
            return 
    1
        } 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 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. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21