Reputation: 17
#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
Upvotes: 0
Views: 2314
Reputation: 112
Try this.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main(void) {
char c='y';
while(c!='n') {
while(!kbhit()) {
printf("Hello\n");
}
c=getch();
}
}
Please note that I have not compiled this as conio.h is not available to me right now.
Upvotes: 0
Reputation: 16540
the posted code does not compile!
The following code will do the job.
Notice that the goto
is eliminated
Notice that the unneeded variables are eliminated
Notice that the appropriate header files are included
Notice the signature for the main() function is corrected
#include <stdio.h> // printf()
#include <conio.h> // getch() kbhit() <-- use correct header file
int main( void ) // <-- use valid signature
{
// <-- eliminate unneeded variables
while(1) // <-- non-confusing (and simple) loop statement
{
printf("Hello\n");
if( kbhit() )
{ // then some key has been pressed
if( 'n' == getch() )
{ // then 'n' key has been pressed
break; // <-- exit the loop
}
}
}
} // end function: main
Upvotes: 1
Reputation: 34585
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
.
Upvotes: 1