Program gives me random results

In the first try block, for the first parameter, I am using a string of "1", "2", or "3" to test out the corresponding error messages. However, I would get mixed results. If I use a "1", it should be caught by the addressNotFound block, a "2" would be caught by the AuthenticationError block, and so forth. When I run the program multiple times with the same parameter, the program seems to jump between the catch blocks. Please help me.

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
70
71
72
73
74
75
76
main.cpp
#include "client.h"

#include <iostream>
#include <string>

using namespace std;
int main()
{
	Client object;

	string ipAddress = "192.168.0.2";
	string username, password;

	cout << "Please enter username: ";
	cin >> username;
	cout << "Please enter password: ";
	cin >> password;
	cout << endl;

	try
	{
		object.open("3", username, password);
		cout << "CONNECTED!\n";
	}
	catch (AddressNotFound a)
	{
		a.print();
		exit(0);
	}
	catch (AuthenticationError b)
	{
		b.print();

		for (int count = 0; count < 3; count++)
		{
			cout << endl;
			cout << "Please enter username: ";
			cin >> username;
			cout << "Please enter password: ";
			cin >> password;
			cout << endl;

			try
			{
				object.open("2", username, password);
			}
			catch (AuthenticationError z)
			{
				z.print();
			}
		}
	}
	catch (TimeOutError c)
	{
		c.print();

		for (int count = 0; count < 3; count++)
		{
			cout << endl;

			try
			{
				object.open("3", username, password);
			}
			catch (TimeOutError d)
			{
				d.print();
			}
		}
	}
	cout << endl;
	system("pause");
	return 0;
}


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
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

class Client {
private:
	int f;
public:
	Client();
	void open(const string &address, const string &u, const string &p);
	void move(const string &src, const string &dest);
	void close();
};



class ExcType {
protected:
	string message;
	ExcType() {};
public:
	void print();
};

class AddressNotFound: public ExcType {
public: AddressNotFound();
};
class AuthenticationError: public ExcType {
public: AuthenticationError();
};
class TimeOutError: public ExcType {
public: TimeOutError();
};
class FileNotFoundError: public ExcType {
public: FileNotFoundError();
};

#endif // CLIENT_H


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
client.cpp
#include "client.h"


void ExcType::print() {cout << message << endl;}
AddressNotFound::AddressNotFound() {message = "The specified address was not found on the network.";}
AuthenticationError::AuthenticationError() {message = "The password is incorrect.";}
TimeOutError::TimeOutError() {message = "The connection attempt timed out.";}
FileNotFoundError::FileNotFoundError() {message = "The specified file was not found.";}

Client::Client() {srand(time(0)); f = 0;}
void Client::open(const string &address, const string &u, const string &p) {
	int r = rand() % 100;
	if((r < 20 && f == 0 )|| address == "1") throw AddressNotFound();
	else if((r >= 20 && r < 40) || address == "2") { f = 1; throw AuthenticationError();}
	else if((r >= 40 && r < 60) || address == "3") { f = 1; throw TimeOutError();}
}
void Client::move(const string &src, const string &dest) {
	int r = rand() % 2;
	if(r == 0 || src == "1") throw FileNotFoundError();
	cerr << "FILE MOVED!\n";
}

void Client::close() {}


I was given client.h and client.cpp, and I am not allowed to change it. I just want to understand why my program is running on haywire.
look at line int r = rand() % 100; of Client::open, for example.

You will notice that a decision is made about which exception to throw, not based only on the value of address, but also on the value of r, which is a random value.

For example, if r happens to have a value between 20 and 39 inclusive, AuthenticationError will be thrown, regardless of the value of address.

However, if r >= 60, then the value of address would determine which exception is thrown.

In other words, you should expect the randomness.
Topic archived. No new replies allowed.