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)
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 :

C++ need help with vector. I have a text file and input it's content into a...
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...
I am reading a text file and trying to store the information into an array so I can use this in different parts of my program. So a dynamic array. So far I have been able to retrieve the information from the text file and organize it by category (User name,User ID, User password) I am having troubles allocating an array and storing the information correctly. Since I have different data types (int, string) do I need to create a...
*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,...
*Suppose I have a C++ Text File That Looks Like This:* Text File: 10 20 30 40 11 40 50 60 12 50 60 70 I want to be able to read the 3rd column which have the numbers "30 , 50, 60" and find their average. How do I read data from a text file in columns instead of rows?
Hi I how do i use a vector to search a array for a string that user has enter into the console. here is some of the instructions for the programing I am trying to work on The purpose of this program is to write a class whose job is to read up to 10,000 names into a string array. The class sorts the names and then the user can ask the class to find full names containing a name...
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 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...
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...
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...
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...