关于我的电脑的基本信息:Windows 7 x64 OS
8gb RAM
Using MingW compiler
因此,每次我在编译后通过终端运行程序时,它都会立即停止响应。我自己似乎无法找到问题所在,所以我要求多一双眼睛来为我指出问题。目前我实际上是 C++ 的初学者,所以请耐心等待。
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
bool correct = false;
srand(time(NULL));
vector<string> guess;
vector<string> pallete;
vector<string> secret;
pallete.push_back("red");
pallete.push_back("green");
pallete.push_back("blue");
pallete.push_back("yellow");
for(int i = 0; i < 4 ; i++)
{
secret.push_back(pallete.at(rand()%secret.size()));
}
for(int j = 0; j < 4 ; j++)
{
guess.push_back(pallete.at(rand()%guess.size()));
}
vector<string> secretMem = secret;
cout << "the guess was:";
for(int x = 0; x<4; x++)
{
cout << guess[x] << endl;
}
while(correct == false)
{
int blackP = 0;
int whiteP = 0;
secret = secretMem;
for (int idxB = 0; idxB < secret.size(); idxB++)
{
if (guess[idxB] == secret[idxB])
{
secret[idxB] = "0";
guess[idxB] = "1";
blackP++;
}
}
for (int idxW = 0; idxW < secret.size(); idxW++)
for (int idyW = 0; idyW < guess.size(); idyW++)
{
if (secret[idxW] == guess[idyW])
{
secret[idxW] = "0";
guess[idxW] = "1";
whiteP++;
}
}
if (blackP == 4)
{
cout << "Congratulations you win." << endl << "The secret code was:"<< endl;
for(int y = 0; y<4; y++)
{
cout << secretMem[y] << endl;
}
correct = true;
}
else
cout << "you scored: " << blackP << " Black pegs" << "and" << whiteP << " White pegs" << endl;
}
return 0;
}
另外....这种可怕的代码格式是怎么回事....我只是无法在预览中正确排列,就像我在编写它时一样....