Assignment 1, Due at 11:59pm, Sunday Sep 07
Objectives
- Write your first non-trivial C++ program, making use of basic control
structures.
- Familiarize yourself with elementary processing of strings and files in C++.
- Think about program design: "design, then code."
It is very important that you think about the design of your program
before sitting down and coding.
What to do
You are to write a C++ program that does the following.
- It keeps reading user's inputs, line by line. Each input line the user
types is supposed to be in one of the following three forms:
numcols filename
numrows filename
exit
where
- If the user types
exit
, then your program quits.
- The other two commands
numrows
and numcols
reports the number of columns and the number of rows the file has.
- For example, if the file
test.txt
has the content as shown above, then the output of the program looks as follows.
> numrows test.txt
4
> numcols test.txt
7
> numcols anothertest.txt
6
> exit
Here, anothertest.txt
is some other file that has 6 columns.
Your program has to keep running until the user types exit
.
My implementation
I have written
a program named txtparser
following the above specification and compiled it under
timberlake
. You can download and run it (in
timberlake
) to see how it is supposed to work. You can download it by clicking on the link above (and upload the file to
timeberlake
), or you can log on to your CSE account on
timberlake
, then type
wget http://www.cse.buffalo.edu/~hungngo/classes/2014/Fall/250/assignments/txtparser
Remember to make it an executable file before trying to run it:
chmod 700 txtparser
You can also get two test files using the same method:
wget http://www.cse.buffalo.edu/~hungngo/classes/2014/Fall/250/assignments/test.txt
wget http://www.cse.buffalo.edu/~hungngo/classes/2014/Fall/250/assignments/anothertest.txt
How to submit
Put all files in a directory named
A1
, then follow
the steps similar to assignment 0. You
do need to construct a
Makefile
for it. If your
cpp
source file is named
txtparser.cpp
, then your
Makefile
might have the following content
all:
g++ txtparser.cpp -o txtparser
clean:
rm -f txtparser
To submit, perform similar steps as in assignment 0.
cd ..
tar -cvf A1.tar A1/*
submit_cse250 A1.tar
Note that the last line only works if you logged in to your CSE account and
the tar file is there. All previous things can be done at home, as long as
you remember to upload the final tar file to your CSE account and run
the submit script from there.
Grading
- 10 points if the
exit
command works. Your program only quits when exit
is typed.
- 40 points if the
numcols
command works, outputting the correct number of columns of the input file.
- 40 points if the
numrows
command works, outputting the correct number of rows of the input file.
- 10 points if your program can handle simple input errors from the users,
such as when the user types
numco file.txt
(unknown command)
or when file.txt
does not exist. Please run my implementation txtparser
to see the expected behavior.
Supporting materials
Read Chapter 1 of the textbook if you needed to. Read the
Getting started in C++ lecture note. The "reverse characters, reverse words" example already showed you how to get input lines from the user. The following snippet of code shows you how to read a file, one line at a time. Overall, you have all of the tools available to write this program.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string fname, line;
ifstream ifs; // input file stream
int i;
cout << "---- Enter a file name : ";
while (getline(cin, fname)) { // Ctrl-Z/D to quit!
// tries to open the file whose name is in string fname
ifs.open(fname.c_str());
if (ifs.fail()) {
cerr << "ERROR: Failed to open file " << fname << endl;
ifs.clear();
} else {
i = 0;
while (getline(ifs, line))
cout << "Line " << i++ << " : " << line << endl;
ifs.close(); // always remember to close it
}
cout << "---- Enter another file name : ";
}
return 0;
}