0

So I recently did a Rock-Paper-Scissors program but I wanted to experiment and a different way to make the code more convenient. So I thought it would be a good idea to use a function to make all the IFs and else IFs outcome take place. (Ie if the player chooses rock and computer choose paper then computer wins.)

The problem, however, is that when I tried to code it, The program says "warning: control may reach end of non-void function [-Wreturn-type]". Now from what I gathered from the web, that means that their no way for me to return back to the int main() function once it gets to the other function I created. I thought I had fixed it but. Could someone have a look and tell me what I'm doing wrong?

Here the code :

#include <bits/stdc++.h>
#include <time.h> 
#include <string.h>
#include <stdlib.h> 
#include <stdio.h> 
using namespace std;


int Player_Wins;
int com_wins;

string computer_choices [3] = {"Rock","Paper","Scissors"};
string Player_Choices [3] = {"Rock","Paper","Scissors"};

bool Game_Over =  false; 


int GameOutCome(int a,string b)
{

        int Player_Choice;
        string Computer_Choice;

        srand(time(0));

        string Picked_Choice = computer_choices[rand() % 3 ];

        string Player_Picked_Choice;

        a=Player_Choice;
        b=Picked_Choice;


        if (Player_Choice == 1)
        {
            Player_Picked_Choice= Player_Choices[0];

            if (Player_Picked_Choice== Player_Choices[0] and Picked_Choice==computer_choices [1])
            {

                cout<< "You have chosen " << Player_Picked_Choice <<endl;

                cout<<"\n"<<endl;

                cout<< "The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "The computer wins the round."<<endl;

                return com_wins++;

                cout<<"\n"<<endl;


                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                com_wins<<endl;     

                cout<<"\n"<<endl;

                if (Player_Wins ==3|| com_wins ==3)
                    {

                      Game_Over= true; 

                        if(Game_Over == true)
                           {

                            cout<<"The game is over!!!"<<endl;

                           }
                     }
              }
            }   
            else if (Player_Picked_Choice== Player_Choices[0] and 
                     Picked_Choice==computer_choices[0])
            {

              cout<<"You have chosen "<< Player_Picked_Choice <<endl;

              cout<<"\n"<<endl;

              cout<<" The Computer have chosen "<< Picked_Choice<<endl;

              cout<<"\n"<<endl;

              cout <<"It's a tie."<<endl;

            }
            else if (Player_Picked_Choice== Player_Choices[0] and 
                     Picked_Choice==computer_choices[2])
                {

                     cout<< "You have chosen "<< Player_Picked_Choice <<endl;

                     cout<< " The Computer have chosen"<< Picked_Choice <<endl;

                     cout<<"You won the round."<<endl;

                     return Player_Wins++;

                     cout<<"\n"<<endl;

                     cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                     com_wins<<endl;    

                     cout<<"\n"<<endl;

                     if (Player_Wins ==3|| com_wins ==3)
                          {

                             Game_Over= true; 

                             if(Game_Over == true)
                                 {

                                    cout<<"The game is over!!!"<<endl;

                                 }

                          } 
                   }
                  }

        else if (Player_Choice == 2)
        {

            Player_Picked_Choice= Player_Choices[1];


            if (Player_Picked_Choice== Player_Choices[1] and Picked_Choice==computer_choices [2])
            {

                cout<< "You have chosen "<< Player_Picked_Choice << " The Computer have chosen "<< 
                Picked_Choice <<endl;

                cout<< "The computer wins the round." <<endl;

                return com_wins++;

                cout<<"\n"<<endl;

                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                com_wins<<endl;     

                cout<<"\n"<<endl;

                if (Player_Wins ==3|| com_wins ==3)
                      {

                         Game_Over= true; 

                         if(Game_Over == true)
                             {

                              cout<<"The game is over!!!"<<endl;
                              return 0;
                             }

                       }
            }   
            else if (Player_Picked_Choice== Player_Choices[1] and 
            Picked_Choice==computer_choices[1])
            {

                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

                 cout<<"\n"<<endl;

                 cout<< "The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" It's a tie."<<endl;

                 cout<<"\n"<<endl;

                 cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                 com_wins<<endl;    
            }
            else if (Player_Picked_Choice== Player_Choices[1] and 
                     Picked_Choice==computer_choices[0])
                       {


                         cout<< "You have chosen "<< Player_Picked_Choice <<endl;

                         cout<<"\n"<<endl;

                         cout<< " The Computer have chosen "<< Picked_Choice <<endl;

                         cout<<" You won the round."<<endl;

                         return Player_Wins++;

                         cout<<"\n"<<endl;

                         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                         com_wins<<endl;

                         cout<<"\n"<<endl;

                         if (Player_Wins ==3|| com_wins ==3)
                         {

                             Game_Over= true; 

                             if(Game_Over == true)
                             {

                                cout<<"The game is over!!!"<<endl;

                                 return 0;
                             }
                          } 
                        }
                     }
        else if (Player_Choice == 3)
        {
            Player_Picked_Choice= Player_Choices[2];


            if (Player_Picked_Choice== Player_Choices[2] and Picked_Choice==computer_choices [0])
            {

                cout<< "You have chosen "<< Player_Picked_Choice<<endl;

                cout<<"\n"<<endl;

                cout<<" The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "  The computer wins the round."<<endl;

                return com_wins++;

                cout<<"\n"<<endl;

                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                com_wins<<endl; 

                cout<<"\n"<<endl;


        if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;

            return 0;


          }

        }

            }   
            else if (Player_Picked_Choice== Player_Choices[2] and 
                     Picked_Choice==computer_choices[2])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl; 

                 cout<<"\n"<<endl;

                 cout<<" The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" It's a tie."<<endl;

                 cout<<"\n"<<endl;

                 cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                 com_wins<<endl;

                 return a;  

            }
            else if (Player_Picked_Choice== Player_Choices[2] and 
                     Picked_Choice==computer_choices[1])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

                 cout<<"\n"<<endl;

                 cout<< " The Computer have chosen \n"<< Picked_Choice <<endl;

                 cout<<" You won the round."<<endl;

                 return Player_Wins++;

                 cout<<"\n"<<endl;

                 cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                 com_wins<<endl;    

                 cout<<"\n"<<endl;

             }


    }

 }


