Cow & Bull exercise asking for criticism

closed account (iw0XoG1T)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*

Name: cow_bull.cpp

Purpose: to create a working example of a console cow & bull game.

*/

#include<iostream>
#include<stdexcept>
#include<sstream>
#include<ctime>
#include<cstdlib>

using namespace std;
//=============================================================================

struct Cow_bull{
	Cow_bull(string g,string a)
		:guess(g),answer(a),cow(0),bull(0){
		calculate_bull();
		calculate_cows();
	}
	void set_guess(string g){
		guess=g;
		bull=0; 	//reset to zero for each guess
		cow=0;	//reset to zero for each guess
		g2="";	//resetting string for each guess
		a2="";	//resetting string for each guess
		calculate_bull();
		calculate_cows();
	}
	int get_cow() const{return cow;}
	int get_bull() const{return bull;}
	private:
		string guess;
		string answer;
		string g2;
		string a2;
		int cow;
		int bull;
		void calculate_bull(){
			for(unsigned i =0; i<guess.size();++i){
				if(guess[i]==answer[i]){++bull;}
				else{
					g2=g2+guess[i]; //saving this wrong guess to check for cows
					a2=a2+answer[i]; //saving this answer to check for cows
				}
			}
		}
		void calculate_cows(){
			for(unsigned i =0; i<g2.size();++i){
				for(unsigned ii =0; ii<g2.size();++ii){
					if(g2[i]==a2[ii]){
						++cow;
						a2[ii]=0; /*ensuring that I do not check this answer 
										right twice.*/
						break;
					}
				}
			}
		}
};
//=============================================================================
ostream& operator<<(ostream& os, Cow_bull& cb){
	return os<<"you had "<<cb.get_bull()<<" bulls and "
		<<cb.get_cow()<<" cows.";
}
//=============================================================================
string create_random_answer(){
	srand(time(0));
	stringstream reply;
	for(int i=0; i<4;++i){
		reply<<int(rand() % 10);
	}
	return reply.str();
}
//=============================================================================
bool just_number(string s){
	for(unsigned i=0; i<s.size();++i){
		if(!isdigit(s[i]))return false;
	}
	return true;
}
//=============================================================================

int main()try{
	cout<<"\n\nTry to guess a four number sequence. If you guess the right number\n";
	cout<<"in the right location you will be awarded a bull. If you guess\n";
	cout<<"the right number in the wrong location you receive a cow. The goal is\n";
	cout<<"to get four bulls.\n\n";
	string guess;
	string answer=create_random_answer();
	Cow_bull cb(guess,answer);
	while(cb.get_bull()!=4){
		do{
			cout<<"\nEnter a four number string e.g. 1234 :";
			cin>>guess;
		}while(guess.size()!=4 || !just_number(guess));
		cb.set_guess(guess);

		cout<<cb<<'\n';
	}

	return 0;

}

catch(exception& e){
	cerr<<e.what()<<endl;

	return 1;

}

catch(...){

	return 2;

}
Last edited on
Your main/try/catch blocks are mixed together for some reason. Try/catch blocks go inside of functions, just like ifs/loops/statements.
Your main/try/catch blocks are mixed together for some reason. Try/catch blocks go inside of functions, just like ifs/loops/statements.
That is not true. You can use try-catch outside function braces
Topic archived. No new replies allowed.