what my code does is it waits for the user to type in "led" and performs the blink function. what i am trying to do is introduce the letter 'p' as a command to stop the blinking. what happens is that blink is only executed once.

if (!(strcmp(userinput, led))){		//condition 1
	for(howlong = 0; howlong++ < 0xFF; howlong++){		//condition 2	
		blink();
		if (getch() == 'p')
		break;
	}
}

what my code does is it waits for the user to type in "led" and performs the blink function. what i am trying to do is introduce the letter 'p' as a command to stop the blinking. what happens is that blink is only executed once.

if (!(strcmp(userinput, led))){		//condition 1
	for(howlong = 0; howlong++ < 0xFF; howlong++){		//condition 2	
		blink();
		if (getch() == 'p')
		break;
	}
}

getch() is probably picking up the '\n' in the input buffer...Try replacing if (getch() == 'p') with

fprintf(stdout, "char->%x\n", getch());

What's the value displayed?

You've used a finite loop which may make it appear that break is being called and that blink is only being called once, I.E. it'll loop 256 times and then come out of the for loop. I suspect you want something like this:

if (!(strcmp(userinput, led))){		//condition 1
	for(;;){		//condition 2	
		blink();
		if (getch() == 'p')
		break;
	}
}

Remove the first ++ from howlong++.

getch() returns it's value to the left hand side (the Lvalue), of the equation:

char1 = getch();
if(char1== 'p')
  break;

Would be how I'd do it. But there's another problem which is always present when scanf() is involved (potentially), and that is clearing out the newline char after the scanf(), prior to the scanf() for a char.

numbers are not bad, since scanf() will skip over a newline, but scanf() can't possibly skip over a newline when it's there to get a char - and this may be your real problem.

So add a getchar() just before the scanf() for the char, and see if that helps. This is UNDOUBTEDLY the most asked topic on the C forums, for beginners.

Since you can't see the newline on the keyboard buffer, it's a shock for C students to learn that scanf() leaves the newline in the kb buffer, and it causes problems, if you don't pull it off, (getchar() is one good way).

overall, don't get too addicted to scanf(). It's too "brittle" for user's to be trusted with.

@gerard4143
im using a zds compiler and the fprintf doesnt seem to be supported even with the stdio and sio libraries included

@Auraomega
sadly it still only blinked once sir

@adak
sadly sir the problem wasnt solved.

So add a getchar() just before the scanf() for the char, and see if that helps

----> sadly, i dont quite get it .can you please show me how this is to be done, if that is possible.

I'm not sure, If I'm actually helping since I'm still learning c myself. What I would suggest is that you test for 'p' before executing the loop.

This is your code:

if (!(strcmp(userinput, led))){		//condition 1
	for(howlong = 0; howlong++ < 0xFF; howlong++){		//condition 2	
		blink();
		if (getch() == 'p')
		break;
	}
}

You would need to create new variable.

int c;

Try this:

if (!(strcmp(userinput, led))){		//condition 1
	for(howlong = 0; (c=getch()) != 'p' && howlong++ < 0xFF; howlong++)	//condition 2	
		blink();

	
}

This way you eliminate the break statement and if-then. Also, the for tests for 'p' before blinking.

Thanks, to whom? what did you implement to get it to work properly?

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.