Hey all, this probably is a simple fix and i just don't see it.


I am having trouble printing out a code. I am supposed to print a list of User-names passwords and Pin numbers in visual C++ console, but when i run the program my pin numbers seem to be set automatically to a large random number.

here's some of my code:
=================================================

const int MAXUSERS = 100;

// struct type
struct Login	
{
string
username,
password;
int PIN;
};

typedef Login Users [MAXUSERS];

void ReadAllUsers (int numusers, Users ID);
void PrintAllUsers (int numusers, const Users ID);
void PrintOneUser (Login inID);

int main ()
{
Users ID;
int numusers;

//read's file to pull into struct
ReadAllUsers (numusers, ID);

//print list of users
PrintAllUsers (numusers, ID);


}


void ReadAllUsers (int numusers, Users ID)


{
int lcv;	 // for loop counter
int pins = 0;
ifstream infile;	 // input data file

// open the data file; abort run if can't open
infile.open ( "file.txt" );
if (!infile)
{
infile.clear();
cout << "Error, file could not be opened."
<< endl << endl;
exit (EXIT_FAILURE);
}


infile >> numusers;
infile.ignore (200, '\n');


for (lcv = 0; lcv < numusers; lcv++)
{
getline (infile, ID[lcv].username);
getline (infile, ID[lcv].password);
infile >> ID[lcv].PIN; 
infile.ignore (200, '\n');

}	// for loop
infile.clear ( );
infile.close ( );

}

=========================================================

I am pulling information for a text file that looks something like this (minus the <<---***** i added that to explain what each item is for):

5 <<-- first input saying how many users
gen34 <<--- username
mixitup <<-- password
8844 <<--- PIN #
marks67
genesis
9494
.....ect.

and repeats a couple more times.

When I debug the program, all my int (lvc, numusers, PIN) are being set to -858993460. I can't seem to figure out why. It should be pulling 5 from the first value, then username and password are being pulled in the struct correctly, then i see PIN set to some ridiculous number.

Thanks.

Pass in numusers to ReadAllUsers by reference. Change lines 14 and 33 and add an & between int and numusers.

The way you have it now, numusers is uninitialized in main, so you send garbage into ReadAllUsers, which only changes a copy of it to a value, but the one in main() is left unchanged.

It never hurts to initialize a variable (to zero or otherwise) when it's declared and is necessary if you are going to use it for a counter/index variable etc.

That may not be the only issue, but it's a start.

Thank you. I knew it was a small mistake.

I have stubbled across another problem in my code. I am getting multiple errors when i try and do a linear search. or write an if statement to compare a user input to the username in the text file.

int linearsearch (string InUserName,const Users ID, int numusers)
{
	int notfound = -1;
	int count;
	for (count = 0; count < numusers; count++)
	{
		if (InUserName == ID [count])
			 return count;
	}
	return notfound;
}

I'm getting an error saying:
error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const _Elem *' from 'const Login'

Because what I'm doing is trying to make a linear search find the same entry. So when a person inputs a Username, Password and PIN # they all match up in the same element in the array. So it's not matching random part's of the array.

Think about what do you do in line 7.

Well in line 7 i am taking the cin from another function and pulling it in and comparing it to the first string in the struct.

I am just confused why it's giving me an error.

Tou need to compare apples with apples, so compare InUserName to ID[count].username

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.