I received a homework assignment that asked the following:
Write a program called country.cpp to read a text file that contains the names and sizes of countries. The program should prompt the user to specify the file name that contains the data. If the file opens successfully, the data should be displayed on the screen in the order that it is read from the file. After reading all entries, the program must display the name of the smallest country on the list.
You are given two sample input files (countriesLarge.txt. and countriesSmall.txt) you may use to test your program.
The following hold:
• The number of items on a file is unknown.
• Each line on the file contains a name, followed by a space and a size.
• The country names to not contain spaces or punctuation.
• The value representing the size is the area of the country in km 2 .
• The sizes are given as real numbers, but should be displayed as integers (rounded).
The output of your program should comply with the following specifications:
• The user should enter the file name on the same line as the prompt.
• Put a line of dashes directly under the prompt.
• Leave no open lines.
• The entries should be numbered 1, 2, 3, . . . , right justified in 3 spaces.
• Leave two spaces after the number and then display the country name left justified in 30 spaces.
• Following the name of the country, display it’s size right justified in 10 spaces.
• Put a line of dashes directly under the list of countries.
• Finally display the name of the smallest country on the list using a full sentence below this line of dashes.
The following is a sample test run.
Give the file name: countriesSmall.txt
------------------------------------------------------
1 Monaco 2
2 Grenada 344
3 Malta 316
4 Maldives 300
5 Vatican 0
6 Nauru 21
7 Tuvalu 26
8 Liechtenstein 160
------------------------------------------------------
The smallest country on this list is Vatican
Could someone please help me in writing this code? I would appreciate it soooooooooo much. I am currently struggling so bad. Oh, and the code should please be in C++. I am doing a writtena assignment.
What have you code so far? What are you struggling with? Break the assignment into parts. If you post the work you have done, then we could help you. Good luck!
In addition to chicofeo's good advice, I'll suggest that first you work on getting the correct output, without worrying about the exact formatting. This will help you get the logic right. Once you have that done, you can work on the formatting.
I haven't coded anything yet. I took this subject (programming) for extra credit, but I took it later than everyone else. The lecturer said that I should ask this forum for help to catch up with the work. So I posted here, because I am completely clueless in what to do :( .
It is not important why you thought programming is a subject to start late. All it means is that you have to catch up with the rest of the class. Below are some food for thoughts to tackle the homework
Write a program called country.cpp to read a text file that contains the names and sizes of countries.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
usingnamespace std;
int
main()
{
ifstream inputFile;
string filename;
// current country name and size
string countryName;
double countrySize;
// smallest country name and size
string smallestCountry;
double smallestSize = 1E100;
int num = 0;
cout << " Give the file name: ";
cin >> filename;
cout << "---------------------------------------------------------------------\n";
inputFile.open(filename.c_str());
if (inputFile) {
while (inputFile >> countryName) {
cout << right << setw(3) << ++num
<< " "
<< left << setw(30) << countryName;
inputFile >> countrySize;
if (countrySize < smallestSize) {
smallestSize = countrySize;
smallestCountry = countryName;
}
cout << right << setw(10) << static_cast < int >(countrySize) << endl;
}
cout << "-------------------------------------------------------------\n";
cout << "The smallest country on the list is " << smallestCountry << endl;
} else {
cout << "Error opening the file.\n";
}
return 0;
}
- I renamed Small to countryName, which is more meaningful.
- The code to pick the smallest country was buggy. I fixed it (lines 36-39). Read this code and understand it. The trick is that I initialize smallestSize to a very large number (10100).
- The code is hard to read (for me anyway) because the logic is:
1 2 3 4 5
read country name
write country name
read country size
determine smallest country details
write country size
I think it would flow better like this:
1 2 3
read a line from the input file.
write a line to the output file
determine smallest country details