multiple getchar() in a row
Here is a simple program which gives me unexpected results:
int main()
{
printf("\nTo calculate a^b, enter value a now.\n");
int a = getchar() - '0';
printf("\nEnter b now.\n");
int b = getchar() - '0';
printf("a = %d, b = %d\n\n", a, b);
return 0;
}
if i hit a digit fora, then hit enter, it assigns -38 as the value of b.
the only way it works is if i enter a and b right next to each other, such as 52. this is not how i want it to behave. your advice greatly appreciated!
MareoRaft
Junior Poster in Training
79 posts since Oct 2005
Reputation Points: 10
Solved Threads: 4
When you run the program, watch your hands carefully and count how many keys you hit. I suspect you're assuming that the Enter key is somehow treated differently when in reality it too is extracted by getchar as the '\n' character. -38 is fully expected when '\n' is 10 and '0' is 48 (as it is in ASCII).
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
When you run the program, watch your hands carefully and count how many keys you hit. I suspect you're assuming that the Enter key is somehow treated differently when in reality it too is extracted by getchar as the '\n' character. -38 is fully expected when '\n' is 10 and '0' is 48 (as it is in ASCII).
thank you! i guess my problem now is that getchar() doesn't behave the way i'd like it to. Perhaps there is a different function I can use so that the user enters a digit fora, then the text "Enter b now." comes up, then they can enter digit b? I have tried getche() because it requires no ENTER, but now I receive the error message:
Undefined symbols:
"_getche", referenced from:
_main in cchtsT6q.o
_main in cchtsT6q.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
thanks again! ;)
P.S. my code includes 1 line above "int main(){", and that is "#include "
P.P.S i read somewhere that stdio.h defines getche() but does not actually have the function written out. If this is the case, is there some generic code or a new library I can include in my program to fix this?
MareoRaft
Junior Poster in Training
79 posts since Oct 2005
Reputation Points: 10
Solved Threads: 4
Dude, just call getchar three times. It's not rocket science:
printf("\nTo calculate a^b, enter value a now.\n");
int a = getchar() - '0';
getchar(); /* Throw away the newline */
printf("\nEnter b now.\n");
int b = getchar() - '0';
printf("a = %d, b = %d\n\n", a, b);
To be truly robust you need to do more, but I fear I'll confuse you too much by explaining all of the nuances of stream I/O.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I'm actually quite interested in a robust method if you don't mind. I want to build good habits as a programmer.
Also, I recognize that I am retrieving a character when really i should be using a function to retrieve a number, so that i won't be limited to the 10 digits.
I want people running this program in a Windows C compiler to be able to see the output before their window closes, so I added the line "std::cin.get();" before "return 0;", but now I receive the error message:
^[[A1.8.c: In function ‘main’:
1.8.c:24: error: expected expression before ‘:’ token
thanks,
Mareo
P.S. so my code now looks like this:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("\nTo calculate a^b, enter value a now.\n");
int a = getche() - '0';
printf("\nEnter b now.\n");
int b = getche() - '0';
printf("a = %d, b = %d\n\n", a, b);
std::cin.get();
return 0;
}
MareoRaft
Junior Poster in Training
79 posts since Oct 2005
Reputation Points: 10
Solved Threads: 4
I'm actually quite interested in a robust method if you don't mind. I want to build good habits as a programmer.
Good habits as a programmer start with understanding the functions you are using and using them properly to get the results you want -- not by searching for a function that doesexactly what you wish.
The most robust method is processing each key individually and doing exactly what you need to do with each character entered.
I want people running this program in a Windows C compiler to be able to see the output before their window closes, so I added the line "std::cin.get();" before "return 0;", but now I receive the error message:
Isn't that C++? Why would you add a C++ function to a C program? And one that is identical to getchar() too?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
just tried using fgets() (see below) but I run into message "warning: passing argument 1 of ‘fgets’ makes pointer from integer without a cast"
char a;
fgets (a, 1, stdin);
then i discovered fgetc() (see below), which works. unfortunately, works exactly the same as getchar().
int a = fgetc(stdin) - '0';
As Narue suggested, I am throwing out the value (see below).
printf("\nTo calculate a^b, enter value a now.\n");
int a = getchar() - '0';
printf("\nEnter b now.\n");
int b = getchar() - '0';
if( b == -38 ) b = getchar() - '0';
printf("a = %d, b = %d\n\n", a, b);
This is a method of using a while loop (see below), although I'm not sure if this is what you were hinting at.
printf("\nTo calculate a^b, enter value a now.\n");
int a = getchar() - '0';
printf("\nEnter b now.\n");
while ( getchar() != 10 );
int b = getchar() - '0';
printf("a = %d, b = %d\n\n", a, b);
not sure what you meant about a magic command. i couldn't find mention of any "profit()". perhaps i misunderstood you :P so as far as efficiency goes, getchar() is the way to go?
MareoRaft
Junior Poster in Training
79 posts since Oct 2005
Reputation Points: 10
Solved Threads: 4
thanks a lot guys. i learned a lot from this :P
MareoRaft
Junior Poster in Training
79 posts since Oct 2005
Reputation Points: 10
Solved Threads: 4