The document discusses various errors that can occur when compiling C++ code and provides examples of errors, the code causing the errors, and error messages. Specifically, it discusses errors that can occur from issues like missing headers, incorrect syntax, dividing by zero, and misspelling variable or function names. The document is helping to demonstrate how to properly write and compile C++ code as well as common errors that may occur.
1. Enid M. Cruz-Colón
COMP 3010-030
HW 1: Compiler Toolchain
Problem 1
(a) The name of the executable program created by the compiler if the programmer does not
specify one is a.out.
(b) To compile a program and rename the executable file with a name other than the default
name, a.out, the command is:
g++ -o <new_name>.out <sourcecode_name>.cpp
Problem 2
The -Wall command line enables error warnings from the compiler when compiling your source
code.
The -c command line compiles a source code into an object file, not yet executable.
With the command g++ -o MyFirstProgram MyFirstSource.o we accomplish to change the
object code into machine code and store it in an executable program.
Problem 3
To change the program for it to greet the user with Hello, I inserted the following line, right
after the int number_of_pods, peas_per_pod, total_peas; line:
cout << “Hellon”;
To change the program for it to output Goodbye at the end, I inserted the following line, right
before the return 0; line:
cout << “Goodbyen”;
Problem 4
When replacing the “*” operator in the computation with a “/” and entering 0 for the number of
peas per pod, the compiler gives the following error: Floating point exception (core dumped). It
gives an error because the program is dividing the number of pods by peas per pod and you cannot
divide by 0.
Problem 5
When replacing the “/” operator in the computation with the “+” operator, the compiler does not
issue any errors, but the program gives a wrong answer.
Problem 6
(a) Error: Extra space between < and iostream.
Message: MyFirstSource.cpp:3:21: fatal error: iostream: No such file or directory
#include < iostream>
(b) Error: Omit < symbol in the #include directive.
2. Message: MyFirstSource.cpp:3:12: error: #include expects "FILENAME" or
<FILENAME>
#include iostream>
^
MyFirstSource.cpp: In function ‘int main()’:
MyFirstSource.cpp:8:3: error: ‘cout’ was not declared in this scope
cout << "Hellon";
^
MyFirstSource.cpp:11:3: error: ‘cin’ was not declared in this scope
cin >> number_of_pods;
^
(c) Error: Omit int from int main().
Message: MyFirstSource.cpp:5:10: warning: ISO C++ forbids declaration of ‘main’
with no type [-Wreturn-type]
main()
^
(d) Error: Misspell the word main.
Message: /usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../lib64/crt1.o: In function
`_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
(e) Error: Omit (.
Message: MyFirstSource.cpp:5:9: error: expected initializer before ‘)’ token
int main)
^
Error: Omit both ( and ).
Message: MyFirstSource.cpp:6:1: warning: extended initializer lists only available
with -std=c++11 or -std=gnu++11
{
^
MyFirstSource.cpp:7:3: error: expected primary-expression before ‘int’
int number_of_pods, peas_per_pod, total_peas;
^
MyFirstSource.cpp:7:3: error: expected ‘}’ before ‘int’
MyFirstSource.cpp:7:3: error: expected ‘,’ or ‘;’ before ‘int’
MyFirstSource.cpp:8:3: error: ‘cout’ does not name a type
cout << "Hellon";
^
MyFirstSource.cpp:9:3: error: ‘cout’ does not name a type
cout << "Press return after entering a number.n";
^
MyFirstSource.cpp:10:3: error: ‘cout’ does not name a type
cout << "Enter the number of pods:n";
^
3. MyFirstSource.cpp:11:3: error: ‘cin’ does not name a type
cin >> number_of_pods;
^
MyFirstSource.cpp:12:3: error: ‘cout’ does not name a type
cout << "Enter the number of peas in a pod:n";
^
MyFirstSource.cpp:13:3: error: ‘cin’ does not name a type
cin >> peas_per_pod;
^
MyFirstSource.cpp:14:3: error: ‘total_peas’ does not name a type
total_peas = number_of_pods + peas_per_pod;
^
MyFirstSource.cpp:15:3: error: ‘cout’ does not name a type
cout << "If you have ";
^
MyFirstSource.cpp:16:3: error: ‘cout’ does not name a type
cout << number_of_pods;
^
MyFirstSource.cpp:17:3: error: ‘cout’ does not name a type
cout << " pea podsn";
^
MyFirstSource.cpp:18:3: error: ‘cout’ does not name a type
cout << "and ";
^
MyFirstSource.cpp:19:3: error: ‘cout’ does not name a type
cout << peas_per_pod;
^
MyFirstSource.cpp:20:3: error: ‘cout’ does not name a type
cout << " peas in each pod, thenn";
^
MyFirstSource.cpp:21:3: error: ‘cout’ does not name a type
cout << "you have ";
^
MyFirstSource.cpp:22:3: error: ‘cout’ does not name a type
cout << total_peas;
^
MyFirstSource.cpp:23:3: error: ‘cout’ does not name a type
cout << " peas in all the pods.n";
^
MyFirstSource.cpp:24:3: error: ‘cout’ does not name a type
cout << "Goodbyen";
^
MyFirstSource.cpp:25:3: error: expected unqualified-id before ‘return’
return 0;
^
MyFirstSource.cpp:26:1: error: expected declaration before ‘}’ token
}
^
4. (f) Error: Misspell cin.
Message: MyFirstSource.cpp: In function ‘int main()’:
MyFirstSource.cpp:11:3: error: ‘cn’ was not declared in this scope
cn >> number_of_pods;
^