-
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
-
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.
-
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.
-
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.
-
Re: Please Help- my first program does something weird !
c is 10 at the end of your first loop.
-
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.:blush:
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.:)
-
Re: Please Help- my first program does something weird !
Quote:
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
-
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 ?
-
Re: Please Help- my first program does something weird !
and thanks for the great help ! : )
-
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.
-
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.
-
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.
-
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
-
Re: Please Help- my first program does something weird !
Quote:
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?
Quote:
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
-
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.
-
Re: Please Help- my first program does something weird !
Quote:
Originally Posted by
Jeff++
ok thank... but the g++ part went right over my head.
where does the g++ come from ? and the switch names ?
thanks.
When you installed Dev-C++, it also installed the g++ (mingw) compiler. Look at the directories where it was installed. Also, those switches are probably set in the project options, but I don't have Dev-C++ in front of me to tell you exactly where to set them.
You need to do this, since otherwise you will inadvertantly create invalid C++ programs (so many posters using g++ make this same error of not setting the appropriate compiler switches beforehand).
Regards,
Paul McKenzie
-
Re: Please Help- my first program does something weird !
is it important to put
int r=0;
as opposed to just
int r;
??
it's always 0 if not defined right ?
so why bother if it gets a digit assigned to it later in the code ?
thanks
-
Re: Please Help- my first program does something weird !
so Paul... I hear what you're saying, sort of. In other words without adding the switch name....
I'm using a different compiler than I should be ?
the switch name is instructions to use compiler g++ instead ?
if this is so, why does it not mention it anywhere in the included instructions or tutorials ?
cheers.
-
Re: Please Help- my first program does something weird !
I'm seeing so many differences in what you folks are saying and my tutorial I've been reading...
like...
the tutorial has taught me the use of printf and scanf
but everyone seems to be using cout and cin,
yet this tutorial makes no mention of cout and cin.
can someone tell me the difference between them PLEASE.....
which one should I use ? where ? why ?
or better yet
maybe direct me to a better tutorial for total newbies.
so I can start over.
cheers.
-
Re: Please Help- my first program does something weird !
maybe first I should ask if I'm using the right language.
I want to one day be making video games.
I heard C++ was great for that so that's why I chose it as my first language here...
but what is
Visual C++ ? is that better ?
thanks.
-
Re: Please Help- my first program does something weird !
Quote:
Originally Posted by
Jeff++
I'm using a different compiler than I should be ?
the switch name is instructions to use compiler g++ instead ?
You don't need to use g++ directly -- the DevC++ environment does this for you.
If you go to the Project and Project Settings, there should be an option to set the compiler parameters. Those parameters are the ones I mentioned:
-pedantic -Wall
You can test if you've set the switches correctly by compiling the following:
Code:
int main()
{
int num = 10;
int array[num];
}
If this code doesn't compile and gives you an error where the array is declared, you have set the switches correctly. If it does compile successfully, then you still have the extensions turned on and you need to try something else to turn them off.
Quote:
if this is so, why does it not mention it anywhere in the included instructions or tutorials ?
DevC++ only has instructions on how to use DevC++, not the g++ compiler it runs underneath the hood. For that, you need to go to the Gnu website or mingw website.
Regards,
Paul McKenzie
-
Re: Please Help- my first program does something weird !
well thanks Paul, but when I click " Project Options " I get an error. Obviously a bug, it does it every time.
I get this
Application version: 4.9.9.2
Machine info
---------
Platform : Windows NT
OS version : version 6.0 (build 6001)
Additional info: Service Pack 1
The following error occured in version 4.9.9.2:
Access violation at address 0012F647. Write of address 0012F647 (at address 0x0012F647)
Please include a description of what you were doing before the error occured (please give as much precisions as possible) :
State information follows:
Stack trace:
------------
0058F80A (0018E80A): ShowExceptionInfo (ExceptionsAnalyzer - 566)
0058F9FF (0018E9FF): TExceptionsAnalyzer.GlobalExceptionHandler (ExceptionsAnalyzer - 574)
005712FA (001702FA): TfrmProjectOptions.InitVersionInfo (ProjectOptionsFrm - 1026)
0056EC8F (0016DC8F): TfrmProjectOptions.SetOptions (ProjectOptionsFrm - 513)
00578C83 (00177C83): TProject.ShowOptions (project - 1674)
00559E2E (00158E2E): TMainForm.actProjectOptionsExecute (main - 3551)
0059105B (0019005B): (devcpp - 230)
00591079 (00190079): (devcpp - 230)
Which I'm assuming means " screw you buddy, I'm a computer program... I don't need to work right"
guess I'll have to see if there's a bug fix before I can change those settings.
thanks though.
any insight on my other questions would be great.
1. what is the difference between " cin, cout " and " printf, scanf " ? should I use one over the other ?
2. What's the dif between C++ and Visual C++ ? I want to make video games in the future...
should I be using visual ?
3. can someone direct me to a more up to date tutorial for total newbies ?
one that explains cout and cin better.
THANK YOU ALL !!
-
Re: Please Help- my first program does something weird !
Quote:
Originally Posted by
Jeff++
well thanks Paul, but when I click " Project Options " I get an error. Obviously a bug, it does it every time.
I get this
Application version: 4.9.9.2
I would suggest getting the CodeBlocks IDE for g++. It is currently maintained, while Dev C++ is basically a dead project.
Quote:
1. what is the difference between " cin, cout " and " printf, scanf " ? should I use one over the other ?...
3. can someone direct me to a more up to date tutorial for total newbies ?
one that explains cout and cin better.
A good C++ book describes the difference between cin and cout. If you don't have a good C++ book, you need to get one since C++ isn't something you can learn "on the fly" or winging it. It just doesn't work that way. As to websites, many, if not most are notorious for giving wrong, outdated, or bad information concerning C++ programming. Unless the website has been recommended by C++ industry professionals, don't learn from them.
One site that is very good is this:
www.parashift.com
On the other hand, books are peer-reviewed, so any wrong information is usually corrected before publication (not all C++ books, but most) -- the same cannot be said about C++ websites.
Quote:
2. What's the dif between C++ and Visual C++ ?
C++ is a computer language invented by Bjarne Stroustrup and officially specified by ANSI/ISO in the year 1998.
Visual C++ is a brand of compiler created by Microsoft that compiles C++ code.
Regards,
Paul McKenzie
-
Re: Please Help- my first program does something weird !
hey thanks again paul.
I think you misunderstood me about the cin , cout thing.
I understand there use fine...
what I meant was, why use printf vs. cout ? is one better in certain situations ?
PS- I just use Dev C++ because I wanted to learn and I found it for free on the net.
cheers.
-
Re: Please Help- my first program does something weird !
Quote:
Originally Posted by
Jeff++
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.
It's ok, but you don't need to restrict the scope of your "click any key to quit" statement. i.e.
Code:
{
printf("click any key to quit");
}
can become simply
Code:
printf("click any key to quit");
With regards to printf/scanf and cout/cin, it depends on the language in which you are writing. Your program is a C program, it includes C headers (e.g. #include<stdio.h>) and uses C IO functions (printf, scanf). A C++ program includes C++ headers (e.g. #include<iostream>) and consequently will use C++ IO objects (cout, cin).
Although you can use the C printf and scanf functions in C++, they are not typesafe and this is a problem when writing C++ programs where you may want to stream non-POD (Plain Old Data) types such as classes.
The program you have written is clearly a C program. An equivalent C++ program would be
Code:
#include <iostream>
int main()
{
int r=0;
do
{
int c=0;
while(c <=9){
std::cout << "Print 10 !\n\n";
++c;
}
std::cout << "want to re-print ? then you need to find the lucky number !\n\n if you get it wrong you will have to quit !" << std::endl;
std::cin >> r;
std::cin.clear();
}
while(r==4);
system("PAUSE");
}
-
Re: Please Help- my first program does something weird !
ok, got code blocks downloading now.
using the intaller package, because I wouldn't know what to do with the source code.
unless I could put it in Dev C++ and compile it.
what's the advantage of building the application yourself ?
thanks
-
Re: Please Help- my first program does something weird !
Quote:
Originally Posted by
Jeff++
PS- I just use Dev C++ because I wanted to learn and I found it for free on the net.
CodeBlocks is also free and currently maintained. DevC++ is a dead project that hasn't been updated in years.
Regards,
Paul McKenzie
-
Re: Please Help- my first program does something weird !
big download and my internet is sloooooow today. I get it free so I can't complain.
guess I'll leave it downloading and head off to bed soon.
One last question since you've been the biggest help with this stuff, and I do appreciate it...
I want to design a C++ program that will open windows calculator at a point in the code...
and then close it again later in the code.
I can't seem to find any real answers on this subject anywhere.
I would think there's some simple code in this language that can open and close exe files,
can you give me an example how to do that ?
thanks again so much for your time.
cheers