Home > Back-end >  C check of input data
C check of input data

Time:09-22

In the "letter to you to look at c + +", which to some questions, come to help,
The book mentioned the cin object input check can use the
STD: : cin. Ignore (100), '\ n');
Or STD: : cin. Ignore (STD: : cin gcount () + 1);
To delete the contents of the buffer than
However, it took me two times, found that there is a small problem, let me can not its solution,
For example, in the use of STD: : cin. Ignore (STD: : cin gcount () + 1); This statement, sometimes need to + 1 to reach the expected effect, but sometimes don't need a + 1,

My question is:
1, STD: : cin. Ignore (STD: : cin gcount ()); This statement will be before the input buffer is empty, including emptying the last carriage return? If included, why the STD: : cin. Ignore (STD: : cin gcount () + 1); Need + 1 statement, what's the difference between the two words?
3, STD: : cin. Ignore (100), '\ n'); In use, when the first '\ n' after the stop, then the '\ n' will be cleared or stay in the buffer?
2, the input buffer and output buffer is independent each other?


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Below is my program, STD: : cin. Ignore (100), '\ n'); And STD: : cin. Ignore (STD: : cin gcount () + 1); Under the circumstances of the first input saplings,
Execution results different, want to ask what's the difference between the two words of specific,
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include & lt; Iostream>

Void PromptAndWait ();
Void DollarsToEuros (float rate, unsigned dollars);
Void DollarsToEuros (float rate, float dollars);

Int main (int arg c, char * * argv) {

Float ConversionRate=0.832339;
Unsigned DollarsIn;
Float DollarsInFloat;

STD: : cout & lt; <"Enter a US dollar amount (without the dollar sign, commas or a decimal) :/# # # #";
STD: : cin & gt;> DollarsIn;

DollarsToEuros (ConversionRate DollarsIn);

//STD: : cin. Ignore (100), '\ n');
STD: : cin. Ignore (STD: : cin gcount () + 1);


STD: : cout & lt; <"Enter a US dollar amount (without the dollar signs or commas) :/. # # # # # #";
STD: : cin & gt;> DollarsInFloat;

DollarsToEuros (ConversionRate DollarsInFloat);

PromptAndWait ();

return 0;
}

Void PromptAndWait () {

STD: : cin. Ignore (100), '\ n');
STD: : cout & lt; <"Press Enter or Return to the continue. \ n";
STD: : cin. The get ();
}//End of the PromptAndWait () funcation.

Void DollarsToEuros (float rate, unsigned dollars) {
STD: : cout. Setf (STD: : ios_base: : fixed);
STD: : cout. Setf (STD: : ios_base: : showpoint);
STD: : cout. Precision (2);

STD: : cout & lt; <"\ n $" & lt; <"US=" & lt; <(rate * dollars)
<"Euros. \ n \ n";
}//End of the DollarsToEuros () funcation.

Void DollarsToEuros (float rate, float dollars) {
STD: : cout. Setf (STD: : ios_base: : fixed);
STD: : cout. Setf (STD: : ios_base: : showpoint);
STD: : cout. Precision (2);

STD: : cout & lt; <"\ n $" & lt; <"US=" & lt; <(rate * dollars)
<"Euros. \ n \ n";
}//End of the DollarsToEuros () funcation (overload).


CodePudding user response:

Red's implementation effect, also please your comments!

CodePudding user response:

reference 1st floor qq_29506411 response:
red's implementation effect, also please your comments!


Under the condition of the first input floating point number, the result is not the same!
  • Related