What is wrong with this.
#include <stdio.h>
main()
{
int a;
int b;
int c;
a = 51;
b = 0;
c = getchar();
printf("Pleaes guess.\n");
while ((c = getchar()) != EOF)
if (c == 51)
printf("Good Job! You got it in %d guesses./n", b);
if (c > 51)
++b;
printf("Your Guess was too high\n");
if (c < 51)
++b;
printf("Your Guess was too low\n");
}
For future reference, as it will both improve the likelihood of getting a useful response, and make other people less frustrated with you, please don''t ask "what''s wrong with this" without ever citing what your problem is. What''s the error your getting? Or how is it misbehaving?
For starters, though, you''re missing a lot of braces. As in:
while(something){
// other code here
}
and whenever you have an if statement with more than one line of code in the condition, use braces. As in:
if (c < 51){
++b;
printf("Your Guess was too low\n");
}
I admit I didn''t look over everything, so I could have missed stuff. Why did I not look over everything? You didn''t even bother to tell me what was going wrong.
Sorry to sound rude.
-Arek the Absolute
For starters, though, you''re missing a lot of braces. As in:
while(something){
// other code here
}
and whenever you have an if statement with more than one line of code in the condition, use braces. As in:
if (c < 51){
++b;
printf("Your Guess was too low\n");
}
I admit I didn''t look over everything, so I could have missed stuff. Why did I not look over everything? You didn''t even bother to tell me what was going wrong.
Sorry to sound rude.
-Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
What do you expect c to be? What you will get is a ASCII value of type int but still a byte. I guess you want a numerical value, in which case you must use either cin or scanf().
Look at scanf() here, you should use it like: scanf("%i", &c);
Look at scanf() here, you should use it like: scanf("%i", &c);
quote: Original post by dampe64
while ((c = getchar()) != EOF)
Your telling it to get a character, but from where, you have not defined
FILE *Input;
or anything like that, (if I'm a little off with syntax or anything its cause I haven't used tht in a little bit), also check on the syntax for getchar and how to use it properly when using a file (also notice the variable c is an int and your trying to pass a char to it, its not gonna happen, unless you actually had some purpose that I'm not seeing for using it like that). Also if I recall in C you have to declare main void or int and stuff so something like this:
void main(void)
Thats what I remember anyways.
Also just a little timbit of info.
quote: Original post by dampe64
int a;
int b;
int c;
a = 51;
b = 0;
c = getchar(); //already assigned getchar() to int c
...
while ((c = getchar()) != EOF) //getchar assigned again to c
because you assigned the value to c in the while statement you do not need to assign it that value early or vice versa.
And if I'm mistaken about anything someone please correct me, just keep in mind I'm a little rusty.
[edited by - n0sp1n3 on September 20, 2002 8:56:30 PM]
dampe64''s code could never compile, very many syntatic and type errors.
int getc(FILE *stream)
int getchar()
quote: Original post by n0sp1n3getc() requires a stream/file, but getchar() gets it implicitly from STDIN. getchar() do return an int which equals EOF if it fails:
Your telling it to get a character, but from where, you have not defined
FILE *Input;
or anything like that, (if I''m a little off with syntax or anything its cause I haven''t used tht in a little bit), also check on the syntax for getchar and how to use it properly when using a file (also notice the variable c is an int and your trying to pass a char to it, its not gonna happen, unless you actually had some purpose that I''m not seeing for using it like that).
int getc(FILE *stream)
int getchar()
My bad, thats nice to know though, ya learn something every day. I guess I shouldn't have skipped the chapter in the book I'm reading that talked about that. Oh and since I'm here, I've seen like ++b and b++ and am wondering does it make a difference or anything in the way it increments the variable or is it just a personal preferance kinda thing?
[edited by - n0sp1n3 on September 20, 2002 12:06:38 AM]
[edited by - n0sp1n3 on September 20, 2002 12:06:38 AM]
Yes: ++a/--a is preincrement/-decrement, and a++/a-- is postincrement/-decrement.
It behaves differently, consider this:
int a = 0, b = 0, c = 0;
if(a++) // Is false; a is tested then incremented
if(++b) // Is true; a is incremented then tested
if((c++)) // However, this is true; think about it
EDIT: another example
EDIT 2: hmm, when thinking about it, I got a bit unsure about that last one. I think it should be like that, but don't qoute me on that, ok?
EDIT 3: Seems like the if((c++)) example is false, and that makes sense. I mixed it up, so never mind
[edited by - CWizard on September 21, 2002 2:17:02 AM]
It behaves differently, consider this:
int a = 0, b = 0, c = 0;
if(a++) // Is false; a is tested then incremented
if(++b) // Is true; a is incremented then tested
if((c++)) // However, this is true; think about it
EDIT: another example
EDIT 2: hmm, when thinking about it, I got a bit unsure about that last one. I think it should be like that, but don't qoute me on that, ok?
EDIT 3: Seems like the if((c++)) example is false, and that makes sense. I mixed it up, so never mind
[edited by - CWizard on September 21, 2002 2:17:02 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement