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:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;

#include "logInInterface.h"
#include "displayCopyright.h"
#include "gatherUserData.h"

// Prototype decelarations
int passwordIn();

// Declare global variables
copyrightMessage ds;
int main(int nNumberofArgs, char* pszArgs[])
{
	// Clear the screen
	system("CLS");
	
	passwordIn();
	
	// After logInFunc form logInInterface.h is done, start the next action
	system("CLS");
	ds.displayCopyright();

	// Now ask the user to enter his/her library card number
	string inputNumber;
	cout << "\nPlease enter your library card number: ";
	cin >> inputNumber;
	userData uD;
	uD.displayData(inputNumber);
	
	system("PAUSE");
	return 0;
}

int passwordIn()
{
	system("CLS");
	ds.displayCopyright();
	logIn a;
	string passwordInput;
	cout << "\nPlease enter the password: ";
	cin >> passwordInput;
	a.logInFunc(passwordInput);
	
	if (a.statusInt == 0)
	{
		passwordIn();
	}

	return false;
}

gatherUserData.h

class userData
{
public:

	userData()
	{
		number = "0";
	}

	// Start a function that is able to display all of the user's data
	int displayData(string number)
	{
		userFound = false;
		ifstream displayData2;
		displayData2.open("userInformation.database.txt");

		while (displayData2 >> tempData)
		{
			 << number
			 << "\n";

		if (tempData == number)
		{
		userFound = true;
		// Now get the name
		displayData2 >> name;
		cout << "\n\nName: "
			 << name
			 << "\n";

		// Now say the number
		cout << "Library Card Number: "
		// Now get the first part of expiration date
		displayData2 >> exp1;
		cout << "Expiration date: "
			 << exp1;

	    // Now get the second part
		displayData2 >> exp2;
		cout << " "
			 << exp2;
		
		// Now the third
		displayData2 >> exp3;
		cout << " "
			 << exp3
			 << "\n";

		}
		displayData2.close();
		}

		return false;
	}



private:

	string number;
	string name;
	string exp1;
	string exp2;
	string exp3;
	string phone;
	string email;
	string pin;
	string fine;
	string out;
	string holdType;
	string holds;
	string availibleHolds;
	string tempData;
	bool userFound;



};

Any help GREATLY appreciated

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

while (displayData2 >> tempData)
      {
	<< number <<"\n";

do not compile. Did you mean to pass the number to std::cout, and what is tempData??

You need something like

class userInfoRecord
{
   //stuff
};

std::istream&
operator>>(std::istream&,userInfoRecord&);

and then
tempData should be a userInfoRecord and your test should be if (tempData.getNumber()==number)

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?

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

if (disPlayData2.good()) 
  { std::cout<<"My file opened"<<std::endl;}
else
   { std::cout<<"File not found/opened."<<std::endl;}

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

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

while(displayData.good())
   {
      displayData>>tempData;
      if (tempData == number)
         {
          //stuff
         }
      else
         displayData.ignore(1024,'\n'); 
}

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!

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.