Input coordinates

This is a discussion on Input coordinates within the C++ Programming forums, part of the General Programming Boards category; I have just started programming, and am writing a program to find the slope of a line, among other things. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    Question Input coordinates

    I have just started programming, and am writing a program to find the slope of a line, among other things. The code itself works fine, but it would be even better if I could put in numbers in the form (x,y). When I type cout<<"(";, then cin>>x;, I see ((my value for x. But when I go to the next line to put cout<<",";, the cursor goes to another line. Is there a way to prevent that?

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    I believe cin.ignore() will fix that. Put it after the cin>> statement.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    cin.ignore() will not fix that. It is because the user has to hit enter before you can process the entered value.

    There are no standard solutions, so it depends on your OS and compiler. You can get the character typed immediately without waiting for <enter> (possibly with getch()) or you can use console programming to go back to the appropriate position in the console.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    I have getch() on my compiler, but could you please tell me how to actually use it? I am an amateur, and I would really like an expert's help.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I am no expert in getch(), but I'm sure there are plenty of examples if you search. Basically, you have a char variable and you set it equal to the result of getch() when you want the user to enter data. You then have to convert that character to an int. As long as the user is only allowed one digit, it will work, but if the user can type multiple digit numbers than it might be a bit harder (how do you know when they are done typing the number?).

    Your best solution is probably to give up on the idea of the user input going directly into your parentheses. It will be quite complicated unless you only allow a single digit.

    You might look at adrianxw's console tutorial, there may be a way to erase the newline from when the user hits enter. If you were able to do that, you wouldn't have to use getch(), you could just let the user type the number and hit enter, then you would "erase" the newline and go back to the proper place to put the comma. That is something to look into.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Sure, whenever you want input, you set getch() = to the variable. This will wait for a character to be pressed and place it into the variable, then echo the variable onto the screen. Just remember that this is a character not an integer. You can use atoi or something to fix that though.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <conio.h>
    
    ...
    
    char keyPressX;
    char keyPressY;
    
    cout << "Please enter your coordinates." << endl;
    keyPressX = getch();
    
    cout << "(" << keyPressX << ",";
    keyPressY = getch();
    cout << keyPressY << ")" << endl;
    
    keyPressX = atoi(&keyPressX);
    keyPressY = atoi(&keyPressY);
    
    ...
    Last edited by SlyMaelstrom; 11-10-2005 at 02:50 PM.
    Sent from my iPadŽ

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Yes, fairly easy. Just create a garbage char to handle the coma and string your inputs together on one line (that is, up to the , then tell the user to input everything on one line in the form "#,#".

    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()
                             
    {
                              
      int x,y;
      char garbage;
    
    
      cout << "Enter X,Y cord seperated by coma followed by enter: ";
    
      cin >> x >> garbage >> y;
    
      cout << "X: " << x << " Y: " << y;
    
      cin.ignore();
      cin.get();
    
      
          return 0;
    
    
    }
    Or if you want the user to input the "(" and ")"
    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()
                             
    {
                              
      int x,y;
      char garbage;
    
    
      cout << "Enter (X,Y) cord seperated by coma followed by enter: ";
    
      cin >> garbage >> x >> garbage >> y >> garbage;
    
      cout << "X: " << x << " Y: " << y;
    
      cin.ignore();
      cin.get();
    
      
          return 0;
    
    
    }
    *edit*
    I think I misunderstood what you where asking...
    Last edited by Enahs; 11-10-2005 at 03:04 PM.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Thank you very much for your answers, they were very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM

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