c++ searching and sorting in student list - c++

What I want to do is to make a searching engine for searching student`s grade in student list with student number and student name. If multiple students match the requested name, the system will ask the user to enter the student id.
I have done the read txt file, however, I stopped at changing txt to array for storing and searching. I don't know how to store those data into array.
I got 20 students and here's two examples of student's grading in each subject:
SS6709 Peter VT001 C VT002 E VT003 D VT004 D VT009 A+ VY018 A++ VT024 B
SS9830 Amy VT001 D VT002 C VT003 C VT004 D VT009 D VT018 D VT023 B

think and define a class Item that will represent a single item that will hold the data about a single student (code, name, ...)
create an empty std::vector of those structures that will hold your "items"
open the file for reading
read the file line-by-line (use std::getline to get WHOLE lines, not just filestream.getline or file >> var operator)
(for each line that you read)
read the line as std::string, just as std::getline wants
create a Item for holding next group of data
analyze the line and cut/copy the student's code into myItem.code
analyze the line and cut/copy the student's name into myItem.name
analyze the line and cut/copy the XXXXXXXXXXXXXX into myItem.whateverelse
append the myItem that you have just read to the vector that you prepared earlier
close the file after reading all lines
Now you have a 'vector' that holds all the data, nicely stored as "items" that each have a code, name, and other data. You can now loop/search over that vector to find if any name or code matches.

it looks like you need to create your own data structure to store the values. You can define your own struct
struct Student{
string name
string iD
....
};
and use a array of type in this case Student
Student data_base[100];
this is a very basic approach but it should get you going...

Related

Storing data from file into array then reading specific line and column from file in c++

I have a text file (students.txt) like this:
StudentID StudentName StudentName2 StudentMarks
100 Justin Smith 94
101 David Mason 79
102 James Stewart 88
103 Carl Brown 79
(The list goes on like this until ID 300)
I will write a program such that when a StudentID is entered, it searches its values in StudentID and outputs its corresponding StudentName and StudentName2 and StudentMarks.
My approach:
I used fstream to open the file.
I want to create an array [4] for each column.
Issues:
I cannot 'separate' the columns into arrays. (for storing & fetching)
I cannot make it to ignore the 1st line of the text(i.e the titles) which would not be a problem but since I declared the 4th column (StudentMarks) as integer, but the title is a string so I think it is better to make it to ignore the 1st line (?)
I cannot input a 3rd name for a student (If I input StudentName3 for some, the others' StudentMarks will appear in the place of their marks.)
[For now, I tried: concatenating StudentName2 and the 3rd name by using '_' e.g Brown_Smith]
I couldve created a simple array and make it to read and output every line one by one, but in some scenario, I do not want to output the StudentID, that's why I want to separate it into columns.
Im a c++ beginner.
Do you think that my approach to create an array for the 4 columns is appropriate?
Comments appreciated a lot ~

Comparing terms entered by the user to lists in a txt file

I'm want the user to enter in their name, sex, age, medication, and condition. Then look through the text to see if their condition matches any of the other people in the text document, then see if their age, sex, or medication is the same. If it is out put the possible side effect that's also in the text document.
Having trouble getting started since its been so long since I did anything like this. I just need to know the basics of how to read and compare a text document.
Txt doc is laid out like this:
Name Med Sex Age Cond Effect
Bill DepMed M 33 Depression StomachAche
Tom ADDMed M 24 ADD HeadAche
I don't know how "basic" you need, but to read and write files you need to include the header file "fstream". You can read and write files in a variety of ways. One way is to open a file and instead of using cin for input and cout for output you would use the name of the file stream that you opened the file in. Example:
#include <fstream>
int main() {
string input;
fstream dataFile; //names stream 'dataFile' sort of like a variable.
dataFile.open("data.txt", ios::in | ios::out); //opens data.txt for reading (ios::in) and writing (ios::out)
dataFile >> input; //stores data to input exactly like 'cin' would from the screen, but in this case the input is coming from 'dataFile'
getline(dataFile, input, '\n'); //stores data to input exactly like 'cin.getline()' would
dataFile << "String to be added in file" << endl; //prints to file exactly like 'cout' prints to screen
dataFile.close() //closes file, be sure to do this or else you risk memory leak issues
}
Specifically for your question:
Ask the user for one of the columns (you don't need to ask for all of them. Name, condition, or symptom would work best).
Open data file
Use getline(inFile, junk, '\n'); to skip first line (you don't want to search the column titles). junk is a string variable and inFile is your '.txt' file.
Read the next line in the file by using getline() again.
For every line, search the string that was read from the file, searchString, for the string that the user inputted, userInput, using found = searchString.find(userInput, 0). You would have to declare size_t found before the loop.
For every line, check if userInput was found in searchString using if(found != std::string::npos)
If found print `searchString to the screen using 'cout'
Repeat step 4-7 until the end of file is reached
Close the file

