我的程序永远循环,只执行我告诉它要做的一半 [英] My program loops forever and does only a half of what I told it to do

查看:136
本文介绍了我的程序永远循环,只执行我告诉它要做的一半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,我开始制作自己的Fire Emblem升级计算器,但是由于某种原因,它无限循环。我找不到答案。
您可以在我的代码中查找任何错误吗?

I'm new to programming and I started to make my own Fire Emblem level up calculator, but for some reason it loops infinitely. I can't find an answer. Could you look for any mistakes in my code, please?

#include<iostream>
#include<cstdlib>
#include<windows.h>
int main () {
using std::cout;
using std::cin;
using std::endl;
int level ,str , skl, lck, def, res, inc, hp, spd, nr ;
char cha[10];
nr=1;
cout<< "Which character?";
cin>> cha ;
cout<< "You chose" << cha << "." << endl << "What level do you want him/her to be?";
cin>> level ;
        if (cha[6] = 'Dieck' ) {
            hp = 26;
            str = 9;
            skl = 12;
            spd = 10;
            lck = 4;
            def = 6;
            res = 1;
            while (level > 0) {

            if (rand() % 100 < 90) {
                inc=1;
                //cout<< "HP increased by" << inc ;
                hp+1;

        }
            if (rand() % 100 < 40) {
                inc=1;
            //  cout<< "Strenght/Magic increased by" << inc ;
                str+1;  
        }
            if (rand() % 100 < 40) {
                inc=1;
                //cout<< "Skill increased by" << inc ;
                skl+1;  
        }
            if (rand() % 100 < 30) {
                inc=1;
            //  cout<< "Speed increased by" << inc ;
                spd+1;  
        }
            if (rand() % 100 < 35) {
                inc=1;
                //cout<< "Luck increased by" << inc ;
                lck+1;  
        }
            if (rand() % 100 < 20) {
                inc=1;
                //cout<< "Defense increased by" << inc ;
                def+1;  
        }
            if (rand() % 100 < 15) {
                inc=1;
                //cout<< "Resistance increased by" << inc ;
                res+1;

        }
        nr+1;
        level-1;
        //cout<<"NR."<< nr << " New stats (in order) HP/STR/SKL/SPD/LCK/DEF/RES " << hp <<" "<< str <<" "<< skl <<" "<< spd <<" "<< lck <<" "<< def <<" "<< res << endl; 
        Sleep(1);

        }
        cout<< "Stats "<< "HP/STR/SKL/SPD/LCK/DEF/RES " << hp <<" "<< str <<" "<< skl <<" "<< spd <<" "<< lck <<" "<< def <<" "<< res << endl;
        return 0 ;
}

}

推荐答案

您遇到的一个问题是 cha [6] ='Dieck'

cha [6] 是单个字符,例如'D'或'i',但不是整个字符。同样, = 设置 cha [6] 等于'Dieck'不会发生,因为'Dieck'不是有效字符。要比较它们,您需要 == ,但是您一次只能比较一个字符,例如 cha [0] =='D'

cha[6] is a single character such as 'D' or 'i' but not the whole thing. Also = sets cha[6] equal to 'Dieck' which can't happen because 'Dieck' is not a valid character. To compare them you'd need == but you can only compare one character at a time like cha[0] == 'D'

实际上,您应该将输入内容设为字符串,并使用字符串的compare()方法。

Really you should make your input a string, and use the compare() method of the string.

std::string input;
// stuff
cin >> input;

if (input.compare("Dieck") == 0)
{
   // more stuff
}

这篇关于我的程序永远循环,只执行我告诉它要做的一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