Thread: I'm getting a runtime error

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    34

    I'm getting a runtime error

    Hi,

    Could you please take a look at the following code? I'm getting a runtime error every time I enter the average score:

    Code:
    struct blah {
        char firstname[20+1];
        char lastname[25+1];
        float average;
    };
    
    typedef struct blah entry;
    
    int main() {
    
        entry student;
    
        do {
            printf("First name: ");
            gets(student.firstname);
            if (strlen(student.firstname)==0) break;
            printf("Last name: ");
            gets(student.lastname);
            printf("Average score: ");
            scanf("%f", &student.average);
            // fflush(stdin);
    
        } while (1);
    
    
    
        return 0;
    
    }
    I simply can't see what I'm doing wrong.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    How about starting with the FAQs which mention why gets() and fflush(stdin) are bad?
    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.

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    FAQ gets()

    Additionally there are a couple of other problems:

    1. You never initialize any of your variables from the struct. This is why you are getting your run time error.

    2. After the first iteration this
    Code:
     if (strlen(student.firstname)==0) break;
    Does not break you out of your loop.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    I realize they're bad, thanks, but I'm still getting that runtime error on the input for average score.

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540

    1. You never initialize any of your variables from the struct. This is why you are getting your run time error.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    Quote Originally Posted by andyhunter

    1. You never initialize any of your variables from the struct. This is why you are getting your run time error.
    Don't yell, I didn't notice your post (look at the time mine was posted).

    Thanks though.

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Ah, but CAPS is yelling. Bolding is 'look at me'
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

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. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM