Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: I need to read in data from a text file and then store it in a structure. T

ID: 3859306 • Letter: C

Question

C++: I need to read in data from a text file and then store it in a structure. Then a vector is made from that structure in main() and passed to a fileInput function by reference.

A count of each line is then made and displayed. I want to add an option for the user to display all of the data from the text file directly into the terminal window of the program. Here is my structure:

struct component // Structure to Hold Resistor Values

{

int id; // Integer Datatype 'id' Declaration

string type; // String Datatype 'type' Declaration

string vendor; // String Datatype 'vendor' Declaration

int value; // Integer Datatype 'value' Declaration

};

** Here is the code after a successful read, to display just the count: **

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

int count = -1; // Counts For Each Value, Allowing for Label Ignorance

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

inFile >> ID >> TYPE >> VENDOR >> VALUE; // What Is This Used For?

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

cout << "Do You Want To Display The Data? [y or n] "; // Not Sure How To Handle This Part

cin >> input;

cout << endl;

if (input == 'n' || input == 'N') // Not Sure How To Handle This Part

return;

else // Not Sure How To Handle This Part

{

** This Is Where I Want To Display The Data From The Text File If The User Chooses To Display It **

** The First Line Of The File Is Just Column Labels, Display The First Line As A Character String **

** Then Display Every Consecutive Line After That Following The Datatype Convention Of The Structure: int, string, string, int **

}

return; // Then Return To Main

Explanation / Answer

#include<iostream>
#include<fstream>
#include<vector>
#include<string>

using namespace std;
struct component // Structure to Hold Resistor Values
{
   int id;
   string type;
   string vendor;
   int value;
};


int main(){

    ifstream fin;
    int id;
    string type;
    string vendor;
    int value;
    vector<component> list;
    component item;
    char input;
    string line;
   

    fin.open("Input.txt");   //Assume the data is present in Input.txt
    getline(fin, line); // Read In The First Line, Ignoring Labels
   
    while (fin >> id >> type >> vendor >> value){// Used for reading values separated by spaces

         item.id = id;
         item.type = type;
         item.vendor = vendor;
         item.value = value;
         list.push_back(item);        
    }
    fin.close();
    cout << "...And The Total Number Of Resistors Is: " << list.size() << endl << endl;
    cout << "Do You Want To Display The Data? [y or n] ";
    cin >> input;
    cout << endl;
    if (input == 'n' || input == 'N')
       return 0;
    else {
       cout << line;   //The header line in the file.
       for (int i = 0; i<list.size(); i++){
            cout << list[i].id << " " << list[i].type << " " << list[i].vendor << " " << list[i].value << endl;
       }
    }
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote