6 Replies - 215 Views - Last Post: 12 May 2014 - 05:58 AM Rate Topic: -----

#1 rld001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 20-February 14

C++ Loop not comparing values correctly?

Posted 12 May 2014 - 01:17 AM

So, I have the beginnings of a rock paper scissors game which i have created before in Java, and i am attempting to create it in c++.

Problem is, the function "int checkConvertInput" containing a loop to make sure input is valid, is not exiting the loop. As far as i can see the two strings are not comparing. Is there a library function for this? I know C had strcmp but i am not sure if it applies here.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int checkConvertInput(int playerSign, string signs[]);

int main() {
    
    string signs[] = {"rock","paper","scissors"};
    
    int playerSign, computerSign;
    
    for (bool continueLoop = true; continueLoop == true;) {
        
        playerSign = checkConvertInput(playerSign, signs);
        
    }
    
    
    return 0;
}

int checkConvertInput(int playerSign, string signs[]) {
    
    bool validInput = false;
    
    while (validInput == false) {
        
        string rawInput;
        
        cout << "Type Rock, Paper or Scissors." << endl;
        cin >> rawInput;
        
        /*
        cout << rawInput;   //used to debug
        cout << signs[0];
        */
        
        validInput = true; // Assume it is true, to avoid having to type this 3 times...
        
        if (rawInput == signs[0]) {
            playerSign = 0;
        } else if (rawInput == signs[1]) {
            playerSign = 1;
        } else if (rawInput == signs[2]) {
            playerSign = 2;
        } else {
            validInput = false;
        }
    }
    
    return playerSign;
}



Thanks,
Rob

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Loop not comparing values correctly?

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: C++ Loop not comparing values correctly?

Posted 12 May 2014 - 01:33 AM

avoid using == for string comparisons. That's the reason why your if else statements end up executing else block (which is setting validInput to false).

Try something like rawInput.compare(signs[0/1/2]) instead.
Was This Post Helpful? 0
  • +
  • -

#3 rld001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 20-February 14

Re: C++ Loop not comparing values correctly?

Posted 12 May 2014 - 01:37 AM

I changed to .compare, and get the following

Undefined symbols for architecture x86_64:
"checkConvertInput(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)", referenced from:
_main in rockPaperScissors-fd7f8c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: C++ Loop not comparing values correctly?

Posted 12 May 2014 - 02:23 AM

that's strange, what change you did? please post your modified code.
Was This Post Helpful? 0
  • +
  • -

#5 rld001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 20-February 14

Re: C++ Loop not comparing values correctly?

Posted 12 May 2014 - 02:25 AM

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int checkConvertInput(int playerSign, string signs[]);

int main() {
    
    string signs[] = {"rock","paper","scissors"};
    
    int playerSign, computerSign;
    
    for (bool continueLoop = true; continueLoop == true;) {
        
        playerSign = checkConvertInput(playerSign, signs);
        
    }
    
    
    return 0;
}

int checkConvertInput(int &playerSign, string signs[]) {
    
    bool validInput = false;
    
    while (validInput == false) {
        
        string rawInput;
        
        cout << "Type Rock, Paper or Scissors." << endl;
        cin >> rawInput;
        
        /*
        cout << rawInput;
        cout << signs[0];
        */
        
        validInput = true; // Assume it is true, to avoid having to type this 3 times...
        
        if (rawInput.compare(signs[0])) {
            playerSign = 0;
        } else if (rawInput.compare(signs[1])) {
            playerSign = 1;
        } else if (rawInput.compare(signs[2])) {
            playerSign = 2;
        } else {
            validInput = false;
        }
    }
    
    return playerSign;
}


Was This Post Helpful? 0
  • +
  • -

#6 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: C++ Loop not comparing values correctly?

Posted 12 May 2014 - 02:52 AM

int checkConvertInput(int &playerSign, string signs[]) You changed this definition. it had int playerSign. Check your prototype at start of your code.

Also, rawInput.compare(signs[0]) returns zero when strings are matching. So compare this with zero in your conditional statements.

Otherwise code looks alright to me.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,315
  • Joined: 25-December 09

Re: C++ Loop not comparing values correctly?

Posted 12 May 2014 - 05:58 AM

Quote

avoid using == for string comparisons.

Why? If you just need to know if two strings are equal then using the operator== is a good solution. If however you need to know if one string is greater, less, or equal to another string then compare() is the way to go.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1