943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 688
  • C++ RSS
Jan 31st, 2009
0

Why wont this work!!!

Expand Post »
Hi. I am making a program, and I have a main section and a class that the main section tefers to in a .h file. The Ifstream part wont work, and i dont know why. Please help.

MAIN:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. #include "logInInterface.h"
  9. #include "displayCopyright.h"
  10. #include "gatherUserData.h"
  11.  
  12. // Prototype decelarations
  13. int passwordIn();
  14.  
  15. // Declare global variables
  16. copyrightMessage ds;
  17. int main(int nNumberofArgs, char* pszArgs[])
  18. {
  19. // Clear the screen
  20. system("CLS");
  21.  
  22. passwordIn();
  23.  
  24. // After logInFunc form logInInterface.h is done, start the next action
  25. system("CLS");
  26. ds.displayCopyright();
  27.  
  28. // Now ask the user to enter his/her library card number
  29. string inputNumber;
  30. cout << "\nPlease enter your library card number: ";
  31. cin >> inputNumber;
  32. userData uD;
  33. uD.displayData(inputNumber);
  34.  
  35. system("PAUSE");
  36. return 0;
  37. }
  38.  
  39. int passwordIn()
  40. {
  41. system("CLS");
  42. ds.displayCopyright();
  43. logIn a;
  44. string passwordInput;
  45. cout << "\nPlease enter the password: ";
  46. cin >> passwordInput;
  47. a.logInFunc(passwordInput);
  48.  
  49. if (a.statusInt == 0)
  50. {
  51. passwordIn();
  52. }
  53.  
  54. return false;
  55. }


gatherUserData.h
C++ Syntax (Toggle Plain Text)
  1. class userData
  2. {
  3. public:
  4.  
  5. userData()
  6. {
  7. number = "0";
  8. }
  9.  
  10. // Start a function that is able to display all of the user's data
  11. int displayData(string number)
  12. {
  13. userFound = false;
  14. ifstream displayData2;
  15. displayData2.open("userInformation.database.txt");
  16.  
  17. while (displayData2 >> tempData)
  18. {
  19. << number
  20. << "\n";
  21.  
  22. if (tempData == number)
  23. {
  24. userFound = true;
  25. // Now get the name
  26. displayData2 >> name;
  27. cout << "\n\nName: "
  28. << name
  29. << "\n";
  30.  
  31. // Now say the number
  32. cout << "Library Card Number: "
  33. // Now get the first part of expiration date
  34. displayData2 >> exp1;
  35. cout << "Expiration date: "
  36. << exp1;
  37.  
  38. // Now get the second part
  39. displayData2 >> exp2;
  40. cout << " "
  41. << exp2;
  42.  
  43. // Now the third
  44. displayData2 >> exp3;
  45. cout << " "
  46. << exp3
  47. << "\n";
  48.  
  49. }
  50. displayData2.close();
  51. }
  52.  
  53. return false;
  54. }
  55.  
  56.  
  57.  
  58. private:
  59.  
  60. string number;
  61. string name;
  62. string exp1;
  63. string exp2;
  64. string exp3;
  65. string phone;
  66. string email;
  67. string pin;
  68. string fine;
  69. string out;
  70. string holdType;
  71. string holds;
  72. string availibleHolds;
  73. string tempData;
  74. bool userFound;
  75.  
  76.  
  77.  
  78. };

Any help GREATLY appreciated
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009
Jan 31st, 2009
1

Re: Why wont this work!!!

Ok the usual rant: I hate system("PAUSE") and CLS. They are non-portable, and I can clear my own screen if that is what I want!

Beyond that you haven't given us much to go on.
You don't test if displayData2 is successful? Does "userInformation.database.txt" exist / get opened, have Windows throw a fit about two dots in the name ?
Use
if (disPlayData2.good()) to find out.

Then the lines
c++ Syntax (Toggle Plain Text)
  1. while (displayData2 >> tempData)
  2. {
  3. << number <<"\n";
do not compile. Did you mean to pass the number to std::cout, and what is tempData??

You need something like
c++ Syntax (Toggle Plain Text)
  1. class userInfoRecord
  2. {
  3. //stuff
  4. };
  5.  
  6. std::istream&
  7. operator>>(std::istream&,userInfoRecord&);
and then
tempData should be a userInfoRecord and your test should be
if (tempData.getNumber()==number)
Last edited by StuXYZ; Jan 31st, 2009 at 5:14 pm.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Jan 31st, 2009
0

Re: Why wont this work!!!

Ok....I tried de-bugging the program before i did this post. I forgot all of the ifstream stuff and put a regular cout >> command. The program didnt even go on to that...

tempData is just the check to make sure the number matches the number on file...

userInformation.database.txt exists, but i dont kno if it opens...

does that help?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009
Jan 31st, 2009
0

Re: Why wont this work!!!

No:

The question what is tempData, was because it is declared string but you are comparing it with an integer.

What you have posted DOES NOT COMPILE, hence we can't debug it. What you have to do is POST code that compiles UNLESS you can't get it to compile and THEN post the error message that is causing the problem.

So does userInformation get opened.... I will repeat ADD
c++ Syntax (Toggle Plain Text)
  1. if (disPlayData2.good())
  2. { std::cout<<"My file opened"<<std::endl;}
  3. else
  4. { std::cout<<"File not found/opened."<<std::endl;}
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Jan 31st, 2009
0

Re: Why wont this work!!!

Ok...It says that the file was accessed, but when it tries to access the information, it cant. Here is my text file.....

2010419735 Name Date1 Date2 Phone e-mail Pin $0.00 3 Hold 0 N/A
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009
Feb 1st, 2009
0

Re: Why wont this work!!!

well we still can't figure it out since I am certain that most of the members of this forum are not psychic.
You have not posted code that actually compiles, so I cannot guess what your real code does.

HOWEVER: I think it is that you don't match the first line/number and then you have only read in the first number not the whole line.
So you need a
c++ Syntax (Toggle Plain Text)
  1. while(displayData.good())
  2. {
  3. displayData>>tempData;
  4. if (tempData == number)
  5. {
  6. //stuff
  7. }
  8. else
  9. displayData.ignore(1024,'\n');
  10. }
Last edited by StuXYZ; Feb 1st, 2009 at 4:55 pm.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Feb 1st, 2009
0

Re: Why wont this work!!!

Hi. I actually figured it out...I got the code to compile and turns out that I was overloading the text file, so, i redesigned my code and it works fine. Thanks anayways!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Stumped on C++ code asterisks
Next Thread in C++ Forum Timeline: compile error?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC