Can someone please check my code?

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;

cout << “Your turn” << player2;
int player2guess;
cin >> player2guess;

int correctguess;
correctguess = 42;

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 :)”;

return 0;

}

How to post code properly

Pete

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.

(deleted)

That does NOT appear to be ARDUINO code!

Why?

Because Arduino code almost never involves cin and cout.

#include <iostream>
#include <stdio.h>

Arduino code is C/C++ code.

With those libraries, it is certainly C/C++ code.

Jacques

Why?

  1. It has a main() function.
  2. It doesn't have setup() and loop() functions.

gfvalvo:

  1. It has a main() function.
  2. It doesn't have setup() and loop() functions.

That makes sense. I didn't see that.

The OP mention of Visual Studio should have rang a bell.

. . . should have rang a bell.

Tis the season . . .

OP states that he/she is practicing. An Arduino forum might not quite be the right place but we can always help as in reply #2.

On the other hand, OP can practice with a real Arduino :wink:

@trently101

If you have access to an Arduino board, please use the IDE so that we can help out here.

To replace cin<< & cout<<, take a look at this reference about Serial.

Cheers

Jacques

trently101:
get atleast decent at it before i try programming the board

Why?

You won't do the Arduino board any harm if your code does not work.

Develop your program little by little and try it out as often as possible.

...R

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.

JustGoFly:
This forum is called "Programming Questions".

Yes, and the implication is that it is (Arduino) "Programming Questions".

Taken too literally and you might assume that "Programming Questions" relates to Python, Basic, Perl, Java, etc.

gfvalvo:

  1. It has a main() function.
  2. It doesn't have setup() and loop() functions.
  1. Programs compiled in the Arduino IDE have a main() function but it is hidden from view.

  2. If you provide your own main() function then setup() and loop() are not required

While that is technically true, and nobody said it wasn't, I think we can all agree that the code in the OP was not written for Arduino. Could you define cout and cin and do that with Arduino? Probably. But does the OP look like he has anything close to the ability to do that? Not a chance.