Help with string check and if statements

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.

#include <iostream.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <cstdlib>

char name[14];
char id[8];
int check_id = 0, correct = 0;
int check_name = 0;


void getIdAndName();

void getIdAndName()
{
do
{
cout << "Enter Id:";
cin >> id;
check_id = strlen(id);

cout << "Enter Conducter Name:";
// Also tried "gets (name);"
cin >> name;
check_name = strlen(name);

{

if((strcmp(id, "1234567") == 0) && (strcmp(name, "Daniel") == 0))
{
correct = 0;
}

if((strcmp(id, "7654321") == 0) && (strcmp(name, "Smith") == 0))
{
correct = 0;
}

if((strcmp(id, "1234321") == 0) && (strcmp(name, "John") == 0))
{
correct = 0;
}

else
{
system ("cls");
cout << "no match found" << endl;
correct = 1;
}

}

}while ((correct != 0) && (check_id != 7) && (id != 0) && (check_name < 0));

cout << "It's Working !";

}



int main()
{
getIdAndName();
system ("pause");
return 0;

}



any help would be very appreciated
Last edited on
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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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)
					return true;   // user found
	
			return false;  // 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;
Last edited on
Topic archived. No new replies allowed.