CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Feb 2009
    Posts
    80

    Exclamation Please Help- my first program does something weird !

    hey,
    I just wrote my first program of my own.

    it works...
    but I can't figure out why if you guess the correct number you have to hit " quit " more than once to exit the program. If you guess the right number 4 times, you have to exit 4 times before it actually closes. can someone please point out in my code what is causing this ?

    thanks.
    here's my lame first written program.

    Code:
    #include <stdio.h>
    #include <conio.h>
    int r;
    int main()
    {
       
    
        int c=0;
          while(c <=9){
          printf("Print 10 !\n\n");
          ++c;
       }
           while(c=9){ 
          printf("want to re-print ? then you need to find the lucky number !\n\n if you get it wrong you will have to quit !");                
          scanf("&#37;d",&r);
          if(r!=4) break;
          else main();
          }
    
       { 
           printf("click any key to quit");
            
    }                                                     
    getche();
    
    }
    any help is appreciated !!

    Thanks
    Last edited by Jeff++; February 9th, 2009 at 06:29 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Please Help- my first program does something weird !

    First, use code tags when posting
    Code:
    while(c=9)
    = assigns the value 9 to c every time through the loop. Use == for comparison

    Code:
    else main();
    Never call main directly. Ever. The purpose of a while loop is to keep repeating while a condition is true. Even if it were okay to call main, it wouldn't be necessary.

  3. #3
    Join Date
    Feb 2009
    Posts
    80

    Re: Please Help- my first program does something weird !

    why not ever call main () directly ?

    and now I've switched it to c==9 I don't get the next arguement, it just goes straight to quit.

    thanks.

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Please Help- my first program does something weird !

    Firstly, please use [code][/code] tags when posting.

    Secondly, you are making an assignment in your condition statement:

    Code:
    while(c=9)
    Thirdly, you are using recursion when you shouldn't be. The reason for the "strange" behaviour that you see is because you have called main() in a recursive manner (main shouldn't be called more than once), so have to exit the same number of times main is called. I am guessing that you used recursion because you couldn't figure out a better way designing your program?

    What you could do is a do-while loop and drop the recursion:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int r=0;
    
      do
      {
        int c=0;
        while(c <=9){
        printf("Print 10 !\n\n");
        ++c;
        }
        printf("want to re-print ? then you need to find the lucky number !\n\n if you get it wrong you will have to quit !");
        scanf("&#37;d",&r);
      }
      while(r!=4);
    
    }
    I hope this helps.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Please Help- my first program does something weird !

    c is 10 at the end of your first loop.

  6. #6
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Please Help- my first program does something weird !

    That condition is wrong, but I was more interested in the bogus structure - I should have caught the condition though.

    In addition, I think I must have pressed the 'Reply' button to start my last my last post, slightly before you pressed 'Submit Reply' button, since there were no replies when I started typing. It made me smile though that the first two points were in the same order.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please Help- my first program does something weird !

    Quote Originally Posted by Jeff++ View Post
    why not ever call main () directly ?
    Because the C++ standard says so.
    Code:
    int main()
    {
       main();
    }
    Code:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout ! 
    
    Your Comeau C/C++ test results are as follows: 
    
    
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 3: error: function "main" may not be called or have its
              address taken
         main();
         ^
    
    1 error detected in the compilation of "ComeauTest.c".
    
    In strict mode, with -tused, Compile failed
    Any C++ compiler that allows you to call main() is broken, or you're using some sort of non-standard extension to the C++ language.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Feb 2009
    Posts
    80

    Re: Please Help- my first program does something weird !

    now I'm confused.
    Is the code repair by predicate normitive right or not ?

    and I'm using Dev C++
    because it allows me to call main () it's broken ?

    huh ?

  9. #9
    Join Date
    Feb 2009
    Posts
    80

    Re: Please Help- my first program does something weird !

    and thanks for the great help ! : )

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Please Help- my first program does something weird !

    Just don't call main(). There's no reason you would ever need to do that. Disregard whether the compiler allows it or not....those things aren't perfect.

  11. #11
    Join Date
    Feb 2009
    Posts
    80

    Re: Please Help- my first program does something weird !

    well,
    I tried your fix there, but now the program doesn't work right.
    if you guess the correct number " 4 " and click enter,
    the window closes.
    it's supposed to "print 10 !" ten more times then ask again.
    if you get the number wrong it's then supposed to say " press any key to quit "
    any ideas ?

    thanks.

  12. #12
    Join Date
    Feb 2009
    Posts
    80

    Re: Please Help- my first program does something weird !

    ok,
    I see, you left off the other part. If I combine that with what I had at the end I get

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int r=0;
    
      do
      {
        int c=0;
        while(c <=9){
        printf("Print 10 !\n\n");
        ++c;
        }
        printf("want to re-print ? then you need to find the lucky number !\n\n if you get it wrong you will have to quit !");
        scanf("&#37;d",&r);
      }
      while(r==4);
    
    
     
    {       printf("click any key to quit");
            
    }                                                     
    getche();
    
    }
    which seems to work just fine.

    is this considered good syntax now ?

    cheers.

  13. #13
    Join Date
    Feb 2009
    Posts
    80

    Re: Please Help- my first program does something weird !

    also I had to change your

    Code:
    while(r!=4);
    to

    Code:
    while(r==4);
    because it is supposed to print only if you guess 4.
    with the != it would re-print for any key except 4.

    is this all correct what I've done ?

    thanks

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please Help- my first program does something weird !

    Quote Originally Posted by Jeff++ View Post
    now I'm confused.
    Is the code repair by predicate normitive right or not ?

    and I'm using Dev C++
    because it allows me to call main () it's broken ?

    huh ?
    Did you read my entire post?
    or you're using some sort of non-standard extension to the C++ language
    Dev C++ is an integrated development environment (IDE) -- it is not a compiler. The compiler that Dev C++ uses is the gcc 3.x compiler. Having said this, gcc by default enables non-standard extensions to the C++ language. Using these extensions, you are creating invalid ANSI C++ programs.

    One of those extensions is calling main(). As I stated, calling main() directly is not allowed by ANSI C++ (if you have the ANSI/ISO C++ standard document, it is 3.6.1, paragraph 3 -- "The function main shall not be used within a program").

    To use g++ as a true, ANSI C++ compiler, you must set the proper compiler switches:

    -pedantic -Wall

    These are the switches that must be used. Once you do that, you will see that g++ will fail to compile your code because it will be working as a legitimate, standard (or close to standard) C++ compiler. If this assignment is for a course, be lucky I warned you about this. A competent teacher would take major points off C++ code that is illegal.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 9th, 2009 at 07:23 PM.

  15. #15
    Join Date
    Feb 2009
    Posts
    80

    Re: Please Help- my first program does something weird !

    ok thank... but the g++ part went right over my head.

    where does the g++ come from ? and the switch names ?

    thanks.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured