Type in midpoint with or without braces

How could I type type the midpoint with or without parenthesis, It works when I type with parenthesis (6,7) but when I don't use a parenthesis 6,7 it doesnt work.

#include <iostream>
#include <limits>


using namespace std;

double midpoint (double x1, double x2);

int main()
{
double X1,X2,Y1,Y2, midpoint1, midpoint2;
char t, yes_no;
cout << "Welcome to the mid-point program!\n";
cout << "Would you like to find a midpoint?\n";
cin >> yes_no;
cin.ignore(numeric_limits<streamsize> ::max(),'\n');
while (toupper (yes_no)=='Y')
{
cout << "please enter the value of your 1st end point\n";
cin >> t >> X1 >> t >> Y1 >> t;
cout << "please enter the value of your 2nd midpoint\n";
cin >> t >> X2 >> t >> Y2 >> t;
midpoint1 = midpoint(X1, X2);
midpoint2 = midpoint(Y1, Y2);
cout << "the midpoint of the line is (" << midpoint1 << ", " <<midpoint2
<< ")." << endl << endl;
cout << "Do you want to find another midpoint?\n";
cin >> yes_no;
cin.ignore(numeric_limits<streamsize> ::max(),'\n');
}
cout << "Have a great day!" << endl;


return 0;
}
double midpoint(double x1,double x2)
{
double mid_point;

mid_point = (x1+x2)/2;
return mid_point;
}
Currently, your code can only accept input in the form of
cin >> t >> X1 >> t >> Y1 >> t;
char double char double char

If you want to be able to accept different kinds of input, you will have to accept the entire line of input as a string and then examine it yourself in code to work out what the numbers you care about are.
Could you show me an example of how the string would look.
Sorry I am a beginner so I am a bit confused.
1
2
3
string user_input;
cout << ""please enter the value of your 1st end point\n";
getline(cin, user_input); 


This stores the user input into a single string.

Then you look at the string and figure out what format they entered the values.
Show all us all possible formats that you want the user to be able to enter the two numbers as, with examples.

Or is "(6,7)" and "6,7" the only two formats you wish to accept?

Edit: (This does allow spacing between the comma -- use cin >> instead of getline if you don't want to allow spacing.)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Example program
#include <iostream>
#include <string>
#include <sstream>

int main()
{ 
    std::cout << "Enter x,y pair: ";
    std::string input;
    std::getline(std::cin, input);

    // correct input == "x,y" or "(x,y)"
    
    bool success = false;
    
    int x;
    int y;
    
    // attempt to find '('
    std::size_t opening_found = input.find("(");
    if (opening_found != std::string::npos)
    {
        // should contain a ( and )
        size_t closing_found = input.find(")");
        if (closing_found != std::string::npos
            && opening_found < closing_found)
        {
            // attempt to split into tokens: '(' 'x' ',' 'y' ')'
            // if any part of this fails, make the whole input fail
            
            char left_paren;
            char comma;
            char right_paren;
            std::istringstream iss(input);
            if (iss >> left_paren >> x >> comma >> y >> right_paren)
            {
                // success 
                success = true;
            }
            else
            {
                success = false;  
            }
        }
        else
        {
            // fail
            success = false;
        }
    }
    else
    {
        // should not contain ( or )
        size_t closing_found = input.find(")");
        if (closing_found == std::string::npos)
        {
            char comma;
            
            std::istringstream iss(input);
            if (iss >> x >> comma >> y)
            {
                // success 
                success = true;
            }
            else
            {
                success = false;  
            }
        }
        else
        {
            // ')' was found --> fail   
            success = false;
        }
    }
    
    if (success)
    {
        std::cout << "Success: " << x << " " << y << std::endl;   
    }
    else
    {
        std::cout << "Failure" << std::endl;   
    }
}


Probably can be simplified :D



Enter x,y pair: 4,3
Success: 4 3

Enter x,y pair: (5,2)
Success: 5 2

Enter x,y pair: 5,  2
Success: 5 2

Enter x,y pair: 5  ,2
Success: 5 2

Enter x,y pair: ()2,3
Failure

Enter x,y pair: )2,3(
Failure

Enter x,y pair: (2,)3
Failure 

Enter x,y pair: 4,3)
Failure



Edit 2: Simpler and doesn't succeed at using symbols other than ( , ) in correct positions

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Example program
#include <iostream>
#include <string>
#include <sstream>
 
int main()
{ 
    std::cout << "Enter x,y pair: ";
    std::string input;
    std::getline(std::cin, input);
 
    // assume: input == "x,y" or "(x,y)"
 
    bool success = false;
 
    int x;
    int y;
    char left_paren;
    char comma;
    char right_paren;
    std::istringstream iss(input);
    if (iss >> left_paren >> x >> comma >> y >> right_paren)
    {
        if (left_paren == '(' && comma == ',' && right_paren == ')')
            success = true;
        else
            success = false;
    }
    else
    {
        std::istringstream iss_attempt2(input);
        if (iss_attempt2 >> x >> comma >> y)
        {
            if (comma == ',')
                success = true;
            else
                success = false;
        }
        else
        {
            success = false;   
        }
    }
 
    if (success)
    {
        std::cout << "Success: " << x << " " << y << std::endl;   
    }
    else
    {
        std::cout << "Failure" << std::endl;   
    }
}

untested
Last edited on
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
34
35
36
37
38
39
40
41
#include <iostream>
#include <utility>

// return true if the next character extracted and discarded is the expected character
bool got_expected( std::istream& stm, char expected )
{
    char c = 0 ;
    return stm >> c && c == expected ;
}

// extract and discard the next character if it is to_be_discarded
// return true if to_be_discarded was extracted and discarded
bool ignore_if( std::istream& stm, char to_be_discarded )
{
    char c = 0 ;

    // read the next char, discard it if it is to_be_discarded, otherwise put it back
    if( stm >> c && c != to_be_discarded ) stm.putback(c) ;

    return c == to_be_discarded ;
}

// accepts input of the following forms: (x,y) or x,y or (x y) or x y
// the input format is free: extra white spaces may be present anywhere
std::istream& get_coordinates( std::istream& stm, std::pair<double,double>& coords )
{
    // true if an open parenthesis at the beginning was found and discarded
    const bool open_paren = ignore_if( stm, '(' ) ;

    // try to read the two floating point values separated by either a comma or white space
    stm >> coords.first ; // read the first number
    ignore_if( stm, ',' ) ; // ignore a comma, if it is present
    if( stm >> coords.second ) // if a second number was also successfully read
    {
        // if there was an open parenthesis, there must be a close parenthesis at the end
        // if not, place the stream into a failed state
        if( open_paren && !got_expected( stm, ')' ) ) stm.clear( stm.failbit ) ;
    }

    return stm ;
}

http://coliru.stacked-crooked.com/a/8aaaf52269440833
Topic archived. No new replies allowed.