i need help
Im trying to make a tic tac toe game but im having a problem.
Im making it so each player enters his name at the begining.
But only one of my cin.getlines work (the second one). it just compleatly skips the first player name place when i compile it.
Heres my code (im not very far iv been bizzy). Hope sumone can help.
#include <iostream.h>
#include <stdlib.h>
int intgrid [3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
void drawgrid ();
char xo;
char userid1 [20];
char userid2 [20];
void playerplayer ()
{
cout << "\nPlese enter your names";
cout << "\nPlayer 1: ";
cin.getline (userid1, 20);
cout << "\nPlayer 2: ";
cin.getline (userid2, 20);
drawgrid ();
}
void drawgrid ()//draw out grid
{
cout << endl << ''_'' << intgrid [0][0] << ''_'' << ''|'' << ''_'' << intgrid [0][1] << ''_'' << ''|'' << ''_'' << intgrid [0][2] << ''_'' << endl;
cout << ''_'' << intgrid [1][0] << ''_'' << ''|'' << ''_'' << intgrid [1][1] << ''_'' << ''|'' << ''_'' << intgrid [1][2] << ''_'' << endl;
cout << " " << intgrid [2][0] << " " << ''|'' << " " << intgrid [2][1] << " " << ''|'' << " " << intgrid [2][2] << " " << endl;
system ("PAUSE");
}
--------------------------http://www.gamedev.net/community/forums/icons/icon51.gif ... Hammer time
Try putting this after the first getline
The problem with getline is when you press the enter key, the ''\n'' is not placed into the string, but it''s still in the buffer, so when you get to the next one, it reads the ''\n'' before you type anything, thinks you hit enter, and moves on.
If this does not fis it, let me know.
I think, therfore I am.
I think?
cin.ignore(20,''\n'');
The problem with getline is when you press the enter key, the ''\n'' is not placed into the string, but it''s still in the buffer, so when you get to the next one, it reads the ''\n'' before you type anything, thinks you hit enter, and moves on.
If this does not fis it, let me know.
I think, therfore I am.
I think?
I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming
My Twitter: https://twitter.com/indieprogram
My Book: http://amzn.com/1305076532
quote: Original post by MTT
#include <iostream.h>
#include <stdlib.h>
What resources are you using to learn C++? iostream.h is a non-standard header, you should use iostream instead. You need to be aware that the contents of standard headers such as iostream are contained within a "namespace" called "std". That means you have to qualify your use of things within the namespace so that the compiler knows where to look for those things. For example, if you change to use "iostream", you can then code "using namespace std;" after the #include''s to tell the compiler to dump the contents of namespace std into the global namespace. If that is at all confusing, I suggest you pick up a decent modern C++ textbook.
quote:
char userid1 [20];
char userid2 [20];
Again, a decent learning resource will teach you to prefer std::string over char arrays.
Thanx Glass knive it worked perfectly, although im not to sure what i did.
and for saberman. I am using the tutorial on cplusplus.com which i think is pretty modern. I know he introduses namespaces later on in the tutorial, i dont know why it comes later but im sure he has his reasons. It was recomended to me by a few people and it seems to be working fine for me. I am not sure what std::string is but im sure il learn later on in the tutorial. Here is the outline of the tutorial. And as you can see i still have alot of things to learn.
1. Basics of C++.
Structure of a C++ program.
Variables. Data types. Constants.
Operators.
Communication through console.
2. Control structures and Functions.
Control Structures.
Functions (I).
Functions (II).
3. Advanced Data.
Arrays.
Strings of Characters.
Pointers.
Dynamic Memory.
Structures.
User defined data types. (typedef, union, enum)
I AM HERE
4. Object-oriented Programming.
Classes. Constructors and Destructors. Pointers to classes.
Overloading Operators. this. Static members.
Relationships between classes: friend. Inheritance.
Virtual Members. Abstraction. Polymorphism.
5. Advanced concepts.
Templates.
Namespaces.
Exception handling.
Advanced classes type-casting. (new cast and typeid operators)
Preprocessor directives.
6. C++ Standard Library.
Input/Output with files.
and for saberman. I am using the tutorial on cplusplus.com which i think is pretty modern. I know he introduses namespaces later on in the tutorial, i dont know why it comes later but im sure he has his reasons. It was recomended to me by a few people and it seems to be working fine for me. I am not sure what std::string is but im sure il learn later on in the tutorial. Here is the outline of the tutorial. And as you can see i still have alot of things to learn.
1. Basics of C++.
Structure of a C++ program.
Variables. Data types. Constants.
Operators.
Communication through console.
2. Control structures and Functions.
Control Structures.
Functions (I).
Functions (II).
3. Advanced Data.
Arrays.
Strings of Characters.
Pointers.
Dynamic Memory.
Structures.
User defined data types. (typedef, union, enum)
I AM HERE
4. Object-oriented Programming.
Classes. Constructors and Destructors. Pointers to classes.
Overloading Operators. this. Static members.
Relationships between classes: friend. Inheritance.
Virtual Members. Abstraction. Polymorphism.
5. Advanced concepts.
Templates.
Namespaces.
Exception handling.
Advanced classes type-casting. (new cast and typeid operators)
Preprocessor directives.
6. C++ Standard Library.
Input/Output with files.
--------------------------http://www.gamedev.net/community/forums/icons/icon51.gif ... Hammer time
Uh oh, running into the same problem in a different place. When it has to show userid1 (cout << userid1 << " goes first") it just skips userid1 when i compile ("goes first: ") but userid2 works ("matt goes first: ")
can sombody help, preferably so i dont hav to deal with every time it dispalys userid1.
can sombody help, preferably so i dont hav to deal with every time it dispalys userid1.
--------------------------http://www.gamedev.net/community/forums/icons/icon51.gif ... Hammer time
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement