Help me with my basic program, nothing I create will run

This is a discussion on Help me with my basic program, nothing I create will run within the C Programming forums, part of the General Programming Boards category; I have tried to attempt 10 challenges from a book I am reading (obviously a c coding book (= ) ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    45

    Thumbs down Help me with my basic program, nothing I create will run

    I have tried to attempt 10 challenges from a book I am reading (obviously a c coding book (= )
    But each time i have tried one I fail... For some reason, to my knowledge I have done exactly what the book shows me to do. I have attempted to go over my latest attempt (Create a program that produces prime numbers). After several attempts my program actually compiles, but of course it doesn't do anything, except count and for some reason it makes my screen flicker. My first question is, what am I doing wrong? And second question is how can anyone stand going over a program over and over again and not seeing any result?
    My code is as follows, it is very basic:

    Code:
    #include <stdio.h>
    
    int[/COLOR] main(void)
     {
    int test_number; 		/* This variable shall be tested whether or not it is prime, although at this point it does not seem to want to */
    int tester;		
     test_number = 3;
     tester = 2;
    
      while (1 == 1) 
        {
    
     	 if ((test_number % tester == 0) && (test_number != tester)) 
    		{
    		   test_number = test_number + 1;
    		    tester = 2;
    		    continue;
    		}
    	
    	
    	
    	 else if ((test_number % tester != 0) && (test_number != tester))
    		{
    		    tester = tester + 1;
    		    continue;
    		}
    
    
    
    	 else
    		{
    		   printf("%d is prime.\n", test_number);
    		    test_number = test_number + 1;
    		    tester = tester + 1;
    		    continue;
    		}
        }
    
     return 0;
    
    }
    Also if anyone can recommend a tutorial that they know for certain is up to date and all code in it is correct PLEASE let me know, I have given up on trying to learn anything from the book I have and at this time I can't run out and get another one )=
    I see there is a sticky post, but I'm wondering if you can recommend a tutorial that you started out on and didn't come across later.

    Now I must get some sleep (= 4 a.m. here (=

    Thanks for any help.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    a. It kind of goes for ever, a while-true loop without a 'break'.
    b. 1 == 1 evaluates to true or 1... so while(1) (restructure your program so it's not infinite!)
    c. Add some debugging lines so you know whats happening, eg. while(1) { puts("Loop iteration"); ...

    d. Do a rewrite, your logic and program design is horrid

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    45

    Unhappy

    Quote Originally Posted by zacs7 View Post
    a. It kind of goes for ever, a while-true loop without a 'break'.
    b. 1 == 1 evaluates to true or 1... so while(1) (restructure your program so it's not infinite!)
    c. Add some debugging lines so you know whats happening, eg. while(1) { puts("Loop iteration"); ...

    d. Do a rewrite, your logic and program design is horrid

    The idea is that It is an infinite loop )= That way it will continue to search for prime numbers.
    I attempted to add debugging lines, i placed printf functions every line to see what the program is doing, it goes through the loop until where it supposed to print the prime numbers, there it continues to repeat the error.
    I would do a rewrite but I'm not going to get a different result, I don't know what I did wrong so I would assume I would continue the error again and again... Kinda like my loop there....
    Thanks for your response =)

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What is the error?

    Your going to get overflow since it goes forever... So your signed int is going to loop around and around infinitly. For arguments sake, say the range of a signed int is -10,000 to 10,000. That means, on your 10,000th iteration, 10,000 + 1 = -10,000, and so on an so forth.

    Writting your algorithm on paper, with a NS or FlowChart would probably benifit you. Your example shows your not lacking knowledge of C, but your logic & design is the problem. Bare in minde program flow is an important factor.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    45

    Smile

    Quote Originally Posted by zacs7 View Post
    What is the error?

    Your going to get overflow since it goes forever... So your signed int is going to loop around and around infinitly. For arguments sake, say the range of a signed int is -10,000 to 10,000. That means, on your 10,000th iteration, 10,000 + 1 = -10,000, and so on an so forth.

    Writting your algorithm on paper, with a NS or FlowChart would probably benifit you. Your example shows your not lacking knowledge of C, but your logic & design is the problem. Bare in minde program flow is an important factor.
    That would be a problem wouldn't it... (=
    So infinite loops shall be avoided from now on, okay, thanks (=
    Not sure what you mean by flowchart though, I guess I must make a stop at google. I shall redesign it again. If you don't see a working version put on soon I probably went crazy (=

    Thanks again for your help

  6. #6
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    As suggested by others the program design is definitely terrible.
    And use a very large number as your limit (of course within bounds of the type you're defining)
    and then execute your program instead of using an infinite loop.
    An infinite loop will definitely be very irritating to debug if something's wrong.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Not to mention the infinite loop is most likely the source of the problem itself

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    To answer you other question, many C books today are pretty much up to date. I have one that was written in 1994 and although the syntax it uses is pretty dated, it still works and runs as it should.

    There are many tutorials on C on the internet. Google "C tutorial" and you will see what I mean. The only problem is these are quite limioted in explanation, and dont cover the important topics of the langauge in that much detail. Get a book as suggested on the sticky thread
    I'm just trying to be a better person - My Name Is Earl

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Also some tutorials might be written by those not entirely 'qualified'. Consider using tutorials on this very website?

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    45
    Thanks for your responses. I shall go buy an updated book, I'll get rid of my infinite loop (even though I favor them ): ) and make another attempt at making the program run. When I try to create it again is there anything I should not do (besides the loop like that)? And I'm open to any hints at designing better (=

    Thanks again.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Infinite loops have their uses (although it should never really be infinite).

    Consider more meaningful variable names

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    45
    Quote Originally Posted by zacs7 View Post
    Infinite loops have their uses (although it should never really be infinite).

    Consider more meaningful variable names
    Ok I shall do that (=
    Curious what do you mean by infinite loops that aren't infinite, lol, maybe it has magical qualities

    I managed to buy a book today (had to go 3 book stores to find a book on C, found lots of C++ though )=
    The book I found is called C The complete Reference, 4th edition by Herbert Schildt. I noticed this book/author did not get very good reviews any read this book, is it really that bad?

    Also if anyone has any advice on how I can make better programs let me know, I'm interested in any opinions.

    Thanks again.

  13. #13
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    By "finite" infinite loops, he's talking about loops, whose condition are always true and run forever but where in the loop body is somewhere a condition that could break out of the loop.

    Code:
    while (1)
    {
      // do some fancy stuff
      if (some_condition)
        break;
    }
    In a very simple case, the condition could of course be written inside the loop condition, but there are more advanced cases where you can't possibly write all the conditions in the loop condition or where you simply don't know if you should continue at that point in the code. A common example for such constructions are game engines, that infinitely display something on the screen (until the program exits).

    Another method to write infinite loops that break at some point are:

    Code:
    int cond = 1;
    while (cond)
    {
      //do some stuff here
      if (some_other_condition)
        cond = 0;
    }

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Herbert Schildt
    Arrrrg!!! Run and hide from his bad practices!
    I'm just trying to be a better person - My Name Is Earl

  15. #15
    Registered User
    Join Date
    May 2007
    Posts
    45
    Quote Originally Posted by swgh View Post
    Arrrrg!!! Run and hide from his bad practices!
    Yeah Ive seen quite a bit of bad reviews for this author, to bad all I had to choose from was this or a book from the "For Dummies" line, I hope he's not as bad as everyone says he is.


    Question about the infinite loop would it still be ok to use the infinite loop if I could guarantee that the variable would stay within its limits (of what it can contain), or if I was able to stop it at the point where that limit were to be reached?

    Also can someone give me a hint at why the program doesnt run...I've looked but nothing jumps out at me )=

    Thanks for the help

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C++ Program help
    By Ronzel in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2009, 03:24 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 04:21 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. program won't run properly, help needed asap
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-16-2002, 09:52 AM

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