如何从一个功能获得2个独立的输出?

时间:2016-12-13 10:44:02

标签: c++

#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
using namespace std;
int Round3(){
    srand ( time(NULL) ); //initialize the random seed
    string QNum[4];
    string l,m,n,o;
    QNum[0]="Name something you try to avoid when camping in the woods.";
    QNum[1]="Tell me something around the house that you empty";
    QNum[2]="Name something you see outdoors that rhymes with the word brain";
    QNum[3]="Give me the name of a country that has exactly four letters.";
    string ans1[4];
    ans1[0]= "bears";
    ans1[1]="bugs";
    ans1[2]= "snakes";
    ans1[3]="skunks";
    string ans2[4];
    ans2[1]="dishwasher";
    ans2[0]="trashcan";
    ans2[2]="ashtray";
    ans2[3]="ice cube tray";
    string ans3[4];
    ans3[1]="rain";
    ans3[0]="train";
    ans3[2]="drain";
    ans3[3]="plane";
    string ans4[4];
    ans4[1]="iraq";
    ans4[0]="iran";
    ans4[2]="peru";
    ans4[3]="cuba";
    int y;
    int z;
    int b;
    string p1;
    string p2;
    string ans;
    int sum=0;
    int x=0;
    int RandIndex = rand() % 4; //generates a random number between 0 and 3
    cout << QNum[RandIndex] << endl;
    if (QNum[RandIndex]==QNum[0]){
        while (true){
            {
                cout << "Player 1, Enter your answer : ";
                cin >> p1;
                if (p1 == ans1[0]) {
                    y = 42;
                }
                else if (p1 == ans1[1]) {
                    y = 33;
                }
                else if (p1 == ans1[2]) {
                    y = 20;
                }
                else if (p1 == ans1[3]) {
                    y = 4;
                }
                else if (p1 != ans1[4]) {
                    y = 0;
                }
                cout << "Player 2, Enter your answer : ";
                cin >> p2;
                if (p2 == ans1[0]) {
                    z = 42;
                }
                else if (p2 == ans1[1]) {
                    z = 33;
                }
                else if (p2 == ans1[2]) {
                    z = 20;
                }
                else if (p2 == ans1[3]) {
                    z = 4;
                }
                else if (p2 != ans1[4]) {
                    z = 0;
                }
            }
            if (y > z) {
                cout << "PLAYER 1! IT'S YOUR TURN " << endl;
                break;
            }
            else if (z>y) {
                cout << "PLAYER 2! IT'S YOUR TURN" << endl;
                break;
            }
            else if (y==z) {
                cout << "Try new answers" << endl;
            }
    }
    cout << endl;
        for (int a=0; a<7; a++){
            if (l==ans1[0]&&m==ans1[1]&&n==ans1[2]&&o==ans1[3]) {
                continue;
            }
            getline(cin,ans);
            if (ans==ans1[0]){
                b=42; cout << "SURVEY SAYS " << b << "! Good Job! " << endl;
                sum += b;
                l = ans;
            }
            else if (ans==ans1[1]){
                b = 33;
                cout << "SURVEY SAYS " << b << "! Nice one man!"<< endl;
                sum += b;
                m = ans;
            }
            else if (ans==ans1[2]){
                b = 20;
                cout <<"SURVEY SAYS " << b << "! Fantastic man!"<< endl;
                sum += b;
                n = ans;
            }
            else if (ans==ans1[3]){
                b = 4;
                cout <<"SURVEY SAYS " << b << "! Fantastic man!" << endl;
                sum += b;
                o = ans;
            }
            else if (ans != ans1[0] && ans != ans1[1] && ans != ans1[2] && ans != ans1[3]){
                cout << "YOU GOT THIS ONE WRONG! "<< endl; x++;
            }
            if (x == 4) {
                cout << "You lost your turn" << endl;
                break;
            }
        }
        cout << " your total score for this round is " << sum << endl;
            if (x == 4){
                if (y > z) {
                    cout << "Player 2, What's your answer? ";
                    cin >> p2;
                    if (p2==ans1[0]) {
                        b=42;
                        sum += b;
                        cout << "Congratulations!correct answer! You have earned points from Player 1. Player 2 score ";
                    }
                    else if (p2 == ans1[1]) {
                        b = 33;
                        sum += b;
                        cout << "Congratulations!correct answer! You have earned points from Player 1.Player 2 score ";
                    }
          else if (p2 == ans1[2]) {
                        b = 20;
                        sum += b;
                        cout << "Congratulations!correct answer! You have earned points from Player 1.Player 2 score ";
                    }
                    else if (p2==ans1[3]) {
                        b = 4;
                        sum += b;
                        cout << "Congratulations!correct answer! You have earned points from Player 1.Player 2 score ";
                    }
                    else if ( p2 != ans1[4]) {
                        b = 0;
                        cout << "WRONG! Player 1 retains their points. Player 1 score ";
                    }
                }

                else if (z > y) {
                    cout << "Player 1, What's your answer? ";
                    cin >> p1;
                    if (p1==ans1[0]) {
                        b=42;
                        sum += b;
                        cout << "Congratulations!correct answer! You have earned points from Player 1. Player 2 score ";
                    }
                    else if (p1==ans1[1]) {
                        b = 33;
                        sum += b;
                        cout << "Congratulations!correct answer! You have earned points from Player 1.Player 2 score ";
                    }
                    else if (p1 == ans1[2]) {
                        b = 20;
                        sum += b;
                        cout << "Congratulations! correct answer! You have earned points from Player 1.Player 2 score ";
                    }
                    else if (p1 == ans1[3]) {
                        b = 4;
                        sum += b;
                        cout << "Congratulations!correct answer! You have earned points from Player 1.Player 2 score ";
                    }
                    else if ( p1 != ans1[4]) {
                        b = 0;
                        cout << "WRONG! Player 1 retains their points. Player 1 score";
                    }
            }
            cout << "is " << sum << "." << endl;
        }
    }
    return sum;
}

int main(){
    Round3();
    return 0;
}

这只是我正在创造的一个游戏问题的代码,它与家庭的不和。现在最后,我获得了1支球队的得分总和,但我希望两支球队的得分总和能够分开出现。我怎么做? 我希望它们单独出现,以便我可以从所有问题中得到总和,然后比较分数。

3 个答案:

答案 0 :(得分:0)

您可以使用std::pair作为回报。

 std::pair <std::string,double> a (var1,var2);
return a;

答案 1 :(得分:0)

您只能从函数返回单个对象。无法返回多个对象。但是,一个对象可以有子对象,你可以返回一个对象,其子对象是你想要的两个独立的结果。

您可以做的是定义一个由多个值组成的自定义类型。在C ++和一般的面向对象编程中,这种结构称为类。示例类:

struct results {
    int team1, team2;
};

您可以定义一个函数,该函数返回results的实例,其中包含您需要的两个值。

答案 2 :(得分:0)

您还可以使用&#34; out变量&#34;,基本上通过引用函数作为参数传递变量:

void Round3(int& team1Score, int& team2Score)
{
   // Do your score calculations
   team1Score = /*However you would normally calculate the score for team1*/
   team2Score = /*However you would normally calculate the score for team2*/
}

然后可以像这样使用:

int main()
{
    int score1;
    int score2;
    Round(score1, score2);

    // score1 and score2 are now filled with the values calculated in Round3()
    return 0;
}