1. Usage of CIN
Char ch;
Cin. Get (CH); // only one character is accepted (input 12, CH is only assigned to 1)
Cout <ch;
2. Differences between the CH, ++ CH and CH + 1 variables
<Strong> int main () <br/>{< br/> char ch; <br/> cin. Get (CH); <br/> while (Ch! = '. ') <Br/>{< br/> If (CH =' \ n') <br/> cout <ch; <br/> else <br/> cout <CH + 1; <br/> cin. get (CH); <br/>}</strong>Cout <++ ch; the output is a character after the corresponding ascci code plus one
Cout <CH + 1; outputs the ascci code after the corresponding ascci code plus one
3. Clever Writing of if condition sentence
When writing an if (I = 0) Statement, the Code may encounter a logic error that is hard to be verified due to missing "=.
If (0 = I) is written. When a "=" is missing, an error is reported during code compilation, so it is easy to find the error source.
4. logical expressions (| ,&&,!)
The priority of a logical expression is lower than that of a relational expression. First, modify the value on the left and then determine the value on the right. For example, I ++ <6 | I = J
Test the scope of a variable: If (age> 15 & age <35)
Incorrect syntax: If (15 <age <35)
Analysis: when the value of 15 <age is true, the expression value is 1 <35 is always set
15 <age is false, the expression value is 0 0 <35 hengcheng Li
5. cctype (header file cctype. h)
Determine whether it is a letter: If (isalpha (CH ))
If (CH> = 'A' & Ch <= 'Z') | (CH> = 'A' & Ch <= 'Z '))
Isalnum () // determines whether it is a letter Array
Isblank () // determines whether it is a space active horizontal Tab
Isdigit () // determines whether it is a number
6 ,? : Operator
Int I = 5> 3? 1:0; // assign an I value to 1
7. Break and continue statements
Run the break statement to jump out of the loop body and execute the next statement other than the loop body.
The execution of the countinue statement indicates that the loop is skipped, but the loop update expression is not skipped.
8. Read the number Loop
Cin> I; is used to receive user input. Three steps should be taken when the user finds an input error
1) reset CIN to accept new input
2) Delete the incorrect input cin. Clear ();
3) prompt the user to enter
Example:
Cout <enter age <Endl;
Int age;
While (! (CIN> Age ))
{
Cin. Clear ();
Cout <enter again <Endl;
}
9. Simple file input/output
Text I/O: when using CIN for input, the program treats the input as a series of bytes, each of which is interpreted as character encoding. Regardless of the target data type, the input starts with character data-text data. Then, the CIN object is responsible for converting text to other types.
38.5 19.2
Char ch;
Cin> CH; // the first character 3 is assigned to CH, and the character encoding (Binary) is stored in the variable ch. The input and target are both characters and do not need to be converted.
Int N;
Cin> N; // CIN is read continuously until a non-numeric character is encountered. Read 38, so the binary code of 38 is copied to Variable N.
Double X;
Cin> X; // CIN is read continuously until the first character that does not belong to the floating point is encountered. Read 38.5
Char word [50];
Cin> word; // CIN is read continuously until blank characters are encountered. Read 38.5, store the 38.5 character encoding in the array word, and add an empty character at the end.
Cin. Getline (word, 50); // CIN is continuously read until a line break is encountered. Read 38.5 19.2
10. Steps for using file output
1) include the header file fstream
2) create an ofstream
3) associate the ofstream object into a file.
4) use ofstream just like using cout
Source code:
# Include <iostream> <br/> # include <fstream> <br/> int main () <br/> {<br/> using namespace STD; </P> <p> char automobile [50]; <br/> int year; <br/> double a_price; <br/> double d_price; </P> <p> ofstream OUTFILE; <br/> OUTFILE. open ("tianshuai.txt"); </P> <p> cout <"Enter the make and model of automobile:"; <br/> cin. getline (automobile, 50); <br/> cout <"Enter the model year:"; <br/> CIN> year; <br/> cout <"Enter the original asking price:"; <br/> CIN> a_price; <br/> d_price = 09.13 * a_price; </P> <p> cout <fixed; // output variable in the floating point format <br/> cout. precision (2); // set the precision to two digits after the decimal point <br/> cout. SETF (ios_base: showpoint); // forcibly display the decimal point <br/> cout <"make and model:" <automobile <Endl; <br/> cout <"year:" <year <Endl; <br/> cout <"was asking {1} quot; <a_price <Endl; <br/> cout <"now asking {1} quot; <d_price <Endl; </P> <p> OUTFILE <fixed; <br/> OUTFILE. precision (2); <br/> OUTFILE. SETF (ios_base: showpoint); <br/> OUTFILE <"make and model:" <automobile <Endl; <br/> OUTFILE <"Year: "<year <Endl; <br/> OUTFILE <" was asking {1} quot; <a_price <Endl; <br/> OUTFILE <"now asking {1} quot; <d_price <Endl; </P> <p> OUTFILE. close (); // close after the program uses the file <br/> return 0; <br/>}
11. Read text files
# Include <iostream> <br/> # include <fstream> <br/> # include <cstdlib> <br/> const int size = 60; <br/> int main () <br/> {<br/> using namespace STD; <br/> char filename [size]; <br/> ifstream infile; </P> <p> cout <"Enter name of data file:"; <br/> cin. getline (filename, size); <br/> infile. open (filename); // open the file <br/> If (! Infile. is_open () // if the file cannot be opened, <br/>{< br/> cout <"cocould not open the file" <FILENAME <Endl; <br/> cout <"program terminating. \ n "; <br/> exit (exit_failure); <br/>}< br/> double value; <br/> double sum = 0.0; <br/> int COUNT = 0; </P> <p> infile> value; // read the value in the file <br/> while (infile. good () <br/>{< br/> + + count; // number of values <br/> sum + = value; // calculate the sum of values <br/> infile> value; // continue to read the value <br/>}< br/> If (infile. EOF () // end of the file <br/> cout <"End of file reached. \ n "; <br/> else if (infile. fail () <br/> cout <"input terminated by Data Mismatch. \ n "; <br/> else <br/> cout <" input termiated for unknown reason. \ n "; <br/> If (COUNT = 0) <br/> cout <" no data processed. \ n "; <br/> else <br/>{< br/> cout <" items read: "<count <Endl; <br/> cout <"sum:" <sum <Endl; <br/> cout <"average:" <sum/count <Endl; <br/>}</P> <p> infile. close (); </P> <p> return 0; <br/>}12. Two important points
Char ch;
CH + 1 // char + Int = int type
Ch ++ // char type
In switch (char)
{Case A: break;
Case B: break;
In the default: break;} statement, what are the advantages of using characters to indicate menu options and case labels over using numbers?
Use characters (a, B, c ......) : If a number is entered, such as 3, 4, and 5, the program will process the data according to the characters, and the program itself will not report an error.
Use numbers (1, 2, 3 ......) : If you enter characters such as A, B, and C, the program reports an error.
Therefore, it is better to use characters.