C ++多程序问题(rand,转换,崩溃) [英] C++ Multiple Program Issues (rand, conversion, crashing)
问题描述
首先,我决定在这里发布我的代码,这样每个人都可以理解我的代码问题,并将能够帮助我更好。
To begin with I have decided to post my code here so everyone can understand the issues I have with the code and will be able to assist me better.
main .cpp
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
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
好的,所以我的代码编译得很好, 。它询问名字,它知道它是无效的,但这里是我的第一个问题。当您输入不正确的名称时,它会再次提示您输入。但它第二次进入时崩溃。以下是屏幕截图示例。
Ok so my code compiles fine and goes through everything perfectly. It asks the name, it knows when it is invalid but here comes my first issue. When you enter an incorrect name it again prompts you to enter it. But it crashes when you enter it the second time. Here is a screenshot example.
程序也不会生成随机数。它每个单次运行相同。这是此截图。
The program also does not generate random numbers. Its the same every single run. Here is a screenshot of that.
任何有关这些问题的协助都将非常感激。
Any assistance with these issues would be greatly appreciated.
。我查询了其他问题,当我尝试修复srand(time(NULL));
Also for the random issue. I looked up on the other questions and when I tried the fix "srand(time(NULL));" it tells me that srand is ambiguous.
推荐答案
关于在调用 getName时应用程序崩溃的问题)
,尝试使用以下命令刷新 cin
缓冲区:
In regards to your application crashing when you call getName()
, try flushing the cin
buffer using:
cin.ignore();
或
cin.clear();
清除缓冲区状态。我有一个类似的问题,而我相信这是解决方案。关于你的随机数,你没有初始化一个随机种子。在生成随机数之前初始化随机种子:
to clear the buffer state. I had a similar issue a while back which I believe that was the solution to. In regards to your random number, you havent initialized a random seed. Initialize a random seed prior to generating random numbers using:
srand (time(NULL));
希望这有助。
刚刚看到您的评论。尝试使用以下代替您的随机数种子:
Just saw your comment above. Try using the following instead for your random number seed:
srand(time(0));
确保您 #include< ctime>
在此解决方案的文件头。
Make sure you do #include <ctime>
at the head of file for this solution.
这篇关于C ++多程序问题(rand,转换,崩溃)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!