gpt4 book ai didi

c++ 师。看似简单的事情让我发疯,请指教

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:47:43 25 4
gpt4 key购买 nike

好的,我已经编程了大约一个星期了,我是从 C++ 开始的。我正在编写一个程序,它是一种算术训练器,你输入你想要的方程式的数量,你输入你对随机数生成器的限制,你指定你想要什么样的方程式(/* - +),然后该程序使用 for 循环并遍历并在 var 中生成方程式及其答案,然后根据该 var 检查用户输入,如果它们与另一个 var 匹配,则计算正确答案的值将递增。在最后一个等式之后,程序会告诉用户他们从多少个等式中答对了多少个,然后将正确答案的数量除以问题的数量,然后将该值乘以 100 u 应该得到该用户算术 session 的准确率.问题是 c++ 一直返回给我一个该死的 0 值,我一辈子都弄不明白为什么 c++ 会这样做。

整个程序:

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
void menu(void);
class session{
public:
session(){
create_session();
}
void create_session(void){
amount = 0;
range_limit = 0;
rights = 0;
answer = 0;
input = 0;
type = "";
while(amount == 0){
cout << "\nHow many equations do you want?: "; cin >> amount;
if(amount < 1){
cout << "\nAmount is too low!";
amount = 0;
}
}
while(range_limit == 0){
cout << "Enter the number range limit: "; cin >> range_limit;
if(range_limit < 1){
cout << "\nRange limit too low!";
range_limit = 0;
}
}
while(type == ""){
cout << "What equation type do you want?: "; cin >> type;
int strlen = type.size();
if(strlen < 1){
cout << "Invalid type input!";
type = "";
}
}
if(type == "+"){
for(int i=0;i<amount;i++){
int a = random();
int b = random();
answer = a + b;
cout << "\n" << a << " + " << b << " = "; cin >> input;
if(answer == input){
rights++;
}
}
}
cout << "\nYou got " << rights << " answers right out of " << amount << " equations." << endl;
cout << "Accuracy percentage: " << getAccuracy() << "%" << endl;
int post_menu=0;
while(post_menu == 0){
cout << "Enter 1 to create another session or 2 to return to the menu: ";
cin >> post_menu;
if(post_menu == 1){
create_session();
}else if(post_menu == 2){
menu();
}else{
cout << "Invalid input: ";
post_menu = 0;
}
}
}
float getAccuracy(){
float x = (rights/amount)*100;
return x;
}
int random(){
int x = 1+(rand()%range_limit);
return x;
}
void set_amount(int a){
amount = a;
}
void set_range_limit(int r){
range_limit = r;
}
void set_rights(int R){
rights = R;
}
void set_answer(int a){
answer = a;
}
void set_input(int i){
input = i;
}
void set_type(string t){
type = t;
}
private:
int amount;
int accuracy;
int range_limit;
int rights;
int answer;
int input;
string type;

};
int main(){
cout << "=== WELCOME TO ARITH! === \n=========================\n";
menu();
return 0;
}

void menu(void){
//Set the seed for random number gen.
srand(time(0));

//Set var for getting menu input, then get the menu input..
int menu_input;
cout << "\n[1]Create a Session. [2]Exit Arith. \nWhat would you like to do?: ";
cin >> menu_input;

//Now we check what the user wants and act accordingly..
if(menu_input > 2){
cout << "error";
menu_input=0;
}else if(menu_input == 1){
session start;
}else if(menu_input == 2){
cout << "\nExiting Arith!";
}else{
cout << "error";
menu_input=0;
}
}

麻烦的部分:

    float getAccuracy(){
float x = (rights/amount)*100;
return x;

一些程序如何返回 0%。

任何人都知道为什么会这样以及如何得到结果。

最佳答案

rightsamount 都是 int ,所以当你划分值(value)时,例如如果你做 5/2 答案将是 2 而不是 2.5。要解决此问题,您需要将其中一个变量转换为 float,如下所示:(float(rights)/amount) * 100

关于c++ 师。看似简单的事情让我发疯,请指教,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9766236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com