It is necessary for the cycle to work again at 0 n 3. My program is looping.
for( a = 0; a < 1; a++ ) { scanf( "%i", &n ); if( n == 1 ) i=1; if( n == 2 ) i=2; if( n == 3 ) i=3; if( n < 1 || n > 3 ) { puts( "давай сначала" ); a = -1; i=0; } } Since the same cycle can be written in different ways, there are many approaches to solving your problem. For example, a loop might look like this. Keep in mind that the user may interrupt input, and you should handle this situation.
n = 0; while ( scanf( "%i", &n ) == 1 && ( n < 1 || n > 3 ) ) { puts( "давай сначала" ); n = 0; } i = n; or the last sentence, depending on the context, can be replaced by
if ( n != 0 ) i = n; Or if in real code you do not have such a straightforward assignment as
if( n == 1 ) i=1; and there is some dependence of the value of the variable i on the enumeration of the values of the variable n , then you can write
int valid = 0; while ( !valid && scanf( "%i", &n ) == 1 ) { valid = 1; switch( n ) { case 1: i = 1; break; case 2: i = 2; break; case 3: i = 3; break; default: puts( "давай сначала" ); valid = 0; } } Or
int valid = 0; while ( !valid && scanf( "%i", &n ) == 1 ) { valid = 1; switch( n ) { case 1: case 2: case 3: i = n; break; default: puts( "давай сначала" ); valid = 0; break; } } You can enter enumerations to enter mnemonic names for magic numbers 1, 2, 3. For example, if these values correspond to the items of a menu, then you can write, let's say
enum { Exit = 0, Add = 1, Division = 2, Multiply = 3 }; i = Exit; while ( i == Exit && scanf( "%i", &n ) == 1 ) { switch( n ) { case Add: case Division: case Multiply: i = n; break; default: puts( "давай сначала" ); break; } } while (true) { scanf("%d", &n); ... if (n >= 1 && n <= 3) break; puts("давай сначала"); } for(;;puts( "давай сначала" )) { int n = scanf( "%i", &i ); scanf("%*[^\n]s"); if( n != 1 || (i >= 1 && i <= 3)) break; } i , and then rewrite it with the uninitialized variable n ? - Grundyn is entered into it, and then this value is assigned to i . so he has it all right! - Grundy do { scanf( "%i", &i ); if (i < 1 || i > 3) puts("давай сначала"); } while (i < 1 || i > 3) Source: https://ru.stackoverflow.com/questions/598107/
All Articles
что-то( n ) = 0is this how to understand? - PinkTux0to0? - slippyk