samedi 26 septembre 2015

How can I change a int into a string in C++ or better yet.. How do I make a function to convert it?

What I am trying to do is convert a int value into a string so the output works correctly. The issue I am having is that's obviously giving me an error message because I cannot assign a string to a integer value. So the help I need is how can I create a function that specifically converts them to a string and return them to my print_result function? Thank you!

#include<iostream>
#include<sstream>
#include<string>

using std::cout;
using std::cin;
using std::endl;
using std::stringstream;
using std::string;

int strategy1(int player, int previous_result, int previous_play, int opponent_previous_play);
int strategy2(int player, int previous_result, int previous_play, int opponent_previous_play);
int strategy3(int player, int previous_result, int previous_play, int opponent_previous_play);
int score(int p1, int p2);
void print_result(int round, int p1, int p2, int winner);





int main (){
    int result, p1, new_p1, p2, new_p2, rounds;
    p1 = 1; // start with rock
    p2 = 1; // start with rock
    cout << "How many rounds:";
    cin >> rounds;

    for(int i=0; i<rounds; i++){
        result = score(p1,p2);
        print_result(i+1, p1, p2, result);
        new_p1 = strategy1(1, result, p1, p2);
        new_p2 = strategy3(2, result, p2, p1);
        p1 = new_p1;
        p2 = new_p2;
    }
}

int strategy1(int player, int previous_result, int previous_play, int opponent_previous_play){

    if(previous_play == 1)
        previous_play = 2;
    else if(previous_play == 2)
        previous_play = 3;
    else if(previous_play == 3)
        previous_play = 1;


    return previous_play;


}

int strategy2(int player, int previous_result, int previous_play, int opponent_previous_play){

    if(player == 1){

        if(previous_result == 2)
            previous_play = opponent_previous_play;
        else
            previous_play = previous_play;
    }

    if(player == 2){
        if(previous_result == 1)
            previous_play = opponent_previous_play;
        else
            previous_play = previous_play;

    }
}

int strategy3(int player, int previous_result, int previous_play, int opponent_previous_play){

     if(player == 1){

        if(previous_result == 2){

            if(previous_play == 1 && opponent_previous_play == 2)
                previous_play = 3;
            else if(previous_play == 2 && opponent_previous_play == 3)
                previous_play = 1;
            else
                previous_play = 2;
        }
    }


    if(player == 2){

        if(previous_result == 1){

            if(previous_play == 1 && opponent_previous_play == 2)
                previous_play = 3;
            else if(previous_play == 2 && opponent_previous_play == 3)
                previous_play = 1;
            else
                previous_play = 2;
        }
    }


    return previous_play;
}

int score(int p1, int p2){
    long result = 0;

    if( ((p1 == 1) && (p2 == 1)) || ((p1 == 2) && (p2 == 2)) || ((p1 == 3) && (p2 == 3)) )
        result = 0; 

    else if( ((p1 == 1) && (p2 == 3 )) || ((p1 == 2) && (p2 == 1)) || ((p1 == 3) && (p2 == 2)) )
        result = 1;

    else if(( (p1 == 1) && (p2 == 2) ) || ((p1 == 2) && (p2 == 3)) || ((p1 == 3 ) && (p2 == 1)) ) 
        result = 2;


        return result;
}




void print_result(int round, int p1, int p2, int winner){


    //ERROR WON'T LET ME CHANGE THE INT INTO A STRING
    if(p1 == 1)
        p1 = "rock";
    else if(p1 == 2)
        p1 = "paper";
    else
        p1 = "scissors";

    //ERROR WON'T LET ME CHANGE THE INT INTO A STRING
    if(p2 == 1)
        p2 = "rock";
    else if(p2 == 2)
        p2 = "paper";
    else
        p2 = "scissors";



    if(winner == 0)
        cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": tie" << endl;
    else if(winner == 1)
        cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": p1" << endl;
    else if(winner == 2)
      cout << "Round " << round << ":" << " p1=" << p1 << " vs" << " p2=" << p2 << ": p2" << endl;

}

Aucun commentaire:

Enregistrer un commentaire