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 for a, 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!

Recommended Answers

All 10 Replies

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).

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 for a, 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 <stdio.h>"
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?

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.

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;	
}

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 does exactly 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?

Since giving the solution right away seems to be frowned upon, I guess I'll give a few hints.

There are multiple ways to make this behave a bit more the way you want it to:

One uses a different approach for which I will give the following hints:
#include <stdlib.h>
use char array[] (which is a string)

Then there's two ways:
1.) replace getchar() with fgets()
2.) Use a while loop for getchar()
Use the magic command called ????

Profit!

The behaviour still isn't optimal but it should be a lot more like the way you want it to be.

Of course the main thing lacking in your program is the actual x^y calculation. I take it you know how to do that?

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?

just tried using fgets() (see below) but I run into message "warning: passing argument 1 of ‘fgets’ makes pointer from integer without a cast"

fgets() requires an array of char for the first argument.

#include <stdio.h>

int main() {
    char input[BUFSIZ];
    printf("\nTo calculate a^b, enter value a now.\n");
    int a = *fgets(input, BUFSIZ, stdin) - '0';
    printf("\nEnter b now.\n");
    int b = *fgets(input, BUFSIZ, stdin) - '0';
    printf("a = %d, b = %d\n\n", a, b);
    return 0;
}

unfortunately, works exactly the same as getchar().

Yes, getchar() is implemented in terms of fgetc(). The best way using getchar() is a loop until the '\n' character is found. The '\n' character in an interactive stream means that the user typed the return key.

#include <stdio.h>

void iflush(FILE* istrm) {
    int c;
    while ((c = getc(istrm)) != '\n' && c != EOF)
        ;
}

int main() {
    printf("\nTo calculate a^b, enter value a now.\n");
    int a = getchar() - '0';
    iflush(stdin);
    printf("\nEnter b now.\n");
    int b = getchar() - '0';
    printf("a = %d, b = %d\n\n", a, b);
    return 0;
}

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?

The hints I gave suggested you use a character *array*, that is:
char a[10]; (Where 10 can be any number)
However to use your 'math program' to produce the result of a^b, you will need to convert that char array to an integer when you're done. (And you still need to add the magic a^b formula)

Hence why I suggested using #include <stdlib.h> so *really* look up the command reference for the stdlib.h library

#include <stdio.h>
#include <stdlib.h>

#define MAX_DIGITS 9
/* If you want to use getchar() add i as well for the while loop */
int a,b; 
char aChar[MAX_DIGITS];
char bChar[MAX_DIGITS];

int main(void) {
printf("\nTo calculate a^b, enter value a now.\n");
fgets(aChar,MAX_DIGITS,stdin);
/* Do something here so that bChar does not get influenced by you pressing enter */
printf("\nEnter b now.\n");
fgets(bChar,MAX_DIGITS,stdin);
a = /*Convert aChar char array to int*/
b = /*Convert bChar char array to int*/
printf("a = %d, b = %d\n\n", a, b);
/* Actually calculate a^b here instead of just printing a = a and b = b (lookup math.h or use a for loop) */
getchar();
return 0;

thanks a lot guys. i learned a lot from this :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.