Write a program called ‘Names’, which does the following: You are given a text f
ID: 3766035 • Letter: W
Question
Write a program called ‘Names’, which does the following:
You are given a text file that contains the names of people. Every name in the file consists of a first name and a last name. Somehow the file got corrupted and the format of the names got mixed up.
Your program will read the given file HW9_FiloIO.txt for input, but prompt the user for the desired output file name.
Your program will read in the names from the input file and write them back to the output file ensuring that each line contains a single last name, followed by a comma and a single first name.
Your program will also append an identifier to the name in the file.
For example, if the input file contains:
Cheryl Adams
Tennessee Williams
Isaac Asimov
Lincoln Orson
Scott Card
The output file should be:
Adams, Cheryl
Williams, Tennessee
Asimov, Isaac
Orson, Lincoln
Card, Scott
Explanation / Answer
Write the Source Code: Enter the following source codes using a programming text editor (such as NotePad++ for Windows or gedit for UNIX/Linux/Mac) or an Interactive Development Environment (IDE) (such as CodeBlocks, Eclipse, NetBeans or Visual Studio - Read the respective "How-To" article on how to install and get started with these IDEs).
Do not enter the line numbers (on the left panel), which were added to help in the explanation. Save the source file as "hello.cpp". A C++ source file should be saved with a file extension of ".cpp". You should choose a filename which reflects the purpose of the program.
Step 2: Build the Executable Code: Compile and Link (aka Build) the source code "hello.cpp" into executable code ("hello.exe" in Windows or "hello" in UNIX/Linux/Mac).
· On IDE (such as CodeBlocks), push the "Build" button.
· On Text editor with the GNU GCC compiler, start a CMD Shell (Windows) or Terminal (UNIX/Linux/Mac) and issue these commands:
where g++ is the name of GCC C++ compiler; -o option specifies the output filename ("hello.exe" for Windows or "hello" for UNIX/Linux/Mac); "hello.cpp" is the input source file.
Step 3: Run the Executable Code: Execute (Run) the program.
· On IDE (such as CodeBlocks), push the "Run" button.
· On Text Editor with GNU GCC compiler, issue these command from CMD Shell (Windows) or Terminal (UNIX/Linux/Mac):
Brief Explanation of the Program
/* ...... */
// ... until the end of the line
These are called comments. Comments are NOT executable and are ignored by the compiler; but they provide useful explanation and documentation to your readers (and to yourself three days later). There are two kinds of comments:
1. Multi-line Comment: begins with /* and ends with */. It may span more than one lines (as in Lines 1-3).
2. End-of-line Comment: begins with // and lasts until the end of the current line (as in Lines 4, 7, 8, 9 and 10).
#include <iostream>
using namespace std;
The "#include" is called a preprocessor directive. Preprocessor directives begin with a # sign. They are processed before compilation. The directive "#include <iostream>" tells the preprocessor to include the "iostream" header file to support input/output operations. The "using namespace std;" statement declares std as the default namespace used in this program. The names cout and endl, which is used in this program, belong to the std namespace. These two lines shall be present in all our programs. I will explain their meaning later.
int main() { ... body ... }
defines the so-called main() function. The main() function is the entry point of program execution. main() is required to return an int (integer).
cout << "hello, world" << endl;
"cout" refers to the standard output (or Console OUTput). The symbol << is called the stream insertion operator (or put-to operator), which is used to put the string "hello, world" to the console. "endl" denotes the END-of-Line or newline, which is put to the console to bring the cursor to the beginning of the next line.
return 0;
terminates the main() function and returns a value of 0 to the operating system. Typically, return value of 0 signals normal termination; whereas value of non-zero (usually 1) signals abnormal termination. This line is optional. C++ compiler will implicitly insert a "return 0;" to the end of the main() function.
2. C++ Terminology and Syntax
Statement: A programming statement performs a piece of programming action. It must be terminated by a semicolon (;) (just like an English sentence is ended with a period), as in Lines 5, 8 and 9.
Preprocessor Directive: The #include (Line 4) is a preprocessor directive and NOT a programming statement. A preprocessor directive begins with hash sign (#). It is processed before compiling the program. A preprocessor directive is NOT terminated by a semicolon - take note of this unusual rule.
Block: A block is a group of programming statements enclosed by braces { }. This group of statements is treated as one single unit. There is one block in this program, which contains the body of themain() function. There is no need to put a semicolon after the closing brace.
Comments: A multi-line comment begins with /* and ends with */, which may span more than one line. An end-of-line comment begins with // and lasts till the end of the line. Comments are NOT executable statements and are ignored by the compiler; but they provide useful explanation and documentation. Use comments liberally.
Whitespaces: Blank, tab, and newline are collectively called whitespaces. Extra whitespaces are ignored, i.e., only one whitespace is needed to separate the tokens. Nevertheless, extra white spaces and newlines could help you and your readers better understand your program. Use extra whitespaces and newlines liberally.
Case Sensitivity: C++ is case sensitive - a ROSE is NOT a Rose, and is NOT a rose.
3. The Process of Writing a C++ Program
Step 1: Write the source codes (.cpp) and header files (.h).
Step 2: Pre-process the source codes according to the preprocessor directives. Preprocessor directives begin with a hash sign (#), e.g., #include and #define. They indicate that certain manipulations (such as including another file or replacement of symbols) are to be performed BEFORE compilation.
Step 3: Compile the pre-processed source codes into object codes (.obj, .o).
Step 4: Link the compiled object codes with other object codes and the library object codes (.lib,.a) to produce the executable code (.exe).
Step 5: Load the executable code into computer memory.
Step 6: Run the executable code, with the input to produce the desried output.
4. C++ Program Template
You can use the following template to write your C++ programs. Choose a meaningful filename for you source file that reflects the purpose of your program with file extension of ".cpp". Write your programming statements inside the body of the main() function. Don't worry about the other terms for the time being. I will explain them later.
5. Output via "cout <<"
In C++, output to the display console is done via "cout" and the stream insertion (or put-to) operator <<. You can print as many items as you wish to cout, by chaining the items with the << operator. For example,
A special symbol called endl (END-of-Line) can be used to produce a newline. Whenever an endl is printed, there is no visible output, but the cursor advances to the beginning (left-margin) of the next line. A string, which is enclosed by a pair of double quotes, will be printed as it is, including the white spaces and punctuation marks within the double quotes. Integers (such as 1, 2, 3) and floating-point numbers (such as 1.1, 2.2) can be printed too. The output for the above two output statements is as follows where the underscore denotes the final cursor position.
Beside the endl, you can also use ' ', which denotes a newline character, to advance the cursor to the next line. Similarly, you could use ' ', which denote a tab character, to advance the cursor to the next tab position. ' ' and ' ' are known as escape sequences representing ASCII codes Hex 0A (line-feed) and Hex 09 (tab), respectively. For example,
The output shall look like (the exact tab stop positions depend on your system's setting - eight spaces is used here):
1
2
3
4
5
6
7
8
9
10
/*
* First C++ program that says hello (hello.cpp)
*/
#include <iostream> // Needed to perform IO operations
using namespace std;
int main() { // Program entry point cout << "hello, world" << endl; // Say Hello
return 0; // Terminate main()
} // End of main function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.