Maddy Maddy - 17 days ago 5
C Question

Print a message till a specific char(in this case'n') is pressed

#include <stdio.h>
void main()
{
int a=1;
char c;
x:for(a=1;a!=0;a++)
{
printf("Hello\n");
c=getch();
if(c=='n')
exit(0);
else
goto x;
}
}


//please assist me with this program by using primary operators only

Answer

This is a little different, to show you a simple solution. But if you are not allowed to use kbhit you are stuck.

#include <stdio.h>
#include <conio.h>              // include the library header

int main(void)                  // correct signature for main
{
    int c = 0;                  // note getch() returns `int` type
    while(c != 'n')             // until correct key is pressed
    {
        do {                    // forever
            printf("Hello\n");
        } while(!kbhit());      // until a key press detected
        c = getch();            // fetch that key press
    }
    return 0;
}

Remember, it only tests for lower-case n.

Comments