Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am writing a C++ program that prompts a user to enter a file name of a text file to open and read in data.

I am writing a C++ program that prompts a user to enter a file name of a text file to open and read in data. I had it working just fine, as I had a little counter and it confirmed how many lines there were in the table of the file. What I am trying to do now is add a little sophistication. I would like to add an option after it says how many lines there are to show the table exactly as it looks in the textfile with each column's label, followed by every line of data; I would like to add user validation when the user enters the file name. If the user enters the wrong file name, I want it to let them know, and then start the whole process of entering a file name over. I tried to use a do-while loop, but I always have a hard time getting those exactly right. Please Help Me! The text file table that is read in looks like this:

ID Type Vendor Value

1 R1000 Panasonic 984

... with 2,999 more lines...

** This is what my inputfile function looks like so far: **

void fileinput(vector& v) // fileinput Function Definition

{

ifstream inFile; // Input File Declaration Statement. Object is inFile.

ofstream outFile; // Output File Declaration Statement. Oject is outFile.

char response; // Character Datatype 'response' Declaration

string name, fileName; // String Datatypes 'name' & 'fileName' Declaration

string str2 = ".txt"; // Needed to Append the .txt

int ID; // Input for Integer, ID

string TYPE; // Input for String, Type

string VENDOR; // Input for String, Vendor

int VALUE; // Input for String, Value

do // **THIS IS THE PART THAT NEEDS WORK**

{

cout << "Please Enter A File Name, Excluding The File Extension: "; // Prompt User To Enter A File Name

cin >> name; // User Enters A File Name

cout << endl;

}

while (!inFile.fail()); **IT'S DEFINITELY MIXED UP**

{

fileName = name + str2; // Appending the .txt

inFile.open(fileName.c_str()); // Open The File With The External Name file1.txt

if (inFile.fail()) // Check For A Successful Open **I KNOW THIS IF-STATEMENT IS OUT OF PLACE**

{

cout << "The File Was Not Sucessfully Opened. " << endl << endl;

cout << "Please Check That The File Currently Exists! " << endl << endl;

cout << "Let's Try This Again... " << endl << endl; **THEN I WANT THE WHOLE PROCESS TO START OVER OF ENTERING A FILE NAME**

}

}

cout << "The File Has Been Sucessfully Opened For Reading... " << endl << endl;

string line; // **** Read In The Data

int count = -1; // Counts For Each Value, Allowing for Label Ignorance **ALLOWING FOR EACH COLUMN'S LABEL NOT TO BE COUNTED**

getline(inFile, line); // Read In The First Line, Ignoring Labels

inFile >> ID >> TYPE >> VENDOR >> VALUE; // **I DO WANT TO INCLUDE THE COLUMN LABELS IF THE FILE HAS BEEN CHOSEN TO DISPLAY**

while (!inFile.eof())

{

getline(inFile, line);

count++;

}

inFile.close();

cout << "...And The Total Number Of Resistors Is: " << count << endl << endl; // Display the Number of Resistors

**HERE IS WHERE I WANT TO ASK THE USER IF THEY WANT TO DISPLAY THE TABLE**

**SOMETHING LIKE...**

cout<<"Would You Like To Display This Data? "<

**IF THEY SAY YES, I WANT THE ENTIRE TABLE FROM THE TEXT FILE TO DISPLAY EXACTLY HOW IT LOOKS IN THE TEXT FILE, WITH COLUMN LABELS FOLLOWED BY EVERY LINE OF DATA, ALL 3,000 LINES! AND IF THEY SAY NO, EXIT THE PROGRAM**

return;

} // End Of File Input

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

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions