Thread: run-time errors ~~

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    7

    Unhappy run-time errors ~~

    My program has a lot of run-time errrors,but I don't know how to correct it after I debugged and looked through it.My program is an analyzer.It has problem on finding the longest word and the font colour. I feel so upsad with my program.Can anyone help me,please?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    scanf("%s",filename);
    Bad. Read my signature for more information.

    Code:
    pfile = fopen(strcat(filename,".txt\0"),"r");
    Confusing to the end user. You say enter a filename but automatically append .txt, even with no mention of so. You don't even check if there even IS a .txt extension in the inputted filename either.

    Code:
    a = fgetc(pfile);
    fgetc returns int, but a is char. This causes a warning. I suggest you redeclare all those variables to int instead.

    Code:
    if(a==' '||(a>32&&a<48)||(a>57&&a<65)||a=='\n')
    {
    	words++;
    	if(b==' '||b<65||(b>90&&b<97)||b>122)
    		words--;
    }
    What's this mess? How am I supposed to know what ASCII value 32 is? Use string liters, such as '\n', ' ', etc, like you've done a little already.

    Code:
    while(ch1!=EOF)
    {
    	count = '.';
    	count++;
    }
    What's this supposed to do? It does nothing, except an infinite loop!

    Code:
    while(y!=EOF)
    {
    	x = '\n';
    	x++;
    }
    Same here... infinite loop.

    Code:
    while(n!=EOF)
    {
    	z = '?';
    	z++;
    }
    Same here... infinite loop.

    Perhaps you should try to actually fix your code first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    Besides these,I still encounter some problems.First of all,the program stopped after I input the filename.It didn't work afterwards,howcome?Are there something that affects it?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Infinite loop.
    Have you fixed these problems yet? If not, then try to do so before moving on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    I have made quite many changes,but it still stuck.Can you find what is the problem with that?I just took a little function out as most of my functions are in the same structure.

    Code:
    pfile = fileopen();
         punct = 0;
         ch = fgetc(pfile); 
         do{
             if( ch == '.' ||ch==','||ch=='!'||ch=='?'||ch=='('||ch==')'||ch==';')
                 punct++; 
            }while(ch!=EOF);
                
            return punct;

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You only call fgetc() once. You need to call it in the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    Do you mean I have to put this(ch=fgetc(pfile))in the loop?Where?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Come on, read your loop.
    If ch is . , ! ? ( ) or ;, then increase punct by one.
    Loop.
    Check the ch value AGAIN.
    You're just comparing the same data over and over.

    We aren't going to do your work for you, so you'll actually need to analyze your code, understand it and understand what you actually have to do to make it work.
    Before writing something, you must have a clear picture on how you are going to do it.
    Is it going to read a character, compare it to see if it matches what you want and increase the counter if it does?

    Well, I'll leave the rest to you. You should be able to figure it out.
    Last edited by Elysia; 01-01-2008 at 05:39 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Sounds like someone should look up and learn flowcharts.

    While they may seem very pointless, they're in-fact not.

    off topic/
    Goodness Elysia's signature is large... and not just largely full of lies
    /off topic

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    I encounter another problem again.I have corrected the loop as laserlight taught.My function only works when it is in single.If I combine the functions together,it doesn't work.Why?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Post your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    I just add the fgetc into the loop.It works only when the program has only this function with the fileopen.
    Code:
         
         pfile = fileopen();
         punct = 0;
         ch = fgetc(pfile); 
         while(ch!=EOF){
             ch = fgetc(pfile);
             if( ch == '.' ||ch==','||ch=='!'||ch=='?'||ch=='('||ch==')'||ch==';'||ch=='"')
                 punct++; 
            }
            fclose(pfile);    
            return punct;
    Last edited by crazy; 01-01-2008 at 08:56 AM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then perhaps you might want to explain what you mean by
    My function only works when it is in single.If I combine the functions together,it doesn't work.Why?
    ?

    Also, if you don't know, you do two fgetc in a row without checking the result.
    Do you actually understand what you're doing here?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    What I mean is the above function only works properly while it is used with the file function (of course also the main program).I don't really know how to solve this error.I feel very hesitating and helpless with that.I know that fgetc will read the characters until EOF while fgets will sudden stop,so I use getc instead of fgets.What should I do the next step then?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Fgetc reads one character from the file. Fgets reads one line from the file.
    Both functions stop if they try to read beyond the end of the file, in which case fgetc will return EOF but fgets will fail and return 0 and feof on your FILE* pointer will return true.

    Try to read your code and see what it actually does. You can't guess what it does and rely on others to tell you what's wrong. If you do, you will most certainly fail any class and you'll never be a real program (whichever applies).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying Function Run Time
    By pf732k3 in forum C Programming
    Replies: 1
    Last Post: 03-21-2008, 12:36 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  3. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 02:58 AM
  4. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  5. Create class at run time
    By muaed in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-11-2002, 08:13 AM