Little program I made: Bulls and Cows

Randomly made this, I know it's really really messy, but it works.
Will add functions to replace all the repeats later.
Still just a beginner, can anyone give me tips or advice?

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
121
  //
//  main.cpp
//  Bulls and Cows
//
//  Created by user on 2015/7/17.
//  Copyright (c) 2015年 Warren Leu. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;


int main ()
{
    std::string number, number2, guess1, guess2, guess3, guess4;
    int num1, num2, num3, num4, number3, win, guesses, a, b, guess5, guess6, guess7, guess8, count;
    win = 0;
    guesses = 0;
    
    srand (time(NULL));
    num1 = rand() % 9 + 1;
    
    srand (time(NULL));
    num2 = rand() % 9 + 1;
    while (num2 == num1) {
        srand (time(NULL));
        num2 = rand() % 9 + 1;
    }
    srand (time(NULL));
    num3 = rand() % 9 + 1;
    
    while(num3 == num2 || num3 == num2){
        srand (time(NULL));
        num3 = rand() % 9 + 1;
    }
    srand (time(NULL));
    num4 = rand() % 9 + 1;
    while(num4 == num3 || num4 == num2 || num4 == num1){
        srand (time(NULL));
        num4 = rand() % 9 + 1;
    }
    while(win != 1){
        std::cin >> number;
        number2 = number;
        int number3 = atoi(number2.c_str());
        guess1 = number[0];
        guess2 = number[1];
        guess3 = number[2];
        guess4 = number[3];
        int guess5 = atoi(guess1.c_str());
        int guess6 = atoi(guess2.c_str());
        int guess7 = atoi(guess3.c_str());
        int guess8 = atoi(guess4.c_str());
        if(number3 > 9999){
            std::cout << "Too Large!\n";
            continue;
        }
        else if(number3 < 1000) {
            std::cout << "Too Small!\n";
            continue;
        }
        if(guess5 == guess6 || guess5 == guess7 || guess5 == guess8){
            std::cout << "No Repeats!\n";
            continue;
        }
        else if(guess6 == guess5 || guess6 == guess7 || guess6 == guess8){
            std::cout << "No Repeats!\n";
            continue;
        }
        else if(guess7 == guess5 || guess7 == guess6 || guess7 == guess8){
            std::cout << "No Repeats!\n";
            continue;
        }
        else if(guess8 == guess5 || guess8 == guess6 || guess8 == guess7){
            std::cout << "No Repeats!\n";
            continue;
        }
        if(guess5 == num1){
            a += 1;
        }
        else if(guess5 == num2 || guess5 == num3 || guess5 == num4){
            b += 1;
        }
        if(guess6 == num2){
            a += 1;
        }
        else if(guess6 == num1 || guess6 == num3 || guess6 == num4){
            b += 1;
        }if(guess7 == num3){
            a += 1;
        }
        else if(guess7 == num1 || guess7 == num2 || guess7 == num4){
            b += 1;
        }if(guess8 == num4){
            a += 1;
        }
        else if(guess8 == num2 || guess8 == num3 || guess8 == num1){
            b += 1;
        }
        count += 1;
        if(a == 4){
            win = 1;
            std::cout << "YOU WIN!!!\n";
            std::cout << "You guessed ";
            std::cout << count;
            std::cout << " times.";
            return 0;
        }
        std::cout << "\n";
        std::cout << a;
        std::cout <<"A";
        std::cout << b;
        std::cout << "B\n\n";
        a = 0;
        b = 0;
    }
    return 0;
}
Last edited on
it has a bunch of errors in it.
for example:
1
2
3
4
        ch1 = guess5;
        ch2 = guess6;
        ch3 = guess7;
        ch4 = guess8;

where are they declared?

use getline on line 46:
http://www.cplusplus.com/reference/string/string/getline/

i dont think you need all of those continue's.
Last edited on
1. Was planning to make it so if you type characters, it will tell you so. But gave up half way. Would appreciate it if someone can tell me how to do that as well.
2. getline... hm, never tried it.
3. Probably not. I'll try to change that.
please use user understandable variable names.It's difficult to get which variable for what purpose.
Can you say what it does...? Comments... and you forgot to use: #include <string>

Also delete this:

1
2
3
4
        ch1 = guess5;
        ch2 = guess6;
        ch3 = guess7;
        ch4 = guess8;
