Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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?

#include #include 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 != NULL)//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 . . ."; } }student submitted image, transcription available below

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 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 10 100 % Error List Entire Solution -||*6Errors |||warnings- 0Messagesl r|| Build + Intellisense |Search Error Lis Description no operator's > " matches these operands no operator "<<" matches these operands no operator'"-" matches these operands binary'>: no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) binary': no operator found which takes a right-hand operand of type'std:string' (or there is no acceptable conversion) binary': no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion) File Source.cpp Source.cpp Source.cpp source.cpp Code E0349 22, E0349 EO349 Project Homework 2 Question 3 Homework 2 Question 3 Homework 2 Question 3 Homework 2 Question 3 ine 20 21 13 20 Homework 2 Question 3 source.cpp 21 C26T8 Homework 2 Question 3 source.cpp 13 Output Build failed 1 / 3 Homework 2 Question 3 Y master

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+4. How is your organization generally perceived?

Answered: 1 week ago

Question

fscanf retums a special value EOF that stands for...

Answered: 1 week ago