int main()
{

  cout<<"Welcome to Rock, Paper Scissors!!"<<endl;

    while (Player_Wins !=3 || com_wins != 3)
    {
        int Player_Choice;

        cout<<"\n"<<endl;

        cout<<"First to three wins. \n To pick rock, press 1\n To pick paper, press 2 \n To pick 
        scissors, Press 3"<<endl;

        cout<<"\n"<<endl;

        cout<<"Please make a choice: ";cin>>Player_Choice;

        string Picked_Choice = computer_choices[rand() % 3 ];

        cout<<"\n"<<endl;

       GameOutCome(Player_Choice,Picked_Choice);


        if (Player_Wins ==3|| com_wins ==3)
             {

               Game_Over= true; 

                 if(Game_Over == true)
                      {

                        cout<<"The game is over!!!"<<endl;


                      }

              }

        }            


      if (Player_Wins==3)
      {
        cout<<" The Player Wins!!"<< endl;  

      }
      else if (com_wins==3)
      {

        cout<< "The Computer Wins!!"<<endl;


      }

  return 0;    
} 

EDIT: Update 12/29/19

Okay so I got the code working to the point to where it gives you the choice but thats it. What else am I going wrong?

#include <bits/stdc++.h>
#include <time.h> 
#include <string.h>
#include <stdlib.h> 
#include <stdio.h> 
using namespace std;


int Player_Wins;
int com_wins;

string computer_choices [3] = {"Rock","Paper","Scissors"};
string Player_Choices [3] = {"Rock","Paper","Scissors"};

bool Game_Over =  false; 


