Archived

This topic is now archived and is closed to further replies.

Simple C program help

This topic is 6276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, 'nother rookie question. I'm trying to write a simple number-guessing game. I've look high and low for what I else I need to do to this program. This is absolutely beginners stuff, I know, but I gotta start somewhere. Here's my program so far: #include <'stdio.h'> #include <'stdlib.h'> char x; int game(); main() { printf("Pick a number between 1 and 10: "); x=atoi(getchar()); game(); return 0; } int game() { for(x=atoi(getchar());; ) { if(atoi(getchar())<7) { printf("\nLower"); continue; } if(atoi(getchar())>7) { printf("\nHigher"); continue; } if(atoi(getchar())=7) { printf("\nYou guessed the correct number!"); break; } } } Like I said, extremely basic. I'm essentially testing myself on what little I know, and I know it has something to do with type-casting. I'm getting about 7 errors total using Dev-C++. PLEASE HELP!!!! --------------------------- "Until next time, boobie. Push the button, Frank." Edited by - Jiggold on 10/7/00 12:51:37 PM Edited by - Jiggold on 10/7/00 12:52:25 PM Edited by - Jiggold on 10/7/00 12:53:18 PM Edited by - Jiggold on 10/7/00 12:54:04 PM

Share this post


Link to post
Share on other sites
First of all this line here:

if(atoi(getchar())=7)

Has a problem because you used the "=" operator instead of the "==" operator. One is for asignment, the other for comparison. That won't always give you an error (making it hard to debug) but here it looks like it will because on the left side of your comaprison you don't have a variable to asign 7 to.

The Game function is supposed to return an int but instead returns nothing, that will give you an error.

Another thing is that this program will have strange results that I'm sure you don't want. You're calling atoi(getchar()) at every comparison which means you're getting a new character for every comparison.

Also the way you set up you set up your for loop causes x to be set to a character from the keyboard once (when the loop is first initialzed), it looks like you want it to be called every time you pass through the loop.

Try this:

    
void game()
{
for(; x != 7; x = atoi(getchar()) )
{
if ( x < 7 )
{
// put your code here

}

if ( x > 7 )
{
// put your code here

}
}
printf( "You guessed it!\n" );
}


Note that the final comparison is worked into the for loop rather than using a break.

-----------------------------
Mice are an excellent source of mice.

Edited by - Jo on October 7, 2000 2:13:27 PM

Edited by - Jo on October 7, 2000 2:16:41 PM

Share this post


Link to post
Share on other sites
One more thing (although it isn''t necessary):

Since you have the user entering a number between 1 and 10 you should probably make sure that they actually do that, and give them an error otherwise. You''ll also get odd results if they enter a character, so try checking to see if you have a character or a number. I think there''s an old function that does that, isChar() or something like that.

Share this post


Link to post
Share on other sites
Thats cool asking for help thats what I would hope this message board is here for, but also for one to master programming languages you need two things 1. Time 2. lots of patience.

One thing that I did when i started is first looking at each line and see what you can find that doesnt look right. even if it looks right look at each part of it.

sometimes it helps to walk away or comment it out then type it over really s-l-o-w and sometimes you see whats up. But to really be good im sorry to say you HAVE TO LOVE TO DEBUG. For me its a love hate Relationship

I will give you a few things from your code I see, it may help.
(these are my comments only if this is your exact code)

//incorrect
#include <''stdio.h''>
#include <''stdlib.h''>

//correct
#include
#include

//incorrect
for(x=atoi(getchar());; )

//correct
for(x=atoi(getchar()) );

also why are your using continue? for a simple program like
this it can be done with a simple else if setup.

Good luck

a_insomniac





If my compiler was voice activated with the voice of my mother-in-law....there would be no errors!

Share this post


Link to post
Share on other sites
I think what a_insomniac meant when he called attention to your include statements was that you either use quotes or brackets, not both at once. The brackets look for librarys that are stored in your default library location, the quotes look for headers that are stored in the same directory as your project (or whatever folder you have setup).

In this case the correct syntax would be:

                
#include <stdio.h> // not #include "stdio.h"


I must admint I'm confused by your other statement though a_insomniac...

for(x=atoi(getchar()) );

Isn't that an improper number of arguments for a for loop, not to mention putting a semicolon at the end creates it with an empty body? Even if that did work, without any comparison statements or room for breaks it would seem you have an infinite loop on your hands.