Last edited on
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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;


int main ()
{
    std::string input, str_first_number, str_second_number, str_third_number, str_fourth_number;
    int random_first_number, random_second_number, random_third_number, random_fourth_number, win, guesses, a, b;
    win = 0;
    guesses = 0;
/****************************Randomizer*************************************/
    srand (time(NULL));
    random_first_number = rand() % 9 + 1;
    
    srand (time(NULL));
    random_second_number = rand() % 9 + 1;
    while (random_second_number == random_first_number) {
        srand (time(NULL));
        random_second_number = rand() % 9 + 1;
    }
    srand (time(NULL));
    random_third_number = rand() % 9 + 1;
    while(random_third_number == random_second_number || random_third_number == random_first_number){
        srand (time(NULL));
        random_third_number = rand() % 9 + 1;
    }
    srand (time(NULL));
    random_fourth_number = rand() % 9 + 1;
    while(random_fourth_number == random_third_number || random_fourth_number == random_second_number || random_fourth_number == random_first_number){
        srand (time(NULL));
        random_fourth_number = rand() % 9 + 1;
    }
/*************************Input********************************************/
    while(win != 1){
        std::cin >> input;
        //Making input string an int for seeing if it's too large or small
        int input_in_int = atoi(input.c_str());
        str_first_number = input[0];
        str_second_number = input[1];
        str_third_number = input[2];
        str_fourth_number = input[3];
        //Making all the number from string to int
        int int_first_number = atoi(str_first_number.c_str());
        int int_second_number = atoi(str_second_number.c_str());
        int int_third_number = atoi(str_third_number.c_str());
        int int_fourth_number = atoi(str_fourth_number.c_str());
        //Checking if input too large or too small
        if(input_in_int > 9999){
            std::cout << "Too Large!\n";
            continue;
        }
        else if(input_in_int < 1000) {
            std::cout << "Too Small!\n";
            continue;
        }
        //Checking if there's any repeats in the inpiut
        if(int_first_number == int_second_number || int_first_number == int_third_number || int_first_number == int_fourth_number){
            std::cout << "No Repeats!\n";
            continue;
        }
        else if(int_second_number == int_first_number || int_second_number == int_third_number || int_second_number == int_fourth_number){
            std::cout << "No Repeats!\n";
            continue;
        }
        else if(int_third_number == int_first_number || int_third_number == int_second_number || int_third_number == int_fourth_number){
            std::cout << "No Repeats!\n";
            continue;
        }
        else if(int_fourth_number == int_first_number || int_fourth_number == int_second_number || int_fourth_number == int_third_number){
            std::cout << "No Repeats!\n";
            continue;
        }
/***************************AB Determiner **********************************/
/************************* A = Bull, B = Cow *******************************/
        if(int_first_number == random_first_number){
            a += 1;
        }
        else if(int_first_number == random_second_number || int_first_number == random_third_number || int_first_number == random_fourth_number){
            b += 1;
        }
        if(int_second_number == random_second_number){
            a += 1;
        }
        else if(int_second_number == random_first_number || int_second_number == random_third_number || int_second_number == random_fourth_number){
            b += 1;
        }if(int_third_number == random_third_number){
            a += 1;
        }
        else if(int_third_number == random_first_number || int_third_number == random_second_number || int_third_number == random_fourth_number){
            b += 1;
        }if(int_fourth_number == random_fourth_number){
            a += 1;
        }
        else if(int_fourth_number == random_third_number || int_fourth_number == random_second_number || int_fourth_number == random_first_number){
            b += 1;
        }
/**********************************Output***********************************/
        guesses += 1;
        if(a == 4){
            win = 1;
            std::cout << "YOU WIN!!!\n";
            std::cout << "You guessed ";
            std::cout << guesses;
            std::cout << " times.";
            return 0;
        }
        std::cout << "\n";
        std::cout << a;
        std::cout <<"A";
        std::cout << b;
        std::cout << "B\n\n";
        a = 0;
        b = 0;
    }
    return 0;
}


Still messy as all holy heck. Change the names so they're more understandable, added comments.
Thinking of a way to get the random numbers with arrays...
Also, have to find a way to do repeating code with functions, since the ints have to check with other ints, "sigh" more arrays.
Last edited on
Topic archived. No new replies allowed.