Please help with cin!

Hello I'm new to programming. I'm trying to tell the user to use proper notation but it keeps saying i forgot to use the left and right parentheses even though i do use them, the comma one works perfectly fine though. No compilation errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>

  using namespace std;

  int main()

  {
      double x1, x2, y1, y2, dist, mid;
      char lftparen, rtparen, comma;
      
         cout<<\t\tWelcome to the 2D Point Program!!!\n"
               "\nWhere is your first point? ";

         cin>>lftparen>>x1>>comma>>y1>>rtparen;
         
             if(lftparen !='(')
                 {
                    cout<<"\nYou were missing the open parenthesis before the "
                            "x coordinate!\n";
                 }
             if(comma !=',')
                 {
                    cout<<"You were missing the comma to separate "
                           "coordinates!\n";
                 }
             if(rtparen !=')')
                 {
                    cout<<"You were missing the close parenthesis after the "
                            "y coordinate!\n";
                 }
return 0;

  }                
Last edited on
Works for me (although you're missing your opening double quote on line 11).

Or at least it works if you enter the point using the proper notation.
If you omit the right parenthesis, it'll stop to wait for more input (it'll get stuck on the cin >> rtparen; part), and if you omit the left parenthesis, it'll eat the first digit of the x-coordinate instead.

I'd recommend cin.peek() to peek at the next character without actually reading it so you can tell whether a left or right parenthesis is missing.
EDIT: Or cin.get(). If you read the first character and it's not a left parenthesis, you know it's missing, and if you read off a character after the y-coordinate and it's not a right parenthesis (if there's nothing after the y-coordinate, it'll eat the newline character ('\n') from you pressing Enter), then you know the right parenthesis is missing.
Last edited on
how do i use cin.get()/ cin.peek() to fix the problem where the program "eats" the x-coordinate and doesn't get stuck after the y-coordinate. I haven't learned those functions yet and don't know how to use them. I've looked around google a little bit and I still haven't been able to solve the problem yet.
Last edited on
Topic archived. No new replies allowed.