C++ - Class method changes member variable, but not in main

I have a Student class which stores a student's name and registration number. One of the private members of the Student class is a map which stores a student's module code (string), along with the mark for the module (float).
The class has an 'addMark' function which takes a module code and the mark for that module. If the module already exists in the map, the mark is overwritten. Otherwise, it is inserted into the map.
void Student::addMark(const string &module, float mark)
{
map<string, float>::iterator iter = marks.find(module);
if (iter != marks.end()){marks[module] = mark;}
else{marks.insert({module, mark});}
}
I have checked, and this seems to work inside that function.
In the main function, I have a vector markLine which stores each token of a line in a text file containing the marks. The first token (markLine.at(0)) is the registration number, the second token is the module code, and the third token is the mark. So the vector might look like the following for a line:
markLine = {10105, "CE101", 78.5};
Anyway, after reading a line and storing each token, the module mark needs to be stored for the correct student, so I use a for loop to go through all the students and see if their registration number matches. If it does, the mark is added:
for (Student st: studVect)
{
if (st.getRegNo() == markLine.at(0))
{
st.addMark(markLine.at(1), markLine.at(2));
}
}
But when I check afterwards to see if the marks have changed for a student, they have not.
I've googled around a lot and found some questions quite similar to mine, and I have a feeling that this is something to do with references, and a copy of marks being created. However, I'm not really sure where exactly I need to be using a reference. If anyone could point me in the right direction, I would be grateful.
You're working with a copy of the Student record.
Change
for (Student st: studVect)
to
for (Student &st: studVect)

finding the max value in a list and adding 1

I have created some code to read data from a text file that has a list of sequential number. The user is prompted to see if they are a teacher or student(I have only written for a student) When they enter student the numbers in the text file are read to the list "log" and the maximum value is found. The code should then add 1 to this value. The user is them asked their name and this along with the new value is written to the original text file. so that the is a list of students with unique id number. The code works fine and add new students with sequential id number up to the number 10 but then stops and will only add the number 10. I suspect the int(max(list)+1 is the problem.
log=[];
Input=input("student or teacher?")
if Input =="student":
file=open("numbers.txt","r")
for line in file:
split=line.split(":")
log.append(split[0]);
file.close()
print(log)
else:
print()
number=int(max(log))+1
name=input("what is your name?")
file=open("numbers.txt","a")
file.write(number+":"+name+"\n")
file.close()

Reading information from a text file and properly storing it in arrays, int [duplicate]

This question already has an answer here:
C++ Read File with multiple column
(1 answer)
Closed 8 years ago.
I would like to have a piece of C++ code that reads from a text file and saves the information in some variables, arrays...
For example, let's say we have a file Numbers.txt, which contains 4 integers. An example code that I found so far ( and does not do the trick for me ) looks like this :
string weight, height, width, depth;
ifstream MyFile("Numbers.txt");
MyFile >> weight;
MyFile >> height;
MyFile >> width;
MyFile >> depth;
But it only reads single pieces of data, separated by an empty space.
I would like code to be able to read several pieces of data that can be in a same row. To clarify, that's an example of how a text file format would look like...
FileName
ProjectName
Names
IDs
and en example of actual content of a text file ...
// SomeData.txt
FileName: SomeData.txt
ProjectName: Second Attempt // should be able to read with spaces too, and save in a string as "Second Attempt"
Names: John, Frank, Mike // I guess it could be without commas too
IDs: 42, 505, 1591, 4358, 12, 743
So the main problem is for the program to skip to next lines while reading, and ignoring "keywords" like "Names:", "IDs"... and at the same time be ready to read as many entries as there are in a line, even if there is no information in advance of how many there would be.
The information just read would best be saved as such:
FileName -> string
ProjectName -> string
Names -> array of strings
IDs -> array of integers
How would an example of such code look like ?
My answer is incomplete but here's an overview of how I would go about this:
When you are using an ifstream you can grab the whole line at once using a function called getline and store it in a char array.
You could then use a function such as strtok to split that line up, as I notice that the data comes after a : character.
At this point you could have a switch statement depending on the string before the :, and process your data appropriately. For example if you have IDs then use atoi to convert them to ints and store them in arrays etc.