小编Hyr*_*x77的帖子

为什么这个案例阻止不执行?

几周前我刚开始尝试使用C++.在尝试使用C++之前,我对Java有了相当不错的把握.很多人告诉我他们在语法方面非常相似.

在底部有一个switch语句启动战斗场景.每当我选择战斗选项时,它就会关闭程序.

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>     // For rand()
#include <string>
#include <sstream>
#include <algorithm>   // transform()
#include <cctype>      // toupper(), tolower()
#include <functional>  // ptr_fun()
#include <time.h>
// PUT S*** BELOW THIS POINT
//____________________________________________________________________________

using namespace std;

int main()
{
    /*    Expirimental text based adventure game.
     *    Mainly being used for practice methods.
     *    Developed by Zack Cook.

     Generic title. Bad story. Bad interactions. 

     Lots and lots of bad, bad code. Be warned.
     */   

    string charName; …
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
2
解决办法
145
查看次数

为什么这样做/永远不会结束?

它只是保持循环.数字继续减少,直到该计划结束.我滥用了什么吗?

playerHealth和orcHealth ints是100.

randomNumber = ("%10d", 1 + (rand() % 100));
Run Code Online (Sandbox Code Playgroud)

这是我在srand()解释页面上看到的随机数的方式.如果这是错的,应该怎么做?

这里还有其他问题吗?

    switch(charDecision)
{
case 1:
    cout << "FIGHT" << endl;
    do{
        randomNumber = ("%10d", 1 + (rand() % 100));
        if(randomNumber >= 50){
            orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
        cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
        }
        else
        {
            playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
            cout << "The orc hit you! You …
Run Code Online (Sandbox Code Playgroud)

c++ infinite-loop do-while

2
推荐指数
2
解决办法
290
查看次数

标签 统计

c++ ×2

do-while ×1

infinite-loop ×1