Why wont this work!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 46
Reputation: cppnewb is an unknown quantity at this point 
Solved Threads: 0
cppnewb's Avatar
cppnewb cppnewb is offline Offline
Light Poster

Why wont this work!!!

 
0
  #1
Jan 31st, 2009
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:
  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
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 393
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Why wont this work!!!

 
1
  #2
Jan 31st, 2009
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
  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
  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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: cppnewb is an unknown quantity at this point 
Solved Threads: 0
cppnewb's Avatar
cppnewb cppnewb is offline Offline
Light Poster

Re: Why wont this work!!!

 
0
  #3
Jan 31st, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 393
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Why wont this work!!!

 
0
  #4
Jan 31st, 2009
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
  1. if (disPlayData2.good())
  2. { std::cout<<"My file opened"<<std::endl;}
  3. else
  4. { std::cout<<"File not found/opened."<<std::endl;}
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: cppnewb is an unknown quantity at this point 
Solved Threads: 0
cppnewb's Avatar
cppnewb cppnewb is offline Offline
Light Poster

Re: Why wont this work!!!

 
0
  #5
Jan 31st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 393
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Why wont this work!!!

 
0
  #6
Feb 1st, 2009
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
  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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: cppnewb is an unknown quantity at this point 
Solved Threads: 0
cppnewb's Avatar
cppnewb cppnewb is offline Offline
Light Poster

Re: Why wont this work!!!

 
0
  #7
Feb 1st, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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