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.
#include <iostream>
usingnamespace 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;
}
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.
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.