So im a C++ newbie. I want to get atleast decent at it before i try programming the board. Im using visual studio 2017. The code is supposed to execute a " Guess the number" game, where 2 people can play. The problem im having is, when opened, it starts as it should, but then it closes after the first user input. Like i said. C++ newbie. All help is appreciated.
//Guess The Number #include #include <stdio.h> #include
using namespace std;
int main()
{
cout << "Enter Your Name Player 1";
int player1;
cin >> player1;
cout << "Enter Your Name Player 2";
int player2;
cin >> player2;
cout << player1 << "Guess a number between 1 and 100";
int player1guess;
cin >> player1guess;
The compiler will help you find your mistakes, but some issues for your to ponder:
if (int player1guess = correctguess)
cout << "'player1' wins!";
else if (int player2guess = correctguess)
cout << "'player2' wins!";
else if (player1guess , player2guess = correctguess)
cout << "Its a tie";
else if (player1guess != correctguess , player2guess != correctguess)
cout << "You both lose :)";
line 1: if (int player1guess = correctguess)
You already initialized player1guess as int, don't do it again
a comparison should use ==
if (player1guess == correctguess)
and here:
else if (player1guess , player2guess = correctguess)
if player1guess is correct - you'll never get here, same goes for player2. So this will never execute.
And it's not proper C, it would not compiler. If you want to do this check - do it first.
if ((player1guess== correctguess) && (player2guess==correctguess))
cout << "it's a tie";
else
if ((player1guess!= correctguess) && (player2guess!=correctguess))
cout << "You both lose";
Keep at it, everyone starts at the same place. The main rule for programmers, ABC - Always Be Coding.
You get rusty quickly if you stop, and the learning never ends.
One trick to keep from falling for the '=' in comparison is to put the constant first:
if (42 = player1guess) would not compiler, but
if (player1guess = 42) would, although modern compilers would warn that you're probably doing something wrong here.
He says right in his Original comment that he's using Visual Studio 2017. Doesn't it make sense for him to write this as a console app and learn what he needs to before progressing to an Arduino, or does he have to come in here after he learns to program? This forum is called "Programming Questions".
VS has an excellent debugger. I would recommend using that before downloading to an arduino. And the Arduino does has a plug in for VS. So he's not at all out of line here.