here is a section of code i have:
scanf( "%d", %26amp;choice1 ) ;
if ( choice1 %26lt; 1 || choice1 %26gt; 2 )
{
printf( "\n" );
printf( "ERROR: Please select either option 1 or option 2\n" );
printf( "\n" );
}
basically there are 2 options, scanf asks for a choice (either option 1 or 2). If the input value is not a 1 or a 2 it prints the error message but i then want it to go back to the scanf part so the user can have another go at inputting either option 1 or 2. Ive been told i need to use a while loop? any help much appreciated...
thanks
How can i repeat a scanf command in C?
whoever told you to use a while loop is correct...
scanf("%d", %26amp;choice1);
while((choice1 != 1) %26amp;%26amp; (choice1 != 2))
printf("\n");
printf("Error blah blah\n");
scanf("%d", %26amp;choice1);
}
Reply:Yes, a while() loop will work fine. THere are a couple of ways to structure it...
while (choice !=1 %26amp;%26amp; choice!=2) {
...your code...
}
or, you could have a do...while loop...
do {
...your code...
} while ( choice!=1 %26amp;%26amp; choice!=2);
or, you could include the scanf within the while portion.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment