在我休息之前如何运行部分代码?

时间:2016-08-25 06:40:12

标签: c++

我需要帮助我的代码我会把它全部放进去,如果有人可以清理它,看起来不错,但我会突出显示我需要帮助的地方。

    #include <Windows.h>
    #include <stdlib.h>
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include <ctime>
    using namespace std;
    int main() {
        string ready;
        system("@echo off");
        system("cls");
        cout << "ready to play? (y/n to play.): " << endl;
        cin >> ready;
        if (ready != "y")
            do { 
                cout << "O.K. Goodbye!" << endl;
                return 0;
            } while (ready == "y");
            cout << "OK!" << endl;
            system("pause");
            system("cls");
            int number = 0;
            int min = 1;
            int max = 125;
            int userinput;
            srand(time(0));
            number = rand() % (max - min + 1) + min;
            int guesses_left = 10;

                cout << "please try to guess a number between 1 and 125, you currently have: " << guesses_left << " guesses left" << endl;
                cin >> userinput;
                if (userinput > number){
                    cout << "Sorry your guess is too high, please guess again.";
                    cout << endl;
                    cout << "you now have: " << guesses_left << " guesses left. Please choose again.";

                }
                else if (userinput < number){
                    cout << "Soory, your guess is too low, please guess again." << endl;
                    cout << "You now have: " << guesses_left << " guesses left. Please choose again.";

                }
                else if (userinput == number){
                    cout << "congrats you won :)... Here have a prize as you won with: " << guesses_left << " guesses left." << endl << "no but seriously WELL DONE!!! :D";
                    system("cd C:\Program Files\Internet Explorer");
                    system("iexplore https://media.property118.com/wp-content/uploads/2013/12/Best-Property-Forum.jpg");
                    return 0;


            }


  return 0;  }

我正在努力的是这个

cout << "please try to guess a number between 1 and 125, you currently have: " << guesses_left << " guesses left" << endl;
            cin >> userinput;
            if (userinput > number){
                cout << "Sorry your guess is too high, please guess again.";
                cout << endl;
                cout << "you now have: " << guesses_left << " guesses left. Please choose again.";

            }
            else if (userinput < number){
                cout << "Soory, your guess is too low, please guess again." << endl;
                cout << "You now have: " << guesses_left << " guesses left. Please choose again.";

            }
            else if (userinput == number){
                cout << "congrats you won :)... Here have a prize as you won with: " << guesses_left << " guesses left." << endl << "no but seriously WELL DONE!!! :D";
                system("cd C:\Program Files\Internet Explorer");
                system("iexplore https://media.property118.com/wp-content/uploads/2013/12/Best-Property-Forum.jpg");
                return 0;


        }

我正在尝试循环这段代码,直到我为你在游戏中失去生命而烦恼。因此,如果我猜得更高,它应该说它更高,然后循环回来给我另一个去。我昨天才开始学习C ++,这主要是我的代码(其中一些是改编自其他人的帖子,如srand和number = rand是其他人)

1 个答案:

答案 0 :(得分:1)

我在循环时更改了你的“y / n”:

cout << "ready to play? (y/n to play.): " << endl;
cin >> ready;
if (ready != "y")
{
   cout << "O.K. Goodbye!" << endl;
return 0;

}
 else
    cout << "OK!" << endl;

如果您想强迫用户按'y'或'n'继续前进,我也会给你一个版本的版本:

        cout << "ready to play? (y/n to play.): " << endl;
        cin >> ready;
while ( ready != 'y' && ready != 'n')
{
cout << "Only answer with 'y' or 'n' "<< endl;
cin >> ready;
}

以及用户猜测的游戏循环:

int guesses_left = 10;
cout << "please try to guess a number between 1 and 125, you currently have: " << guesses_left << " guesses left" << endl;
while (guesses_left != 0)
{
                cin >> userinput;
                if (userinput > number){
                    cout << "Sorry your guess is too high, please guess again.";
                    cout << endl;
            --guesses_left;
                    cout << "you now have: " << guesses_left << " guesses left. Please choose again.";

                }
                else if (userinput < number){
                    cout << "Soory, your guess is too low, please guess again." << endl;
            --guesses_left;
                    cout << "You now have: " << guesses_left << " guesses left. Please choose again.";

                }
                else if (userinput == number){
                    cout << "congrats you won :)... Here have a prize as you won with: " << guesses_left << " guesses left." << endl << "no but seriously WELL DONE!!! :D";
                    system("cd C:\Program Files\Internet Explorer");
                    system("iexplore https://media.property118.com/wp-content/uploads/2013/12/Best-Property-Forum.jpg");
                    return 0;
            }
}
cout << "You have used up all your guesses." << endl;
return 0;