Rorix Rorix - 3 months ago 15
C++ Question

Error with second half of my conditional statements

Hi all I am trying to make a rock, paper, scissors game.

My goal is to have the game run until someone reaches 5 wins, but I haven't inputted that code, because I am having problems with my conditional statements.

Here is my code

#include <iostream>
#include <ctime>
#include <iomanip>
#include <cmath>

using namespace std;



int main() {

int rand1, userChoice, compInput;
int userWin, compWin;

srand(time(0));

rand1 = (1 + rand() % 3);

cout << "Lets play a game of Rock-Paper-Scissors" << endl;
cout << "Choose \n 1 for Rock \n 2 for Paper \n 3 for Scissors" << endl;
cin >> userChoice;

while (userChoice < 1 || userChoice > 3) {
cout << "Please enter a valid selection between 1-3.\n";
cin >> userChoice;
}

compInput = rand1;

if ((userChoice == 1 && compInput == 3) || (userChoice == 2 && compInput == 1) ||
(userChoice == 3 && compInput == 2)) {
cout << "You win!" << userChoice << compInput << endl;
userWin++;
}


if ((userChoice == 1 && compInput == 2) || (userChoice == 2 && compInput == 3) ||
(userChoice == 3 && compInput == 1)) {
cout << "You lose!" << cout << userChoice << compInput << endl;
compWin++;
}



return 0;
}


I am getting errors if I run the code with the second "if" statement, however if I put the second if statement in comments the code runs just fine.

I am not sure what I am doing wrong here. In my first if statement I am trying to check if the user wins, if so increment useWin by 1.

In my second if statement I am try to check if the user loses, and if that is the case increment compWin by 1.

As you can tell I am new to C++ and this is not homework, just practice for me. Thank you for your help!

EDIT: This is the errors I am receiving

**invalid operands to binary expression ('basic_ostream<char, std::__1::char_traits<char> >' and 'ostream' (aka 'basic_ostream<char>'))
cout << "You lose!" << cout << userChoice << compInput << endl;
~~~~~~~~~~~~~~~~~~~ ^ ~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:207:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'const void *' for 1st argument; take the address of the argument with &
basic_ostream& operator<<(const void* __p);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:191:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'std::__1::basic_ostream<char> &(*)(std::__1::basic_ostream<char> &)' for 1st argument
basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:192:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'basic_ios<char_type, traits_type> &(*)(basic_ios<char_type, traits_type> &)' for 1st argument
basic_ostream& operator<<(basic_ios<char_type, traits_type>&
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:194:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'std::__1::ios_base &(*)(std::__1::ios_base &)' for 1st argument
basic_ostream& operator<<(ios_base& (*__pf)(ios_base&));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:195:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'bool' for 1st argument
basic_ostream& operator<<(bool __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:196:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'short' for 1st argument
basic_ostream& operator<<(short __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:197:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'unsigned short' for 1st argument
basic_ostream& operator<<(unsigned short __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:198:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'int' for 1st argument
basic_ostream& operator<<(int __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:199:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'unsigned int' for 1st argument
basic_ostream& operator<<(unsigned int __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:200:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'long' for 1st argument
basic_ostream& operator<<(long __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:201:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'unsigned long' for 1st argument
basic_ostream& operator<<(unsigned long __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:202:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'long long' for 1st argument
basic_ostream& operator<<(long long __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:203:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'unsigned long long' for 1st argument
basic_ostream& operator<<(unsigned long long __n);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:204:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'float' for 1st argument
basic_ostream& operator<<(float __f);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:205:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'double' for 1st argument
basic_ostream& operator<<(double __f);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:206:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'long double' for 1st argument
basic_ostream& operator<<(long double __f);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:208:20: note: candidate function not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'basic_streambuf<char_type, traits_type> *' for 1st argument
basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:785:1: note: candidate function [with _CharT = char, _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'char' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:818:1: note: candidate function [with _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, char __c)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:825:1: note: candidate function [with _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'signed char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:832:1: note: candidate function [with _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'unsigned char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:846:1: note: candidate function [with _CharT = char, _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'const char *' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:892:1: note: candidate function [with _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'const char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:899:1: note: candidate function [with _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'const signed char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:907:1: note: candidate function [with _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'const unsigned char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1086:1: note: candidate function [with _CharT = char, _Traits = std::__1::char_traits<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'const std::__1::error_code' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:778:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::__1::basic_ostream<char>')
operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iomanip:361:1: note: candidate template ignored: could not match '__iom_t8' against 'basic_ostream'
operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iomanip:476:5: note: candidate template ignored: could not match '__iom_t10' against 'basic_ostream'
operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1063:5: note: candidate template ignored: disabled by 'enable_if' [with _Stream = std::__1::basic_ostream<char> &, _Tp = std::__1::basic_ostream<char>]
!is_lvalue_reference<_Stream>::value &&
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:839:1: note: candidate template ignored: could not match 'const _CharT *' against 'ostream' (aka 'basic_ostream<char>')
operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1077:1: note: candidate template ignored: could not match 'basic_string' against 'basic_ostream'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1094:1: note: candidate template ignored: could not match 'shared_ptr' against 'basic_ostream'
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1101:1: note: candidate template ignored: could not match 'bitset' against 'basic_ostream'
operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
^
1 error generated.
make[3]: *** [CMakeFiles/randomPractice.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/randomPractice.dir/all] Error 2
make[1]: *** [CMakeFiles/randomPractice.dir/rule] Error 2
make: *** [randomPractice] Error 2**

Answer

In your second if statement, you have this line:

cout << "You lose!" << cout << userChoice << compInput << endl;

You typed cout twice on that line. You should remove the second cout, like this:

cout << "You lose!" << userChoice << compInput << endl;

I believe this is what's causing your syntax error.

Comments