So i have been here for hours trying to figure out why this code won't work properly.
I'm basically trying to make the program accept a certain ID and password.
There are two things wrong with your post: 1) You did not specify what exactly goes wrong. 2) You did not put the source code in [code]int i=5; // code goes here [/code] tags so it's hard to read.
There are a lot more things wrong with your code: 1) Modern C++ has a bool type which can hold true or false, don't use int. 2) Modern C++ has deprecated standard header files that have .h extension. 3) Your program doesn't need conio.h and windows.h. 4) Modern C++ programmers are supposed to use std::string instead of char[] and the string.h helper functions.
Finally your problem could be because of cin >> id; and cin >> name;.
Use getline(cin, yourvar); instead or you can't use spaces in your input.
Please invest time in learning modern C++ programming.
I am actually really green too (been coding seriously for about 8 months) but I f'in love object oriented programming so I rewrote your program using a class. Some of the things you were doing I don't quite understand but try this code out if you like.
I tested it and it seems to work.....For the most part. lol
*oh, and you can add mulitple users with ease if you like. Just simply call Adduser and send the id and name. If you want themto be automatically added to the class you can make a default constructor that reads from a file or just has the users hard-coded into it.
I could have done more error checking but this is your program, not mine. haha
#include <vector>
class User {
public:
struct user {
int id;
string name;
};
void AddUser(int id, string name) {
user u;
u.id = id; // add id
u.name = name; // add name
logs.push_back(u); // insert user into vector
}
user getIdAndName() {
user u;
cout << "Enter Id:";
cin >> id;
cin.ignore(256, '\n'); // flush stream (if non-int is entered stream fails)
cout << "Enter Conducter Name:";
cin >> name; // cannot have whitespaces (use getline if you want spaces)
cin.ignore(256, '\n'); // flush stream just because it's safe, especially in loops
u.id = id; // add id
u.name = name; // add name
return u; // return user
}
bool ValidateUser(user u) {
for (int i = 0; i < logs.size(); i++)
if (logs[i].id == u.id and logs[i].name == u.name)
returntrue; // user found
returnfalse; // user not found
}
private:
int id;
string name;
vector <user> logs;
};
int main () {
User user;
bool valid = false;
user.AddUser(1234567, "Daniel");
user.AddUser(7654321, "Smith");
user.AddUser(1234321, "John");
while (!valid) {
if (user.ValidateUser(user.getIdAndName())) {
cout << "It's Working !";
valid = true; // user is valid
}
else
cout << "no match found" << endl;
}
return 0;