2014 Autumn C + + 5th week Project 1 Reference-see beginners common mistakes

Source: Internet
Author: User

Course Homepage in http://blog.csdn.net/sxhelijian/article/details/39152703, practice requirements See http://blog.csdn.net/sxhelijian/article/ details/39493833.

Curriculum Resources in the Cloud Academy "teacher class" synchronized display, the use of the account number, please go to the course homepage to view.

"Project 1" below is the most classic and simplest C + + program. Run the program in Ideone.com and, based on the programs that work correctly, make some mistakes, and watch them. Experience is from the wrong, this project is to let everyone accumulate this kind of experience.

#include <iostream>  using namespace std;  int main ()  {         int a,b,sum;      cin>>a>>b;       Sum=a+b;       cout<< "a+b=" <<sum<<endl;       return 0;  

(1) cin>>a>>b; the 6th line The semicolon at the end of a line removes the error to be made as follows (after a "nuisance" ends, the program is corrected again, continuing to "mess up"):
(2) the 5th line int a,b,sum is changed to int a, b;, that is, when declaring a variable, the sum is not declared
(3) Write the 8th line cout<< as COUT<< Note the case in the previous statement
(4) write the 8th line cout<< in order to cout>>
(5) Sum=a+b the 7th line; Changed to Sum=ab; The plus sign is missing.
(6) Delete the last closing curly brace of the 10th line of the program
(7) the 5th line int a,b,sum; instead of IMT a,b,sum; Note that a little hand may shiver, and int becomes IMT
(8) You are a bad boy than Mr. Ho, and think of some trouble


Reference Answer:

Description

    • The following hint is the error hint in ideone.com, which may have different descriptions in other platforms, but the meaning is generally the same;
    • In view of the students just learn, can not use too professional and in-depth explanation, the following tips only the pursuit of students have experience can, with the progress of learning, you can give a complete explanation.

Various explanations after the change:

(1) cin>>a>>b; the 6th line Remove the semicolon at the end of the line
The system prompts for "failed to edit (CE)" specifically:
1.prog.cpp:In function ' int main () ':
2.Prog.cpp:7:5:error:expected '; ' Before ' Sum '
3.Sum=a+b;
4.^
The 1th line prompts "in function ' int main () '", indicating that an error occurred in the function int main ()
The 2nd line, "7:5:" means that the cause of this error, and the 7th line to the 5th line of the program related, this is an error (error), because "expected"; ' Before ' Sum ' ", that is," you want a semicolon before sum. " Every sentence in the program must have a semicolon, the 6th line at the end of the division, accounts to the 7th line! This fact is to be accepted. Modify the method, as prompted, before sum, which is the end of the previous line, plus a semicolon.
The 3rd line states that the error was found here in "Sum=a+b".
The "^" in line 4th points to the exact location of the problem.

(2) the 5th line int a,b,sum is changed to int a, b;, that is, when declaring a variable, the sum is not declared
"Editing Information" section tips
1.prog.cpp:In function ' int main () ':
2.Prog.cpp:7:5:error: ' Sum ' is not declared in this scope
3.Sum=a+b;
4.^
The 1th line prompts "in function ' int main () '", indicating that an error occurred in the function int main ()
Line 2nd, "7:5:" refers to the cause of this error, and the 7th line to the 5th line of the program related, this is an error (error), because "' sum ' is not declared in this scope", that is, "sum is not declared within this scope", indeed, The 7th line of the program to use sum, but not previously declared, the error and the 7th line, the program 5th line is a declaration statement, if declared here, also will not error, said and 5th line, also reasonable.
The 3rd line states that the error was found here in "Sum=a+b".
The "^" in line 4th points to the exact location of the problem.

(3) Write the 8th line cout<< as COUT&LT;&LT; Note the case of the preceding statement
The 2nd line of the prompt is:
Prog.cpp:8:5:error: ' Cout ' is not declared in this scope
C + + Vocabulary, the case is strictly differentiated, the compiler knows "cout", encountered "cout" do not know, said "cout in this range is not declared. "Calm down, don't follow the prompts to declare cout, but instead change the capitalization to lowercase."

(4) write the 8th line cout<< in order to cout>>
The 2nd line of the hint indicates:
Prog.cpp:8:9:error:no match for ' operator>> ' (#@#&*!$# ... )
This is an operator mismatch. Cout should be matched with <<, and the >> match is CIN.

(5) Sum=a+b the 7th line; Changed to Sum=ab; The plus sign is missing.
The 2nd line of the hint indicates
Prog.cpp:7:9:error: ' AB ' is not declared in this scope
Omitted to write the plus, AB is a new variable name, this variable cannot escape "not declared."

(6) Delete the last closing curly brace of the 10th line of the program
The 2nd line of the hint indicates
Prog.cpp:9:13:error:expected '} ' at end of input
There must be a closing parenthesis to match the opening parenthesis, which is quite accurate.

(7) the 5th line int a,b,sum; instead of IMT a,b,sum; Note that a little hand may shiver, and int becomes IMT
There was a bunch of hints, "one wrong hundred wrong"? int is the name of the data type, written in IMT, the system thought is the variable name, is the variable name, must be declared in advance, but not, so the first hint:
Prog.cpp:5:5:error: ' IMT ' is not declared in this scope
So this line can not be used as a declaration variable to speak, need to have a semicolon, a, B, sum "not declared", such as errors, followed.
Note that when int is written, it is blue, which is called "syntax coloring" for the composition of the language itself. IMT has no such characteristics, and this is an experience to accumulate.


=================== Helijian csdn Blog column =================|== It student Growth Guide Column column categories (not regularly updated) ==| | = = C + + Classroom Online Column The course teaching link (sub-course grade) ==| | = = I wrote the book-"The reverse of the university-to the positive energy of IT students" ==|===== for it rookie runway, and students enjoy a happy and passionate university =====




2014 Autumn C + + 5th week Project 1 Reference-see beginners common mistakes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.