C++ 多程序问题(rand、转换、崩溃)
C++ Multiple Program Issues (rand, conversion, crashing)
首先,我决定 post 我的代码放在这里,这样每个人都能理解我在代码中遇到的问题,并且能够更好地帮助我。
main.cpp
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include "validate.h"
#include "main.h"
using namespace std;
int main() {
name = getName();
score = quiz();
cout << "\n\n";
system("pause");
return (0);
}
string getName() {
cout << "Enter your name: ";
getline(cin, name);
val.set_item(name);
valid = val.vName();
if (valid) return name;
else {
cout << "Invalid name!\n\n";
getName();
}
}
int quiz() {
for (int loop = 0; loop <= 10; loop++) {
rand1 = rand() % 20 + 1;
rand2 = rand() % 20 + 1;
randop = rand() % 3;
op = operators[randop];
if (op == '*') ans = rand1 * rand2;
else if (op = '+') ans = rand1 + rand2;
else ans = rand1 - rand2;
cout << "What is " << rand1 << op << rand2 << " ? ";
getline(cin, input);
val.set_item(input);
valid = val.vInput();
if (valid) {
istringstream(input) >> inputint;
if (ans == inputint) {
cout << "Correct!\n\n";
score++;
}
else cout << "Incorrect!\n\n";
}
else cout << "Incorrect!\n\n";
}
return score;
}
validate.h
#ifndef validate_h
#define validate_h
using namespace std;
class validate {
string item;
int len;
bool check;
public:
void set_item(string x);
bool vInput() {
len = item.size();
for (int loop = 0; loop < len; loop++) {
if (item[loop] == '-' && loop == 0) continue;
check = isalnum(item[loop]);
if (check) continue;
else return false;
}
return true;
}
bool vName() {
len = item.size();
for (int loop = 0; loop < len; loop++) {
check = isalpha(item[loop]);
if (check) continue;
else return false;
}
return true;
}
};
void validate::set_item(string x) {
item = x;
}
#endif
main.h
#ifndef main_h
#define main_h
string name;
string input;
string getName();
validate val;
bool valid;
int quiz();
int score;
int rand1;
int rand2;
int randop;
int ans;
int inputint;
const char operators[3] = { '-', '+', '*' };
char op;
#endif
好的,所以我的代码编译得很好并且完美地完成了所有事情。它询问名称,它知道什么时候无效,但这是我的第一个问题。当您输入不正确的名称时,它会再次提示您输入。但是当你第二次输入它时它会崩溃。这是一个截图示例。
该程序也不生成随机数。每个运行都一样。这是它的屏幕截图。
如能就这些问题提供任何帮助,我们将不胜感激。
同样针对随机问题。我查看了其他问题,当我尝试修复时 "srand(time(NULL));" 它告诉我 srand 不明确。
关于调用 getName()
时应用程序崩溃的问题,请尝试使用以下方法刷新 cin
缓冲区:
cin.ignore();
或
cin.clear();
清除缓冲区状态。前一段时间我遇到了类似的问题,我相信这是解决方案。关于您的随机数,您还没有初始化随机种子。使用以下方法在生成随机数之前初始化随机种子:
srand (time(NULL));
希望对您有所帮助。
编辑:
刚看到你上面的评论。尝试使用以下代替您的随机数种子:
srand(time(0));
请确保在该解决方案的文件开头执行 #include <ctime>
。
首先,我决定 post 我的代码放在这里,这样每个人都能理解我在代码中遇到的问题,并且能够更好地帮助我。
main.cpp
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include "validate.h"
#include "main.h"
using namespace std;
int main() {
name = getName();
score = quiz();
cout << "\n\n";
system("pause");
return (0);
}
string getName() {
cout << "Enter your name: ";
getline(cin, name);
val.set_item(name);
valid = val.vName();
if (valid) return name;
else {
cout << "Invalid name!\n\n";
getName();
}
}
int quiz() {
for (int loop = 0; loop <= 10; loop++) {
rand1 = rand() % 20 + 1;
rand2 = rand() % 20 + 1;
randop = rand() % 3;
op = operators[randop];
if (op == '*') ans = rand1 * rand2;
else if (op = '+') ans = rand1 + rand2;
else ans = rand1 - rand2;
cout << "What is " << rand1 << op << rand2 << " ? ";
getline(cin, input);
val.set_item(input);
valid = val.vInput();
if (valid) {
istringstream(input) >> inputint;
if (ans == inputint) {
cout << "Correct!\n\n";
score++;
}
else cout << "Incorrect!\n\n";
}
else cout << "Incorrect!\n\n";
}
return score;
}
validate.h
#ifndef validate_h
#define validate_h
using namespace std;
class validate {
string item;
int len;
bool check;
public:
void set_item(string x);
bool vInput() {
len = item.size();
for (int loop = 0; loop < len; loop++) {
if (item[loop] == '-' && loop == 0) continue;
check = isalnum(item[loop]);
if (check) continue;
else return false;
}
return true;
}
bool vName() {
len = item.size();
for (int loop = 0; loop < len; loop++) {
check = isalpha(item[loop]);
if (check) continue;
else return false;
}
return true;
}
};
void validate::set_item(string x) {
item = x;
}
#endif
main.h
#ifndef main_h
#define main_h
string name;
string input;
string getName();
validate val;
bool valid;
int quiz();
int score;
int rand1;
int rand2;
int randop;
int ans;
int inputint;
const char operators[3] = { '-', '+', '*' };
char op;
#endif
好的,所以我的代码编译得很好并且完美地完成了所有事情。它询问名称,它知道什么时候无效,但这是我的第一个问题。当您输入不正确的名称时,它会再次提示您输入。但是当你第二次输入它时它会崩溃。这是一个截图示例。
该程序也不生成随机数。每个运行都一样。这是它的屏幕截图。
如能就这些问题提供任何帮助,我们将不胜感激。
同样针对随机问题。我查看了其他问题,当我尝试修复时 "srand(time(NULL));" 它告诉我 srand 不明确。
关于调用 getName()
时应用程序崩溃的问题,请尝试使用以下方法刷新 cin
缓冲区:
cin.ignore();
或
cin.clear();
清除缓冲区状态。前一段时间我遇到了类似的问题,我相信这是解决方案。关于您的随机数,您还没有初始化随机种子。使用以下方法在生成随机数之前初始化随机种子:
srand (time(NULL));
希望对您有所帮助。
编辑:
刚看到你上面的评论。尝试使用以下代替您的随机数种子:
srand(time(0));
请确保在该解决方案的文件开头执行 #include <ctime>
。