Help! noob trouble...

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Duffles22
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Tue Aug 10, 2010 6:18 pm

Help! noob trouble...

Post by Duffles22 »

Hi, just started reading these forums and i'm a complete noobie when it comes to C++. I started this topic because i am trying to create a tic-tac-toe game using C++ and i'm having a bit of trouble using cin >>. Here is my Code:
(NOTE: don't judge my codding to hard, like i said before i'm a complete noobie.)

#include<iostream>
#include"windows.h"
char A = '1', B = '2', C = '3', D = '4', E = '5', F = '6', G = '7', H = '8', I = '9';
int Fcmove = 0;
int Fpmove = 0;
char Win;
char Pcoin;
int Ccoin;
int Board()
{
system("CLS");
std::cout << "|---------|---------|---------|\n"
<< "| | | |\n"
<< "| " << A << " | " << B << " | " << C << " |\n"
<< "| | | |\n"
<< "|---------|---------|---------|\n"
<< "| | | |\n"
<< "| " << D << " | " << E << " | " << F << " |\n"
<< "| | | |\n"
<< "|---------|---------|---------|\n"
<< "| | | |\n"
<< "| " << G << " | " << H << " | " << I << " |\n"
<< "| | | |\n"
<< "|---------|---------|---------|\n\n";}
int main()
{
std::cout << "Tic-Tac-Toe Game\n"
<< "-----------------\n";
system("PAUSE");
system("CLS");
std::cout << "We will now flip a coin to see who will go first. Heads or Tails?\n";
std::cin >> Pcoin;
srand ( time(NULL) );
Ccoin = rand() % 2 + 1;
system("CLS");
std::cout << "Flipping Coin...\n";
Sleep(1000);

if(Pcoin=='H' && Ccoin==1 | Pcoin=='h' && Ccoin==1)
{
system("CLS");
std::cout << " ___ \n"
<< " (_H_) \n"
<< "You Won! you get to go first.\n";
system("PAUSE");
Win = 'y';}

if(Pcoin=='H' && Ccoin==2 | Pcoin=='h' && Ccoin==2)
{
system("CLS");
std::cout << " ___ \n"
<< " (_T_) \n"
<< "You Lost! The Computer gets to go first.\n";
system("PAUSE");
Win = 'n';}

if(Pcoin=='T' && Ccoin==1 | Pcoin=='t' && Ccoin==1)
{
system("CLS");
std::cout << " ___ \n"
<< " (_H_) \n"
<< "You Lost! The Computer gets to go first.\n";
system("PAUSE");
Win = 'n';}

if(Pcoin=='T' && Ccoin==2 | Pcoin=='T' && Ccoin==2)
{
system("CLS");
std::cout << " ___ \n"
<< " (_T_) \n"
<< "You Won! you get to go first.\n";
system("PAUSE");
Win = 'y';}

if(Win=='y'){
Board();
std::cout << "Players Move:";
std::cin >> Fpmove;
switch(Fpmove){
case 1:{A = 'X';}break;
case 2:{B = 'X';}break;}}

if(Win=='n'){
Board();
std::cout << "Computer is thinking...";}

}

The piece of code i'm having trouble with has been bolded. the trouble is that even though i put it in there to get the input of the player, the program skips over it and doesn't wait for the player to type in which move the player is doing, i've tried using std::cin.ignore(); to solve the problem but with no success. Please help!
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Help! noob trouble...

Post by Ginto8 »

you say you've used cin.ignore()... how did you do it? because what you should probably do is cin.ignore([some high number, maybe the length of supercalafragalisticexpialidocious],'\n'). The newline from the previous cin >> has carried over and remains in stdin's buffer. You need to clear that out before you can take more user input.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Duffles22
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Tue Aug 10, 2010 6:18 pm

Re: Help! noob trouble...

Post by Duffles22 »

Alright cool, i just tried to put in std::cin.ignore(255,"\n") and a error came up, telling me: "Invalid conversion from 'const char*' to 'int'".
since i've never really used cin.ignore (correctly anyways) could you post what you would put in Exactly?

EDIT: i was fouling around with it and i got it to work. you were right i needed the code: std::cin.ignore(big Number,'\n'). i was putting " instead of ' before. whhooops. anyways thanks for the help! =D
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Help! noob trouble...

Post by Ginto8 »

Duffles22 wrote:Alright cool, i just tried to put in std::cin.ignore(255,"\n") and a error came up, telling me: "Invalid conversion from 'const char*' to 'int'".
since i've never really used cin.ignore (correctly anyways) could you post what you would put in Exactly?

EDIT: i was fouling around with it and i got it to work. you were right i needed the code: std::cin.ignore(big Number,'\n'). i was putting " instead of ' before. whhooops. anyways thanks for the help! =D
yeah double quotes are strings (well, const char*'s, but that's just details). Individual characters are surrounded in single quotes.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply