视觉对象触发了断点 c++

Visual triggered a breakpoint c++

本文关键字:断点 c++ 对象 视觉      更新时间:2023-10-16

我是编码新手,我试图写一个刽子手游戏。程序随机选择10个单词中的一个,仅显示"_",每次猜测后屏幕刷新。

问题是有时循环显示的是整个屏幕的 2 倍。所以,我尝试使用——

cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

但后来 Visual 告诉我,程序触发了一个断点。怎么了?

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void showLogo();
int main()
{
int number, amount;
int mistake = 5;
int wrongLetter = 0;
char letter;
char tab[13];
char unknown[13];
char usedLetters[5];
char *wsk;
srand(time(NULL));
number = rand() % 10;  //choose one of 10 words
string words[10] ={"LONGITUDINAL","UNFORTUNATELY","EXPLANATORY",
"PERENNIAL","UNPUTDOWNABLE","REMORSELESS",
"INTERMITTENT","ADJUDICATE","INERSTITIAL",
"MALPOSED" };
amount = size(words[number]);  
strcpy_s(tab, words[number].c_str()); //string to char
for (int i = 0; i < amount; i++)
{
unknown[i] = '_';
}
while (mistake)
{
int noOfGuessed = 0;
showLogo();
cout << "You have: " << mistake << " chances." << endl << endl;
cout << "Used words: ";
for (int i = 0; i < wrongLetter; i++)
{
cout << usedLetters[i] << " ";
}
cout << endl << endl;
for (int i = 0; i < amount; i++)
{   
cout << unknown[i] << "  ";
}
cout << "t Enter a letter: ";
letter = getchar();
letter = toupper(letter);
for (int i = 0; i < amount; i++)
{
if (letter == tab[i] && unknown[i] == '_')
{
wsk = &unknown[i];
*wsk = letter;
noOfGuessed++;
}
}
if (noOfGuessed == 0)
{
mistake--;
usedLetters[wrongLetter] = letter;
wrongLetter++;
}
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
system("cls");
}
system("pause");    
return 0;
}
void showLogo()
{
cout << "ttttttt HANGMAN" << endl;
cout << endl;
cout << endl;
cout << endl;
}

ignore()将设置cin的eof位,并使流处于错误状态。调用ignore()后,应始终清除流错误位。

cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
cin.clear();