15.18. Mixed Up Code Practice¶
We have a file called "locations.txt" that we want to read data from. Check to make sure that the file was opened properly; if it wasn't, display an error message and exit with a status of 1. Put the necessary blocks of code in the correct order.
-
if (infile.good() == false) { -
if (infile.good()) { -
int main() { -
return 1; -
std::cout << "Unable to open the file." << std::endl; -
std::exit(1); -
std::ifstream infile("locations.txt"); -
std::ifstream infile(locations.txt); -
} -
}
Let's write a program that prompts the user for a filename and opens that file. Put the necessary blocks of code in the correct order.
-
if (infile.good() == false) { -
int main() { -
std::cin >> filename; -
std::cout << "Enter the name of the file: "; -
std::cout << "Unable to open the file." << std::endl; -
std::exit(1); -
std::ifstream filename; -
std::ifstream infile(filename); -
std::ofstream infile(filename); -
std::string filename; -
} -
}
Now let's write some output to a file. Write a program that prompts
a user for a list of 5 integers separated by spaces, calculates the
average of those integers, and outputs "The average is average"
to an output file called "average.txt". Put the necessary blocks
of code in the correct order. Declare the output file first and
check that it is opened correctly.
-
if (outfile.good() == false) { std::cout << "Unable to open the file." << std::endl; std::exit(1); } -
int main() { -
int n1, n2, n3, n4, n5; -
int sum = 0; -
outfile << "The average is " << sum / 5.0 << std::endl; -
std::cin >> n1 >> n2 >> n3 >> n4 >> n5; -
std::cout << "Enter five integers separated by spaces: "; -
std::cout << "The average is " << sum / 5.0 << std::endl; -
std::ofstream outfile("average.txt"); -
std::vector<int> list; #distractor -
sum = n1 + n2 + n3 + n4 + n5; -
}
We are given a file called "data.txt" with an unknown number of
double values. Write a program that finds the minimum, maximum,
and number of data and outputs these values to a file called
"summary.txt". Put the necessary blocks of code in the correct order.
Declare the input and output files first, and check to see that
both are opened correctly before dealing with data. Increment the
number of data points before checking for the min and max.
-
++num_data; -
double min, max, value; -
if (!(infile >> value)) { return 1; } -
if (infile.good() == false || outfile.good() == false) { std::cout << "Unable to open a file." << std::endl; std::exit(1); } -
if (value < min) { min = value; } -
if (value > max) { max = value; } -
int main() { -
max = value; -
min = value; -
outfile << "Number of data: " << num_data << ", min: " << min << ", max: " << max << std::endl; -
std::ifstream infile("data.txt"); -
std::ofstream outfile("summary.txt"); -
std::size_t num_data = 1; -
std::vector<int> data; #distractor -
while (infile >> value) { -
} -
}
You are given a file called "employee_data.txt" and you want to store
the information from that file into a vector of data. The file contains
information about an employee's first and last name, age, phone number,
and email. Write the definition of an employee before you write your
main function. Open and check the file before working with the data.
Put the necessary blocks of code in the correct order.
-
data.push_back(e); -
employee e(fname, lname, age, phone, email); -
employee(std::string f, std::string l, int a, int p, std::string e) { fname = f; lname = l; age = a; phone = p; email = e; } -
if (infile.good() == false) { std::cout << "Unable to open the file." << std::endl; std::exit(1); } -
int age, phone; -
int main() { -
std::ifstream infile("employee_data.txt"); -
std::string fname, lname, email; -
std::vector<employee> data; -
struct employee { std::string fname; std::string lname; int age; int phone; std::string email; -
while (infile >> fname >> lname >> age >> phone >> email) { -
while (infile) { -
} -
} -
};
You are given a file but it appears that someone's capslock key was stuck
because everything is in uppercase. Write a program that takes the input from
the file "UPPER.txt" and converts all the words to lowercase and prints
out the modified message to a file called "lower.txt". Write the definition
of the function to_lower first. Separate the words with spaces.
Put the necessary blocks of code in the correct order.
-
} return upper; } -
if (infile.good() == false || outfile.good() == false) { std::cout << "Unable to open a file." << std::endl; std::exit(1); } -
int main() { -
outfile << upper << " "; -
std::ifstream infile("UPPER.txt"); -
std::ofstream outfile("lower.txt"); -
std::string upper = upper_to_lower(word); -
std::string upper_to_lower(std::string upper) { for (std::size_t i = 0; i < upper.length(); ++i) { -
std::string word; -
std::tolower(upper[i]); -
upper[i] = static_cast<char>(std::tolower(static_cast<unsigned char>(upper[i]))); -
upper_to_lower(word); -
while (infile >> word) { -
} -
}
Nobody ever put a limit on how many files we can work with. Does this mean we can open two or more files at once? Yes we can! Write a program that combines two files "odds.txt" and "evens.txt" into one output file "numbers.txt". You should combine them in a way such that "numbers.txt" contains the first odd number then the first even number then the second odd number and so on. You are guaranteed that there are equal amounts of odd and even numbers. Put the necessary blocks of code in the correct order.
-
if (!odds.good() || !evens.good() || !outfile.good()) { std::cout << "Unable to open a file." << std::endl; std::exit(1); } -
int main() { -
int odd, even; -
outfile << odd << " " << even << " "; -
std::ifstream evens("evens.txt"); -
std::ifstream odds("odds.txt"); -
std::ofstream outfile("numbers.txt"); -
while (odds >> odd && evens >> even) { -
} -
}
Convert a vector of names to a set of unique names. Assume the necessary standard-library headers are included.
-
for (const auto& name : data) { -
names.insert(name); -
return names; -
std::set<std::string> names; -
std::set<std::string> vector_to_set(const std::vector<std::string>& data) { -
} -
}
Create a rectangular table with rows and columns supplied at run time. Initialize all entries to zero. Assume the necessary standard-library headers are included.
-
return std::vector<std::vector<int>>(rows, std::vector<int>(columns, 0)); -
std::vector<std::vector<int>> make_table(std::size_t rows, std::size_t columns) { -
}
Count repeated city names with a map. Return the frequency of each name. Assume the necessary standard-library headers are included.
-
++counts[name]; -
for (const auto& name : names) { -
return counts; -
std::map<std::string, std::size_t> count_cities(const std::vector<std::string>& names) { -
std::map<std::string, std::size_t> counts; -
} -
}