How to ask the user if they wish to input another value in c
This is how i attempted it, how ever when i enter q it just skips a line in command and continues the program.
int main()
{
int a;
char c;
cont(&a);
while(a != 'q' && a != 'Q')
{
while ( ( c = getchar() ) != EOF)
{
putchar(开发者_运维知识库 r13( c ) );
}
}
return 0;
}
You need to pass the reference of a
to cont()
-
void cont(int* a)
{
printf("If you do not want to enter a value press q");
scanf("%c", a);
}
and call it like that:
cont(&a);
otherwise, only the the copy of a
(that is passed to the function) is changed, not a
itself.
If you want to change the value of a
in the function, then you need to store the return value somewhere, but you ignored it (e.g. a = cont(a);
).
Or, give a reference (e.g. address of) a to the function, so it will be able to change the value of a
.
精彩评论