checking the subject related to the user

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 1
Reputation: oah1837 is an unknown quantity at this point 
Solved Threads: 0
oah1837 oah1837 is offline Offline
Newbie Poster

checking the subject related to the user

 
0
  #1
May 8th, 2009
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.


  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <fstream>
  5. using namespace std;
  6. int main()
  7. {
  8.  
  9. ifstream instaff ("loginstaff.in"); //creating the stream
  10. if (!instaff)
  11. {
  12. cout << "no file" << endl;
  13. exit (0);
  14. }
  15. char user[10],username[10],subject[30], sub[30] , pass[30];
  16. cout<<"enter username: ";
  17. cin.getline(username,10,'\n');//getting the username from the keyboard
  18. cout<<"enter subject: ";
  19. cin.getline(subject,30,'\n');//getting the subject from the keyboard
  20. int i=0;
  21.  
  22. while(!instaff.eof()) // while not the end of the file
  23. {
  24. instaff.getline(user,10,' '); // get the username from the text file
  25. instaff.getline(pass,30,' '); // get the password from the text file
  26. if(strcmp(user,username)==0) // compare username and the one from the file
  27. {
  28. char let;
  29. while (instaff.get(let)) // take character by character
  30. {
  31. sub[i]=let; // assign the letter to the subject array
  32. i++;// increment the array index for next letter
  33. if (let==' ') // if the letter is space which means the end of the subject
  34. {
  35. sub[i]='\0'; // close the subject string
  36. i=0; // return the array index to the first position
  37. if (strcmp(sub,subject)==0) // compare the input subject with that in the file
  38. {
  39. cout << "valid"; // if they are same output "valid"
  40. return 0; // end the program
  41. }
  42. }
  43. if (let=='\n') // if the letter is '\n' which means the end of the subject list of that user
  44. {
  45. cout << "not valid" <<endl; // no subjects match
  46. return 0;
  47. }
  48. }
  49. }
  50. }
  51. return 0;
  52. }


but i am not getting the right output
Last edited by Ancient Dragon; May 8th, 2009 at 10:29 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: checking the subject related to the user

 
0
  #2
May 8th, 2009
>> 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().
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC