Just need a few questions answered...

S

SithVegeta

Code:

#include <stdio.h>
int main(void)
{
int in;

printf("Please enter an ascii number to convert:\n");
scanf("%d \n", &in);
printf("You entered %d, the regular value is %c", in, in);
getchar();
getchar();
getchar();
getchar();
return 0;
}

This program is supposed to convert an ascii number value to its regular
equivalent. Example: entering 90 will give you a Z. Why then do I need to
type in 90 twice? Also, why do I need to have so many "getchar()"'s at the
end of the code to prevent the window from closing right away?

Thanks!
-=Allen=-
 
A

Al Bowers

SithVegeta said:
Code:

#include <stdio.h>
int main(void)
{
int in;

printf("Please enter an ascii number to convert:\n");
scanf("%d \n", &in);
printf("You entered %d, the regular value is %c", in, in);
getchar();
getchar();
getchar();
getchar();
return 0;
}

This program is supposed to convert an ascii number value to its regular
equivalent. Example: entering 90 will give you a Z. Why then do I need to
type in 90 twice? Also, why do I need to have so many "getchar()"'s at the
end of the code to prevent the window from closing right away?

Thanks!

It may be as simple as forcing a flush of the output buffer. Try either
changing the last printf statement (at the newline char to the end) to:

printf("You entered %d, the regular value is %c\n", in, in);

or you can leave the last printf statement unchanged but add
fflush(stdout);
just after it.
 
A

Al Bowers

Al said:
It may be as simple as forcing a flush of the output buffer. Try either
changing the last printf statement (at the newline char to the end) to:

printf("You entered %d, the regular value is %c\n", in, in);

or you can leave the last printf statement unchanged but add
fflush(stdout);
just after it.

Also, take the whitespace out of the scanf format string.
scanf("%d",&in);
 
S

SithVegeta

Ah Crud

I tried both but I seems I still need to enter the value twice and it
still requires four getchar()'s to be there. Sorry for the trouble I just
started learning C and I've been reading "C Primer Plus (4th)" but I can't
seem to get past this review exercise in chapter three. I'll try entering
this into another compiler to see if I still get this problem.
 
S

SithVegeta

Well darn, I've tried just about everything I can think of. I can't discern
what the problem is. What did I miss?
 
J

Joe Wright

SithVegeta said:
Ah Crud

I tried both but I seems I still need to enter the value twice and it
still requires four getchar()'s to be there. Sorry for the trouble I just
started learning C and I've been reading "C Primer Plus (4th)" but I can't
seem to get past this review exercise in chapter three. I'll try entering
this into another compiler to see if I still get this problem.

#include <stdio.h>
int main(void)
{
int in;
printf("Please enter an ascii number to convert:\n");
scanf("%d", &in);
printf("You entered %d, the regular value is %c\n", in, in);
return 0;
}

I took out whitespace from the scanf string and removed the
getchar() statements and added '\n' to the printf().
 
S

SithVegeta

wow. I feel very small now. Thanks for you help, hopefully I'll get better
at this soon enough!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top