I am trying to use C++ (in Visual Studio 2008) to create a program that I first made using Visual Logic.

The program is supposed to check the users input with an external text file. The external text file just has the username's and passwords...

https://sites.google.com/site/sharearea404/

I tried using arrays (ex: "char u[10]") because that is what I had to use in Visual Logic, but then the first username (I tested it with cout) " "peanut butter" " would read out " "peanut ", so I assume the space in between peanut and butter is screwing up the array? I googled that problem and tried to put add a for loop code that would skip the space using \n. It didn't work.

I moved on to using "strings" because it would ouput peanut butter correctly, but now I can't figure out how to make a for loop that will check through all the names and passwords to see if the user input for a username exists in the external text file.

Thanks for any help and advice you can give me in advance.

This is the mess that I have now...

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    string Username, Password, userx; int counter = 0;
  ifstream unpwfile ("C:\\Users\\Daniel\\Desktop\\usernames.txt");
  if (unpwfile)   // note just test the file object
  {
    while (! unpwfile.eof() )
    {
      getline (unpwfile,Username);
      getline (unpwfile,Password);
      //TEST LINE// cout << "User: " << Username << "\n" << "Pass: " << Password << endl;
    }
    unpwfile.close();
  }
  //TEST LINE// else cout << "\n" << "  Error: 'usernames.txt' could not be located." << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; 


    // Get username and pass from user
    cout << "Please enter your username" << endl;
cin >> userx;
      //TEST LINE// cout << Username << Password << userx;
   {
    cout <<"\n\n\n\n\n\n"<< userx << "\n\n\n";
    for (int i = 0; i < 10; i++)
    {
    if (userx == Username)
            cout << "congratulations you finally got the forloop to work" << endl;
    else
    cout << "Username not found" << endl;
    }
   }


  system ("PAUSE");
    return 0;
}

Recommended Answers

All 3 Replies

You are not saving the file contents, your program just reads the file and tosses away the information.

Turn the program around, first prompt for user name and password, then read the file, making the comparison for each line of the file.

You are not saving the file contents, your program just reads the file and tosses away the information.

Turn the program around, first prompt for user name and password, then read the file, making the comparison for each line of the file.

Thanks! I will give that a try and see how far I can get.

Line #31: the string class does not offer an overloaded ==, it only offers overloaded = assignment, += accumulation (append), and [] array subscript and of course an overloaded () operator as a constructor. But what the string class does have that ye' can use instead of a == is the compare() function:

//instead of
if(userx == Username) 

//try
if(userx.compare(Username) == 0)

you're probably thinking, "why don't they just add an overloaded string class == comparison operator, they have everthing else..." to which my response would be, "I have no clue." They took the time to make a dedicated function for comparison when it would be just as easy to overload the comparison operator.

they should also make the != operator also.

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.