Reading/comparing string

Hi! I am trying to create a simple administration system that takes in info from two files, AdminNum.txt and data.txt

AdminNum.txt, which has the student's administration number as shown:

123456
123457
123458
123459


data.txt, which has the student's administration number, first name, last name and their grade

123456 Nelson Cape A
123457 Lance Varnes A
123458 Darren Aguinaldo C
123459 Noreen Rierson B


I have managed to firstly prompt the user to enter their admin number, and then checks if the admin number matches any on AdminNum.txt. If it matches, their admin number will be stored in a string variable called StoredNum and they may enter a menu where they have the option to view their grades etc.

My question is, how can I retrieve the user's info from data.txt? Is there a way I can sort of compare the StoredNum with the first 6 characters in each line of data.txt and if it matches, the line will be extracted into a temp variable, which then I would be able to output their name and grade. If it helps, the data on each line of AdminNum.txt and data.txt will be for the same user.

Thanks for the help! :)
I would just ignore AdminNum.txt altogether and just do something like this:

load data.txt into some structure or string or whatever you want
then just look at the admin number in there when they input their number

EDIT: ExDeath doesn't like you. :( </random inside joke>
Last edited on
Thanks for the quick response :D

so is there a way I can compare the admin number input by the user with only the first 6 character of the string?

I am using something like this:
1
2
3
4
5
6
7
8
9
    ifstream ifs("data.dat");
    string line;
    string inputnum;
    cout<<"Enter your admin number : ";
    cin>>inputnum;
    while (getline(ifs,line))
    {
      //This is where im unsure
     }
Yes, you can use substr from std::string: http://www.cplusplus.com/reference/string/string/substr/
Thanks! I have another question, now that i can use substr to find the admin number, how can i extract the user's name from the line?

Edit: got things done, thanks for the help :)
Last edited on
Topic archived. No new replies allowed.