Hi, I am trying to compile some code using a g++ GNU Compiler, and it keeps complaining. Other people have told me the exact same code compiles with their compiler. Anyone know why?

Here is the code...

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
char getUserChoice();
char getCompChoice();
int main()
{
  char userChoice;
  char compChoice;
  compChoice = getCompChoice();
  userChoice = getUserChoice();
  cout << "User picked: " << userChoice << endl;
  cout << "Computer picked: " << compChoice << endl;
  return (0);
}
 
char getUserChoice ()
{
  int again = 0;
  do {
    char x;
    cout << "Please enter Rock, Paper, or Scissors: ";
    cin >> x;
    cin.ignore(1000, '\n');
    x = toupper(x);
    if (x == 'R') {
      return ('R');
    } else if (x == 'P') {
      return ('P');
    } else if (x == 'S') {
      return ('S');
    } else {
      again = 1;
      cout << "Not a good choice. ";
    }
  } while (again == 1);
}
 
char getCompChoice()
{
  srandom(time(0));
  int n = random() % 3;
  n++;
  if (n == 1) {
    return ('R');
  } else if (n == 2) {
    return ('P');
  } else {
    return ('S');
  }
}

This is compiler error.

cc1plus: warnings being treated as errors
hw3b.cc: In function 'char getUserChoice()':
hw3b.cc: warning: control reaches end of non-void function

The last else in the getChar() function has no return statement, that's what the warning is all about. It is always a better choice to set the return variable in the condition and return that at the end of the funciton. Something like this:

// Untested
char getUserChoice ()
{
  int again = 0;
  do {
    char x = '\0', result = '\0';
    cout << "Please enter Rock, Paper, or Scissors: ";
    cin >> x;
    cin.ignore(1000, '\n');
    x = toupper(x);
    if (x == 'R') {
      result = 'R';
    } else if (x == 'P') {
      result = 'P';
    } else if (x == 'S') {
      result = 'S';
    } else {
      again = 1;
      cout << "Not a good choice. ";
    }
  } while (again);
  return result;
}

Thanks, that cleared it up. I thought If I ruturned a variable like that I would have to say so in the prototype, call, and the title of the definition like the char getUserChar(char);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.