Home > Back-end >  C language for help.
C language for help.

Time:02-19

This program is to stay in a hotel, input the correct number of days, only captures the input number function part,
There are two don't understand.
(1) the conditions in the while loop, I understood as when days are 1, not into the circulation, enter not into the loop when 1 (but is not in conformity with the simply enter again the purpose of the correct number of days),
Can run, no matter what I input Numbers (including 1) will not enter the circulation, (that is, the application meets the requirements)
That is, the program is right, is to meet the requirements, but I understand is wrong, for the

2 cycles of the scanf (' * '% s')
What is the role of (1) *
(2) why do you want to join this statement here? (after I get rid of this statement, the program may enter the infinite loop, I can't understand why)

CodePudding user response:

The scanf data obtained from the return value is the correct item number,
Circulation inside the scanf is to remove the input buffer,
With a * is one of the input is said that the, but after input to throw away, don't send to variables, so no follow behind accept input address,

CodePudding user response:

reference 1st floor forever74 response:
the scanf data obtained from the return value is the correct item number,
Circulation inside the scanf is to remove the input buffer,
With a * is one of the input is said this, but after input to throw away, don't send to variables, so no follow behind accept input address,

* function is learned, the rest still do not understand,,

CodePudding user response:

With this code runs and see:
 # include & lt; stdio.h> 
Int main ()
{
Int I, j, days1 days2, days3;

Printf (" input days1: ");
I=the scanf (" % d ", & amp; Days1);
Printf (" I=% d, dyas1=% d \ n ", I, days1);

Printf (" input days2 days3: ");
J=the scanf (" % d % d ", & amp; Days2, & amp; Days3);
Printf (" j=% d, dyas2=% d, days3=% d \ n ", j, days2, days3);

system("pause");
return 0;
}

//input days1:5
//I=1, dyas1=5
//input days2 days3:6 8
//j=2, dyas2=6, 8 days3=
//please press any key to continue...

CodePudding user response:

reference QZJHJXJ reply: 3/f
Lord put this code runs and see:
 # include & lt; stdio.h> 
Int main ()
{
Int I, j, days1 days2, days3;

Printf (" input days1: ");
I=the scanf (" % d ", & amp; Days1);
Printf (" I=% d, dyas1=% d \ n ", I, days1);

Printf (" input days2 days3: ");
J=the scanf (" % d % d ", & amp; Days2, & amp; Days3);
Printf (" j=% d, dyas2=% d, days3=% d \ n ", j, days2, days3);

system("pause");
return 0;
}

//input days1:5
//I=1, dyas1=5
//input days2 days3:6 8
//j=2, dyas2=6, 8 days3=
//please press any key to continue...

Understand, understand, a great god,

CodePudding user response:

 # include & lt; stdio.h> 
Int main ()
{
Int days;
Printf (" how many days do you want to live?" );
While (the scanf (" % d ", & amp; Days)!=1)//when the input is not number, enter the loop body, until prompted to input, input digital
{//but there are still loopholes, input two Numbers, it is recognized a calculated to complete the task,
* the scanf (" % s ");
Printf (" both please enter an integer. \ n ");
}
Printf (" days=% d \ n ", days);
system("pause");
return 0;
}


//how many days do you want to live? A
//both please enter an integer.
//8 and 9
//8 days=
//please press any key to continue...
  • Related