You gave some great general advice for programming in general though. Of course it never hurts to have a good debugger too.

Edited by - Jo on October 7, 2000 3:07:12 PM

Edited by - Jo on October 7, 2000 3:07:55 PM

Edited by - Jo on October 7, 2000 3:08:28 PM

Share this post


Link to post
Share on other sites
Everyone, thanks for the responses. It helps to get support. I know the #include statments appear wrong on the post, but I had to add those apostrophes, otherwise the post interpets the brackets as HTML and doesn't display them at all, hence the numerous edits. Anyways, the reason for the 'continue' statement was pretty much experimental, plus I'm not terribly familiar with the else-if loops yet (something new to look up, the Complete C++ Reference is a beautiful thing!). I made some changes along the lines Jo suggested (just assume the #includes are there):

-----------------------------------------------------------------
char x;
int game();
main()
{
printf("Pick a number between 1 and 10: ");
x==atoi(getchar());
game();
return 0;
}
int game()
{
for(x==atoi(getchar());
{
if(x<7)
{
printf("\nLower");
continue;
}
if(x>7)
{
printf("\nHigher");
continue;
}
if(x==7)
{
printf("\nYou guessed the correct number!");
break;
}
}
}
-----------------------------------------------------------------

I didn't put the '!' in 'cause, again, unfamiliar with it (and again, something new to look up). I'm not sure the '==' need to be in ever statement, too, but I added em anyways. It's now only coming up with two errors, same type of error on two different lines.: "passing 'int' argument of 'atoi(const char*) lacks cast". It comes up on lines 8 and 14. Also, Jo, how do you get boxed code like that to appear in the post?



---------------------------
"Until next time, boobie. Push the button, Frank."

Edited by - Jiggold on October 7, 2000 3:19:58 PM

Share this post


Link to post
Share on other sites
Maybe I should make some general explanations here since it looks like I caused some confusion from before.

You use a single = symbol for when you want to assign a variable a value. Example:

x = 10;

You use a == symbol for when you want to compare 2 values. Example:

if ( x == 10 )

A for loop has 3 parameters, the first is executed once when the loop is first encountered, the second is a comparison statement that causes the loop to break when it is no longer true, and the third is a statement that is execute every loop time. If you put a semicolon right after a for statement that causes it to execute as a loop without a body. That means that the statements in brackets afterwards are not used in the loop, and execute seperately.

The != symbol means "Not Equal To".

So...

My for statement meant the following (in english):

for (; x != 7; x = atoi(getchar())

There's no initializing statement, that's why there's a lone semicolon there. As long as x is not equal to 7 continue the loop. At the end of each loop, get a new character (x = atoi(getchar()).

Now that I think of it you might want to initaialize x as well, since the third param doesn't execute untill the end of the loop. Try changing it to this.

for( x = atoi(getchar()); x != 7; x = atoi(getchar())

Now note that you no longer need to check and see if x equals 7. We know that when the loop stops executing x is equal to 7.

My comments to you would be to first go back and check to see if you're making an assignment, or a comparison, and make sure you're using the correct operator. x == atoi(getchar()) would evalute to true of false, while x = atoi(getchar()) does what you want it to.

As for the cast errors, it's been a long time since I've used those functions, just experiment with the casting operators to see if that fixes it. Looking it over I would imagine that errors actually have to do with you using the wrong operators. Get that all fixed and see if you have the same errors.

My final bit of advice would be for you to focus on understanding exactly what your program is going to do before you even execute. It saves time, trust me.

BTW You get the boxed code by using the [ source ] [ / source ] (without the spaces - those were added so I could type them out) tags that are supported by this message board. Read the FAQ to find out more.

Hope this helps, and best of luck.

-----------------------------
Mice are an excellent source of mice.

Edited by - Jo on October 7, 2000 3:47:49 PM

Share this post


Link to post
Share on other sites
Just a side note: The Microsoft C++ reference material usually has good explanations and some sample code for some of this stuff, and I would imagine they do a better job than I do of explaining everything.

Share this post


Link to post
Share on other sites
Thanks, Jo, I think that''s one of the few times reading about programming I''ve totally comprehended what was said the first time. Ever feel like you''re banging your head against a wall? Ayways, thanks. Ever consider becoming a teacher for this?

---------------------------
"Until next time, boobie. Push the button, Frank."

Share this post


Link to post
Share on other sites