it keeps saying everything is and invalid input...why?

i dont understand where the logic falls apart, everything makes sense to 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//libraries
#include <algorithm> // reference to transform
#include <cctype> //reference to tolower
#include <cstdlib> //random number usage
#include <ctime> //random number usage
#include <iostream>
using namespace std;
//required for conversion to uppercase
class toUpper {public: char operator() (char c) const {return toupper(c);}};

//Programmer defined data types
//NONE

//Special compiler dependent definitions
//NONE

//global constants/variables
//NONE

//Programmer defined functions
void introduction(string obj, string ins);  //user introduction
string getColor(string cC); //
int chckColr(int cC, string u1, string c1); //
int chckPstn(int cP, string u1, string c1); //
bool inptVali(bool iV, string u1); //

//main program
int main()
{
  //Data
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  string objective = "will run the mastermind game until user opts out."; //program objective
  string instructions = "Try to guess which four colors the computer has chosen"; //user instructions
  string c1 = "DDDD";
  string u1;
  int cC = 0;
  int cP = 0;
  int uG = 0;
  char letsGo = 'Y';
  bool iV = false;
  //user introduction
  introduction(objective, instructions);
  while (true)
  {
    //computer choices
    c1 = (getColor(c1));
    cout << c1 << endl;
    //
    while (true)
    {
      cC = 0;
      cP = 0;
      //
      do
      {
        cout <<"Guess the colors. Ex; ['BRGY'] ";
        getline(cin, u1);
        transform(u1.begin(), u1.end(), u1.begin(), toUpper()); //change from uppercase to lower
        iV = (inptVali(iV, u1));
        if (iV == false) cout << "Invalid Input. Try Again" << endl;
      }while (iV == false);
      //
      cC = chckColr(cC, u1, c1);
      cP = chckPstn(cP, u1, c1);
      cout <<endl;
 
      cout <<"You guessed " << cC << " colors correctly" << endl;
      cout <<"You guessed " << cP << " positions correctly" << endl << endl;
      uG++;
      if (cP == 4 && cC == 4)    
      {
        cout << "[WINNER] -- You guess all colors and positions correctly " <<endl;
        break;
      }
      else if (uG == 10)
      {
        cout << "[Game Over] -- No more guesses remaining" << endl;
        cout << "Computer's choice was " << c1 << endl;
        break;
      }//else
    }//while

    //
    cout <<"You guess all colors and positions correctly " <<endl;
    cout <<"Do you want to play again? [Y for yes/N for no] ";
    cin >> letsGo;
    cin.ignore(1000, 10);
    cout << endl;
    if (letsGo == 'n' || letsGo == 'N') break;
  }
}//main

// user introduction
void introduction(string obj, string ins)
{
  //data
  //obj is the program objective
  //ins is the user instructions

  //output user introduction
  cout << "Objective: This program will " << obj << endl; 
  cout << "Programmer: Prosper\n"; 
  cout << "Editor(s) used: Notepad\n"; 
  cout << "Compiler(s) used: TDM MinGW\n"; 
  cout << "File: " << __FILE__ << endl; 
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl; 
  cout << ins << endl;
  cout << "Enter 'B' for blue, 'R' for red, 'G' for green, or 'Y' yellow." << endl; 
  cout << "The computer's choice could be any combination of these colors." << endl;
  cout << "Your answer should look like this [BGYR]." << endl;
  cout << "You have 10 chances to guess correctly, GOOD LUCK!" <<endl;
}//introduction

//Pick a random color
string getColor(string cC)
{
  //char cc is the current being set 
  int rndNmb; //the random number
  for (int i = 0; i < 4; i++)
  {
    rndNmb = rand() % 4;
    //
    switch (rndNmb)
    {
      case 0:
        cC[i] = 'R';
        break; 
      case 1:
        cC[i] = 'B';
        break;
      case 2:
        cC[i] = 'G';
        break;
      case 3:
        cC[i] = 'Y';
        break;
    }
  }
  return cC;
}

//validate answer
bool inptVali(bool iV, string u1)
{
  //
  bool cV = true;
  for (int i = 0; i < u1.length(); i++)
  {
    if (u1[i] != 'R' || 'B' || 'G' || 'Y') cV = false;
    else cV = true;
  }
  if (u1.length() == 4 && cV == true) iV = true;
  return iV;
}

//check answer
int chckColr(int cC, string u1, string c1)
{
  //
  int i;
  int uRs = 0;
  int uGs = 0;
  int uYs = 0;
  int uBs = 0;
  int cRs = 0;
  int cGs = 0;
  int cYs = 0;
  int cBs = 0;
  //
  for (i = 0; i < 4; i++)
  {
    if (u1[i] == 'R') uRs++;
    if (u1[i] == 'G') uGs++;
    if (u1[i] == 'Y') uYs++;
    if (u1[i] == 'B') uBs++;
    //
    if (c1[i] == 'R') cRs++;
    if (c1[i] == 'G') cGs++;
    if (c1[i] == 'Y') cYs++;
    if (c1[i] == 'B') cBs++;
    //
  }
  //
  for (i = 0; i < uRs; i++)
  { 
    if (uRs > i && cRs > i) cC++;
  }
  for (i = 0; i < uGs; i++)
  { 
    if (uGs > i && cGs > i) cC++;
  }
  for (i = 0; i < uYs; i++)
  { 
    if (uYs > i && cYs > i) cC++;
  }
  for (i = 0; i < uBs; i++)
  { 
    if (uBs > i && cBs > i) cC++; 
  }
  return cC;
}

//check answer
int chckPstn(int cP, string u1, string c1)
{
  //
  if (u1[0] == c1[0]) cP++;
  if (u1[1] == c1[1]) cP++;
  if (u1[2] == c1[2]) cP++;
  if (u1[3] == c1[3]) cP++;
  return cP;
}
if (u1[i] != 'R' || 'B' || 'G' || 'Y')
Ouch! Horrible!


Also, you need to break from the loop at the first false item, or it will only ever finish with the truthity or falsity of the last element.
Last edited on
why is that bad? and what do you mean
You can't combine or (||) operations like that. You probably meant something like
if ( ( u1[i] != 'R' ) && ( u1[i] != 'B' ) && ( u1[i] != 'G' ) && ( u1[i] != 'Y') ) cV = false


A simpler validator would be something like (untested):
1
2
3
4
bool inptVali( string u1 )
{
   return u1.length() == 4   &&   u1.find_first_not_of( "RGBY" ) == string::npos;
}



You have also written far too much code at once, without testing each bit as you go along.


Do you really only have 4 colours in your Mastermind game? Seems like another example of shrinkflation!
Last edited on
that makes sense, i see how && does what i want it to. thank you!
Last edited on
Topic archived. No new replies allowed.