Thread: New Guy needs help to enter and print x and y on one line

  1. #1
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61

    New Guy needs help to enter and print x and y on one line

    I'm trying to enter an 'x' and 'y' coordinate on only one line separated by a comma. But I keep getting a syntax error. Here are the lines of code I'm using. This has to be simple. What am I doing wrong with this code?

    Code:
    cout<< "Please enter the x and the y coordinates of the first point,"<<endl;
    cout<< "use a comma to separate them. " <<endl<<endl;
    cin>> "You entered: " >>x1>>",">> y1 >>"for the first point" >>endl;

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    The cin stream is used to retrieve input from the user not display messages. The cout stream is used to display the messages.


    Jim

  3. #3
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Thanks Jim, Senor moment. Ok cin>>x1; cin>>y1 works. So the space-bar works, but is there a way to do multiple inputs using a comma in C++?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    What do you mean by "using a comma"? If you mean you want the user to enter a comma to separate the entries then yes it is possible.

    Code:
    int number1, number2;
    char comma;
    
    cin << number1 << comma << number2;
    You could also use a string for the entire entry, the use a stringstream to process that string.

    Jim

  5. #5
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Thanks Jim
    That's exactly what I needed to know. Could the comma also be used as a reference something like this char comma = &, (ampersand comma)? So it can be used (,) as a substitute for the word comma??

    I have another question. I trying to work on a number line with x and y coordinates the x and y coordinates run positive to negative. (or reversed) I what to be able to divide the line into any number of points on that line but since I'm new to C++, I'm not sure of how to do this. I've already figured out how to divide the line in two. But that was a simple divide by 2. Can you give me some hints as to what to use to accomplish this. I'm thing of a 2D array with two for loops

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    A char can only hold one character not multiple characters. In the snippet I provided comma is a variable, it could be called anything that is allowed by the language. And also note what that one character actually is doesn't really matter (as long as it isn't a digit).

    Jim

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by jimblumberg View Post
    And also note what that one character actually is doesn't really matter (as long as it isn't a digit).
    The digits have character equivalents ('0', '1', '2'...etc) so that doesn't matter, either.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    In this case it does, because if you look at the code I'm using the extraction operator to retrieve a numeric variable separated by a delimiter, this delimiter can't be a digit.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-06-2013, 08:57 AM
  2. how to open files and print line by line in shell
    By omega666 in forum Linux Programming
    Replies: 4
    Last Post: 04-15-2011, 04:54 PM
  3. it won't print the 0 if i enter 102
    By maybabier in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 02:13 AM
  4. print astrik when enter password
    By ck12 in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2001, 10:16 PM