| | |
checking the subject related to the user
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
but i am not getting the right output
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.
C++ Syntax (Toggle Plain Text)
#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
Last edited by Ancient Dragon; May 8th, 2009 at 10:29 am. Reason: add code tags
>> 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().
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().
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Website disappeared on Google since coverted to DIV? (Search Engine Optimization)
- checking if a value exists (MySQL)
Other Threads in the C++ Forum
- Previous Thread: Erasing element from a vector
- Next Thread: can't build wxwidgets in cygwin
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






