-
February 9th, 2009, 10:30 AM
#1
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("%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.
-
February 9th, 2009, 10:42 AM
#2
Re: Please Help- my first program does something weird !
First, use code tags when posting
= assigns the value 9 to c every time through the loop. Use == for comparison
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.
-
February 9th, 2009, 10:50 AM
#3
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.
-
February 9th, 2009, 10:56 AM
#4
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:
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("%d",&r);
}
while(r!=4);
}
I hope this helps.
-
February 9th, 2009, 10:58 AM
#5
Re: Please Help- my first program does something weird !
c is 10 at the end of your first loop.
-
February 9th, 2009, 11:08 AM
#6
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.
-
February 9th, 2009, 02:16 PM
#7
Re: Please Help- my first program does something weird !
Originally Posted by Jeff++
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
-
February 9th, 2009, 05:46 PM
#8
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 ?
-
February 9th, 2009, 05:47 PM
#9
Re: Please Help- my first program does something weird !
and thanks for the great help ! : )
-
February 9th, 2009, 06:24 PM
#10
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.
-
February 9th, 2009, 06:34 PM
#11
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.
-
February 9th, 2009, 06:43 PM
#12
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("%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.
-
February 9th, 2009, 06:46 PM
#13
Re: Please Help- my first program does something weird !
also I had to change your
to
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
-
February 9th, 2009, 07:17 PM
#14
Re: Please Help- my first program does something weird !
Originally Posted by Jeff++
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.
-
February 9th, 2009, 07:27 PM
#15
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|