Assignment description: create a C++ program that reads a user-defined number of
ID: 3722449 • Letter: A
Question
Assignment description: create a C++ program that reads a user-defined number of names names from a file. the program should check for file opening errors. Show the names on the screen. Use a counted loop to read and show the names.
Sample output:
Which file would you like to use? names.txt
How many names would you like to read (up to 20)? 7
Now we are going to read 7 names from the file names.txt.
Name 1: Bill
Name 2: Bart
Name 3: Jim
Name 4: Griffin
Name 5: Marty
Name 6: Geri
Name 7: Terri
Now we are done reading from the file and we need to close it.
Press any key to continue . . .
Which file would you like to use? numbers.txt
How many names would you like to read (up to 20)? 7
Error opening file.
Press any key to continue . . .
Grading:
( 5 pts): student name in file header
( 5 pts): reads from the file and closes file at end
( 5 pts): program reports file open errors
( 10 pts): correct number of names are read and shown
( 5 pts): user can specify the file name to use
( pts):
This is what I have so far but I keep getting the bottom errors! What am I doing wrong?
Homework 2 Question 3-Microsoft Visual Studio File Edit View Project Build ebugTeam Tools Test Analyze Window Help Quick Launch (Ctrl+ Q) Autumn Montgomery Local Windows Debugger | · Source.cpp Homework 2 Question 3 (Global Scope) 1 2 3using namespace std; #include " matches these operands no operator "Explanation / Answer
for that errors you have to include header file i.e., #include<string> and use if( in ) at line number 13(you need to check cpp version and it was valid in C++03, but is not valid in C++11).
#include <iostream>
#include<string>
#include <fstream>
using namespace std;
int main()
{
int n; // to hold no of names that user wants program to read from file
char fileName[100]; //to hold file name
string name; //to hold name read from file
ifstream in; //in variable to read from file
cout << "Which file would you like to use? "; //asking user file name
cin >> fileName; //reading into fileName
in.open(fileName); //opening file
if (in) //checking whther was opened or not
{
cout << "How many names would you like to read (up to 20)? "; //asking user how many names he wants program to read
cin >> n;
cout << "Now we are going to read" << n << " names from them " << fileName; //displaying header to user frm which file we are reading
for (int i = 1; i <= n; i++) //reading files
{
in >> name;
cout << " Name " << i << ":" << name; //prnting names
}
cout << " Now we are done reading from the file and we need to close it. "; //printing message to user that reading names from file is completed
in.close(); //closing file
cout << "press any key to continue";
}
else //if file is not opened then displaying error message
{
cout << "Error opening file. Press any key to continue . . .";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.