my problem is about checking the subject related to the user

a text file structure as below:

username_1 password subject_1 subject_2 subject_ n
username_2 password subject_1 subject_2 subject_ n
username_3 password subject_1 subject_2 subject_ n
.
.
.
username_m password subject_1 subject_2 subject_ n

in my problem the user will enter his username and the subject and i want to check if the subjct that entered is related to that user.

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{

    ifstream instaff ("loginstaff.in"); //creating the stream
    if (!instaff)
    {
        cout << "no file" << endl;
        exit (0);
    }
    char user[10],username[10],subject[30], sub[30] , pass[30];
    cout<<"enter username: ";
    cin.getline(username,10,'\n');//getting the username from the keyboard
    cout<<"enter subject: ";
    cin.getline(subject,30,'\n');//getting the subject from the keyboard
    int i=0;

    while(!instaff.eof()) // while not the end of the file 
    {
        instaff.getline(user,10,' '); // get the username from the text file
        instaff.getline(pass,30,' '); // get the password from the text file 
        if(strcmp(user,username)==0) // compare username and the one from the file
        {
            char let;
            while (instaff.get(let)) // take character by character
            {
                sub[i]=let; // assign the letter to the subject array
                i++;// increment the array index for next letter
                if (let==' ') // if the letter is space which means the end of the subject 
                {
                    sub[i]='\0';  // close the subject string
                    i=0; // return the array index to the first position
                    if (strcmp(sub,subject)==0) // compare the input subject with that in the file
                    {
                        cout << "valid"; // if they are same output "valid"
                        return 0; // end the program
                    }
                }
                if (let=='\n') // if the letter is '\n' which means the end of the subject list of that user
                {
                    cout << "not valid" <<endl; // no subjects match 
                    return 0;
                }
           }
           }
        }
        return 0;
}

but i am not getting the right output

>> if(strcmp(user,username)==0)

That is a case-sensitive comparison, so "John" is not the same as "john" due to capitalization. Depending on your compiler, use stricmp(), comparenocase(), or convert both strings to either upper or lower case before calling strcmp().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.