English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

here is a section of code i have:

scanf( "%d", &choice1 ) ;

if ( choice1 < 1 || choice1 > 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

2007-02-21 08:08:41 · 4 answers · asked by Johnno 2 in Computers & Internet Programming & Design

4 answers

whoever told you to use a while loop is correct...

scanf("%d", &choice1);

while((choice1 != 1) && (choice1 != 2))
printf("\n");
printf("Error blah blah\n");
scanf("%d", &choice1);
}

2007-02-21 08:27:40 · answer #1 · answered by bytekhan 2 · 0 0

Scanf Command

2016-12-18 07:45:23 · answer #2 · answered by Anonymous · 0 0

Yes, a while() loop will work fine. THere are a couple of ways to structure it...

while (choice !=1 && choice!=2) {

...your code...

}

or, you could have a do...while loop...

do {

...your code...
} while ( choice!=1 && choice!=2);

or, you could include the scanf within the while portion.

2007-02-21 08:23:37 · answer #3 · answered by BigRez 6 · 0 0

now no longer anymore, at one time C++ develop into truly an extension of C. pondering that then, C has replaced, and so has C++. regardless of the easy shown truth that that being said it really is widely used for the similar application to collect both C and C++. the frame of mind is the similar, yet there are truly some ameliorations int he libraries that you want the compiler to link to.

2016-12-04 11:34:02 · answer #4 · answered by ? 4 · 0 0

fedest.com, questions and answers