Cin problems

This is a discussion on Cin problems within the C++ Programming forums, part of the General Programming Boards category; I'm writing a C++ console Tic Tac Toe game where the players enter coordinates to place the X's and the ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    43

    Cin problems

    I'm writing a C++ console Tic Tac Toe game where the players enter coordinates to place the X's and the O's. It should look like that:

    Code:
    Enter coordinates: (x, y)
    Where x and y are input. But by pressing enter it skips a line so the output is messed up.
    Is there a way to use cin so that it doesn't skip the line when pressing enter?

    Here's the code snippet:

    Code:
    	cout << endl;
    	cout << "Enter coordinates: (";
    	cin  >> x;
    	cout << ", ";
    	cin  >> y;
    	cout << ")" << endl;

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    cin doens't skip over commas, so you'll either have to enter the coordinates separated by spaces, or use cin.getline to enter the coordinates, but since getline only accepts strings, you'll need to convert the string to an int.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There are non-standard functions that will do what you want. You could also allow the users to type the comma themselves and just read it into a dummy char variable, although that would make error checking even harder. You could also use non-standard console functions to move the cursor back to where it was after they hit enter. It just depends on how standard you want your code to be versus how much you want the UI to match your vision.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, I misread your post. It depends on what system and compiler you're using. Windows probably has some console functions to do this. Adrianx has some console tutorials, but I'm not sure what is the link. Also if you have it, the function getch will do what you want. See if you have the <conio.h> header.
    Last edited by swoopy; 09-15-2006 at 11:43 AM.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Since reading console input isn't the centerpiece of your game, you could settle for something like:

    Code:
    int x, y;
    cout << "Enter x coordinate: ";
    cin >> x;
    cout << "Enter y coordinate: ";
    cin >> y;
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Alright thanks, I decided to keep it simple. I don't want to bother converting strings to ints for now.

    I have another question related to cin though. Why isn't this code...

    Code:
    	string myString;
    
    	cin >> myString;
    ...working in Visual C++? It worked fine in Dev-C++. Here's the error I get:

    Code:
    Error	1	error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	c:\documents and settings\dominic\desktop\tictactoe\main.cpp	123

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >binary '>>' : no operator found which takes a right-hand operand of type 'std::string'
    Did you include <string> and <iostream>?
    Code:
    #include <iostream>
    #include <string>

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Ah, I was using <string.h>, but using <string> instead fixed it.

    Thanks for the help.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I was using #include <string.h>, but <string> fixed it.
    string.h contains functions for manipulating C style strings (char arrays). Stuff like strcpy, strcat, strlen.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    >I was using #include <string.h>, but <string> fixed it.
    string.h contains functions for manipulating C style strings (char arrays). Stuff like strcpy, strcat, strlen.
    The equivalent header in C++ is <cstring>. (All the headers in C are included in C++, but their names are changed; the .h is dropped and a leading c is appended.) <string> is a completely different header file, for the C++ string class.

    You could use two cin statements, and then the user just has to put whitespace between the coordinates, a space or a newline or whatever.
    Code:
    cout << "Enter the coordinates: ";
    cin >> x >> y;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >(All the headers in C are included in C++, but their names are changed; the .h is dropped and a leading c is appended.)
    Actually string.h is still available to use in C++. But it's recommended to use <cstring>.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually string.h is still available to use in C++.
    Not as part of the standard. Most compilers have it, of course, since C programs use it; but it's not part of the C++ standard.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by dwks
    but it's not part of the C++ standard.
    Hm? I found it in my copy.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Not as part of the standard. Most compilers have it, of course, since C programs use it; but it's not part of the C++ standard.
    It's deprecated, but still present.
    My best code is written with the delete key.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    ... like many other C libraries, no?
    Aren't they needed anyways for embed C?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. cin and getline() problems....
    By AdampqmabA in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2008, 05:55 PM
  3. Problems with cin and GetAsyncKeyState
    By KgNe in forum C++ Programming
    Replies: 32
    Last Post: 08-21-2008, 10:00 AM
  4. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  5. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21