int GameOutCome(int a,string b)
{

        int Player_Choice;


        srand(time(0));

        string Picked_Choice = computer_choices[rand() % 3 ];

        string Player_Picked_Choice;

        a=Player_Choice;
    b=Picked_Choice;


        if (Player_Choice == 1)
        {
            Player_Picked_Choice= Player_Choices[0];

            if (Player_Picked_Choice== Player_Choices[0] and Picked_Choice==computer_choices [1])
            {

                cout<< "You have chosen " << Player_Picked_Choice <<endl;

        cout<<"\n"<<endl;

        cout<< "The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "The computer wins the round."<<endl;


        cout<<"\n"<<endl;

        cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;       

        cout<<"\n"<<endl;

        return com_wins++;

        if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;

            return 0;


          }

          return a;

        }
                return a;
            }   
            else if (Player_Picked_Choice== Player_Choices[0] and Picked_Choice==computer_choices[0])
            {

            cout<<"You have chosen "<< Player_Picked_Choice <<endl;

      cout<<"\n"<<endl;

      cout<<" The Computer have chosen "<< Picked_Choice<<endl;

      cout<<"\n"<<endl;

            cout <<"It's a tie."<<endl;

      return 0;

            }
            else if (Player_Picked_Choice== Player_Choices[0] and Picked_Choice==computer_choices[2])
                {

                     cout<< "You have chosen "<< Player_Picked_Choice <<endl;

           cout<< " The Computer have chosen"<< Picked_Choice <<endl;

                     cout<<"You won the round."<<endl;

           cout<<"\n"<<endl;

                  cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl; 

          cout<<"\n"<<endl;

          return Player_Wins++;

          if (Player_Wins ==3|| com_wins ==3)
          {

            Game_Over= true; 

            if(Game_Over == true)
            {

              cout<<"The game is over!!!"<<endl;

              return 0;



            }

            return 0;

          } 

          return 0;
                }

     return 0;
      }





        else if (Player_Choice == 2)
        {

            Player_Picked_Choice= Player_Choices[1];


            if (Player_Picked_Choice== Player_Choices[1] and Picked_Choice==computer_choices [2])
            {

                cout<< "You have chosen "<< Player_Picked_Choice << " The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "The computer wins the round." <<endl;

        cout<<"\n"<<endl;

                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;       

        cout<<"\n"<<endl;

        return com_wins++;

        if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;
            return 0;


          }
          return 0;

        }

        return 0;
            }   
            else if (Player_Picked_Choice== Player_Choices[1] and Picked_Choice==computer_choices[1])
            {

                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

         cout<<"\n"<<endl;

         cout<< "The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" It's a tie."<<endl;

         cout<<"\n"<<endl;

         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;  

         return a;
            }
            else if (Player_Picked_Choice== Player_Choices[1] and Picked_Choice==computer_choices[0])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

         cout<<"\n"<<endl;

         cout<< " The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" You won the round."<<endl;

         cout<<"\n"<<endl;

         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;

         cout<<"\n"<<endl;

         return Player_Wins++;

         if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;

            return 0;


          }

        return 0;

        }   

                return 0;

            }
         return 0;
        }
        else if (Player_Choice == 3)
        {
            Player_Picked_Choice= Player_Choices[2];

            return a;

            if (Player_Picked_Choice== Player_Choices[2] and Picked_Choice==computer_choices [0])
            {

                cout<< "You have chosen "<< Player_Picked_Choice<<endl;

        cout<<"\n"<<endl;

        cout<<" The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "  The computer wins the round."<<endl;


        cout<<"\n"<<endl;

                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;   

        cout<<"\n"<<endl;

        return com_wins++;


        if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;

            return 0;


          }

          return 0;

        }
            return 0;   
            }   
            else if (Player_Picked_Choice== Player_Choices[2] and Picked_Choice==computer_choices[2])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl; 

         cout<<"\n"<<endl;

         cout<<" The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" It's a tie."<<endl;

         cout<<"\n"<<endl;

         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;

         return 0;  

            }
            else if (Player_Picked_Choice== Player_Choices[2] and Picked_Choice==computer_choices[1])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

         cout<<"\n"<<endl;

         cout<< " The Computer have chosen \n"<< Picked_Choice <<endl;

                 cout<<" You won the round."<<endl;

         cout<<"\n"<<endl;

         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;  

        cout<<"\n"<<endl;

        return Player_Wins++;

      }
      return 0;    
    }
return 0;
}


