Question

C++ need help with vector. I have a text file and input it's content into a...

C++ need help with vector. I have a text file and input it's content into a struct that holds an array for each entry as a string type.

for example:

struct container{

string ID[10000];

string anotherID[10000];

}

in the text file there is a huge list that looks something like this:
1 abc

3 xyz

1 abc

1 xyz

How can I use a vector to sort these so I can get an output like

ID: 1 and anotherID: abc found 2 time(s)

ID: 1 and anotherID: xyz found 1 time(s)

ID: 3 and anotherID: xyz found 1 time(s)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution for the above question is given below with the screenshot of output.
---------------------------------------------------------------------------------------------------------------

I have kept the logic simple and output as per the question.

If there is anything else do let me know in comments

---------------------------------------------------------------------------------------------------------------

id.txt

1 abc
3 xyz
1 abc
1 xyz

--------------- CODE TO COPY -----------------------------------------------------------------------

main.cpp

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

struct container{
  
  string ID[10000];
  string anotherID[10000];
  int times[10000];

};

void sort( struct container *con, int count){
  
   int i, j;
   for (i = 0; i < count-1; i++)      
 
       // Last i elements are already in place   
       for (j = 0; j < count-i-1; j++) {
         
            if ( con->ID[j] > con->ID[j+1] )
            {
              string id;
              string anotherID;
              int times;
              
              id = con->ID[j];
              anotherID = con->anotherID[j];
              times = con->times[j];
              
              con->ID[j] = con->ID[j + 1] ;
              con->anotherID[j] = con->anotherID[j + 1];
              con->times[j] = con->times[j + 1];
              
              con->ID[j + 1] = id;
              con->anotherID[j + 1] = anotherID;
              con->times[j + 1] = times;
            }
       }
}

int main() {
  
  container con;
  int count = 0;
  
  ifstream in;
  in.open("id.txt");
  
  if ( in.fail() ) {
    cerr << "Error : cannot open file." << endl;
    return 1;
  }
  
  // reading data from file 
  
  while ( in.eof() == false ){
    
    string id;
    string anotherID;
    int i = 0, found = 0;
    
    in >> id >> anotherID;
    
    // if id alreaky in array increase times
    for ( i = 0; i < count; i++ )
    {
      if ( con.ID[i] == id )
        if ( con.anotherID[i] == anotherID ){
          con.times[i] = con.times[i] + 1;
          found = 1;
        }
    }
    
    // if ad not found add it to list
    if ( i >= count ){
      con.ID[count] = id;
      con.anotherID[count] = anotherID;
      con.times[count] = 1;
    }
    
    if ( ! found )
      count++;
  }
  
  sort( &con, count);
  
  for ( int i = 0; i < count; i++ )
    cout  << "ID: " << con.ID[i] 
          << " and anotherID: "
          << con.anotherID[i] 
          << " found "
          << con.times[i] 
          << " time(s)" << endl;
  
  
  in.close();
  return 0;
}


---------------------------------------------------------------------------------------------------------------

Output :

gcc version 4.6.3 ID: 1 and anotherID: abc found 2 time(s) ID: 1 and anotherID: xyz found 1 time(s) ID: 3 and anotherID: xyz

Add a comment
Know the answer?
Add Answer to:
C++ need help with vector. I have a text file and input it's content into a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi! 1. I need some help with sorting string in a text file. My goal is...

    Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...

  • *HOW TO SEARCH FOR THE STUDENT ID IN C++ USING TEXT FILES?* Text File Content: ID...

    *HOW TO SEARCH FOR THE STUDENT ID IN C++ USING TEXT FILES?* Text File Content: ID G1 G2 G3 200 20 20 20 30 30 30 30 400 40 40 40 25 25 25 25 78 78 78 78 666 66 6 66 777 77 100 77 420 77 88 99 1711 10 10 10 404 40 50 60 505 100 100 100 void studentStats() {    ifstream inputFile;    inputFile.open("outFile.txt");    string studentData;    string studentID;    string ID,...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • Hi, i am working with MATLAB. I have a text file from which I have to...

    Hi, i am working with MATLAB. I have a text file from which I have to read in my data. So far i have this in my code. fid = fopen('ecgnormaloff.txt', 'r'); data = textscan(fid,'%f') According to all the websites, this should be enough to read in my points, however, I get this as my output data = 1×1 cell array {0×1 double} >> And when I try to access my data, there is nothing. I do not want to...

  • Hi, I need help with my comp sci assignment. The parameters are listed below, but I...

    Hi, I need help with my comp sci assignment. The parameters are listed below, but I am having trouble generating the number of occurrences of each word. Please use a standard library. Read in the clean text you generated in part 2 (start a new cpp file). Create a list of all the unique words found in the entire text file (use cleanedTextTest.txt for testing). Your list should be in the form of an array of structs, where each struct...

  • c# csci312: character counter - vector design a program that reads in an ascii text file...

    c# csci312: character counter - vector design a program that reads in an ascii text file (provided) ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provide... C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provided) and creates an output file that contains each unique ASCII...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • In C. I want to read in the data of a text file into an array...

    In C. I want to read in the data of a text file into an array of dynamically allocated structs. The file is read as a command-line argument The text file format is: <number of classes in this file> <department name of class 1>:<number of class 1>:<location of class 1> <department name of class 2>:<number of class 2>:<location of class 2> ... Assume that no piece of information in the file contains a colon: the colons are only used as...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT