Thread: Using files: Wacked Question

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    Using files: Wacked Question

    Code:
    #include <abbrev.h>
    
    main()
    {
    	char user[2], vuser[2], pass, vpass;
    
    	ifstream fin("C:\\users.abc");
    	fin >> user;
    	fin >> pass;
    
       	fin.close();
    
    	cout<<"Please enter your name:\n";
    	cin>>vuser;
    	cout<<"\nPlease enter yourt password:\n";
    	cin>>vpass;
    
    	if(pass == vpass)
    	{
    		if(user == vuser)
    		{
    			cout<<"Welcome";
    		}
    		else
    		{
    			cout<<"Wrong name";
    		}
    	}
    	else if(user == vuser && pass != vpass)
    	{
    		cout<<"Wrong Password";
    	}
    	else if(user != vuser && pass != vpass)
    	{
    		cout<<"Wrong name and password";
    	}
    	else
    	{
    		cout<<"Something went Wrong!!";
    	}
    	cin>>vpass;
    
    	return 0;
    }
    this code runs with no compile errors, but it always turns out as "Something went Wrong!!". I checked and "user" has the first 4 letters of the username and cuts off the fifth letter and replaces it with the password. WTF???

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Whoa whoa. What is this abbrev.h and C-style main?

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    69

    Correction

    <code>
    #include <iostream.h> // needed for cout and cin
    #include <fstream.h> // for File I/O
    #include <windows.h> // for Sleep and exit

    int i = 0;
    main()
    {

    char user[10], vuser[10], pass[10], vpass[10];

    ifstream fin;
    fin.open("C:\\users.abc");
    fin >> user;
    fin >> pass;
    fin.close();

    cout<<"Please enter your name:\n";
    cin>>vuser;
    for(i=0;i!=10;i++) //it will loop 9 times
    {
    if(user[i] == vuser[i]) //instead of saying user == vuser which doesn't work
    { //scans through each character in both arrays making sure they're correct
    }
    else
    {
    cout<<"Wrong User Name"<<endl; //if it isn't correct it'll exit
    Sleep(500); //pauses the program for 500 milliseconds which is half a second
    exit(0); //exits the program

    }
    }
    cout<<"\nPlease enter your password:\n"; //same as above just for password
    cin>>vpass;

    for(i=0;i!=10;i++)
    {
    if(vpass[i] == pass[i])
    {
    }
    else
    {
    cout<<"Wrong Password"<<endl;
    Sleep(500);
    exit(0);

    }
    }

    cout<<"Welcome "<<user<<endl;
    Sleep(1500);

    return 0;
    }
    </code>

    Make sure you have the right includes and you can never of what i know of compare correctly character strings.
    Hope This was helpful. The Only Problem is that the File that is compiled is pretty big it's possible to make it smaller but i didn't want to muck around that much witht he code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question on reading files in C
    By TaiL in forum C Programming
    Replies: 12
    Last Post: 10-05-2008, 09:48 PM
  2. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  3. input/output files question
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 12:38 PM
  4. Question about ".o" files
    By Jez_Master in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 01:54 AM
  5. question about header files
    By ArseMan in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2001, 02:33 AM