int main()
{

  cout<<"Welcome to Rock, Paper Scissors!!"<<endl;

    while (Player_Wins !=3 || com_wins != 3)
    {
        int Player_Choice;

        cout<<"\n"<<endl;

        cout<<"First to three wins. \n To pick rock, press 1\n To pick paper, press 2 \n To pick scissors, Press 3"<<endl;

    cout<<"\n"<<endl;

        cout<<"Please make a choice: ";cin>>Player_Choice;

    string Picked_Choice = computer_choices[rand() % 3 ];

    cout<<"\n"<<endl;

    GameOutCome(Player_Choice,Picked_Choice);


      if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;


          }

        }

  }          








      if (Player_Wins==3)
      {
        cout<<" The Player Wins!!"<< endl;  

      }
      else if (com_wins==3)
      {

        cout<< "The Computer Wins!!"<<endl;


      }

  return 0;    
} 
Mactin
  • 17
  • 3
  • 1
    Unrelated, but you should take a look at [Why should I not #include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Using those two together really invites trobule in larger projects. – Lukas-T Dec 28 '19 at 10:53
  • You got seeveral if-else-branches where no value is returned from `GameOutCome`. Also you got sometimes code _after_ a `return`, which is dead code and should also appear as warning. But those are problem you can easily fix yourself, that's why I only comment. – Lukas-T Dec 28 '19 at 10:56

3 Answers3

2

Even though the main problem is simple I would like to add some clarification here:

What happens when you don't have a return-statement?

Now from what I gathered from the web, that means that their no way for me to return back to the int main() function once it gets to the other function I created.

That's wrong. Your function will always return once it reaches the end of the function body. Your code doesn't suddenly stop executing if you miss a return-statement in a branch, your code can't "get stuck" this way.

What can happend however is undefined behaviour, when you try to read the return-value of a function where not all paths return something. If you don't ever read the return value all is fine.

What does happen to code after return?

You sometimes got code after a return. This code will never be executed and your compiler should also warn you about this.

Possible solutions

  1. Go through your code and make sure there is a return in every possible branch.

  2. You don't even need to return anything here, because you never use the return-value. So your function could return void.

Community
  • 1
  • 1
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
1

using namespace std is not good practice, you should avoid that. The naming of your variables is not optimal, while the name itself is not bad as it tells you what the content of the variable(except for a and b in your function) is, you often start with capital letters (e.g. Picked_Choice, Player_Choice and many more) - that's not compliant with the normal c++ naming convention. Why do you use two seperate arrays holding choices for computer and player, only use one if you really need an array holding the string representation of your possible choices. You let the computer chose twice, once in the main loop and then again in the GameOutcome function. There's no sanity check for your inputs, the player could enter random garbage instead of a number 1-3.

The structure of the GameOutCome function is quite messy. First, try to always initialize your variables, so instead of int Player_Choice do int player_choice = 0. Then what is the purpose of srand(time(0))? The line a = Player_Choice overwrites your function parameter (with null actually), that's why you don't have any useful return value.

I'd recommend to only work with integers instead of having the need to get the number from a string array.

Then having something like

Game_Over= true; 
if(Game_Over == true)
{
  cout<<"The game is over!!!"<<endl;
  return 0;        
}

Doesn't make much sense for hopefully obvious reasons. The code is anyway too complicated, try to simplify it. There's a saying "Two weeks of coding can easily save you one hour of planning.".

So an easier approach to problem would be to define a 2d-array that already holds all possible outcomes of a single game.

int outcome[3][3] =     {
                        {0, 1, 2}, 
                        {2, 0, 1}, 
                        {1, 2, 0}
                        } ;
int result = outcome[player_choice][computer_choice];
if (result == 0){
    std::cout << "we have draw\n";
} else if (result == 1){
    std::cout << "you win\n";
} else {
    std::cout << "you lost\n";
}

One player's choice defines the x and the other player's choice defines the y. This simplifies the whole process.

po.pe
  • 1,047
  • 1
  • 12
  • 27
0

The problem is in the GameOutCome function. Your function is a non-void function, but you don't ensure to the compiler a certain return value. In other words, the compiler says that in some special case if your if-else branches are reached your function doesn't return a value.

In additio i suggest you to avoid dead code ( that will never be executed ) in your code. In this case all the code after the return.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35