Distance Calc Program not working correctly

The program below works, but not the way I wanted. I want the program to allow the user to enter in values with or without parentheses and/or commas. It doesn't seem to be working correctly. How can I fix this?!?!?!? Also, what can I do so (using the peek() function) so that the program ignores or automatically puts in a comma.



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
#include<climits>
#include<cmath>
#include<cctype>
#include<cstdlib>

using namespace std;

int main(void)

{

    
      cout<<"\n\t\tWelcome to the 2D Point Program\n";
        
          double dist_x1,
                 dist_y1,
                 dist_x2,
                 dist_y2,
                 dist,
                 xmid,
                 ymid;
          char t;
            
      cout<<"\n\t\tPlease enter your first point.\n";
      
           cin >>ws;
           if (  cin.peek() == '(' )
           {
               cin >> t;
           }  
           else if ( ! isdigit( cin.peek() ) &&
                      cin.peek() != '+' && cin.peek() != '-'  )
           {
               cout << "invalid";
               cin >> t;
           }
           else
           {
               cout << "You are missing the open parenthesis before x ";
           }
           cin >> dist_x1 >> ws;
           
           
           
                      cin >>ws;
           if (  cin.peek() == ')' )
           {
               cin >> t;
           }  
           else if ( ! isdigit( cin.peek() ) &&
                      cin.peek() != '+' && cin.peek() != '-'  )
           {
               cout << "invalid";
               cin >> t;
           }
           else
           {
               cout << "You are missing the close parenthesis after y ";
           }
           cin >> dist_y1 >> ws;
           
           
      cout<<"\n\t\tPlease enter your second point.\n";
           
                      cin >>ws;
           if (  cin.peek() == '(' )
           {
               cin >> t;
           }  
           else if ( ! isdigit( cin.peek() ) &&
                      cin.peek() != '+' && cin.peek() != '-'  )
           {
               cout << "invalid";
               cin >> t;
           }
           else
           {
               cout << "You are missing the open parenthesis before x ";
           }
           cin >> dist_x2 >> ws;
           
           
           
                      cin >>ws;
           if (  cin.peek() == ')' )
           {
               cin >> t;
           }  
           else if ( ! isdigit( cin.peek() ) &&
                      cin.peek() != '+' && cin.peek() != '-'  )
           {
               cout << "invalid";
               cin >> t;
           }
           else
           {
               cout << "You are missing the close parenthesis after y ";
           }
           cin >> dist_y2 >> ws; 
           
      cout<<"\n\t\tThank you!  Calculating... Done.\n";
      
      
      dist = sqrt(pow((dist_x2 - dist_x1),2) + pow((dist_y2 - dist_y1),2));  
      
      cout << "(" << dist_x1 << ", " << dist_y1 << ")" << " is " 
          << dist << " units " << "away " << "from " << "(" 
          << dist_x2 << ", " << dist_y2 << ")" << "." << "\n";
      
       
      
      xmid = (dist_x1 + dist_x2)/2;
      ymid = (dist_y1 + dist_y2)/2;
      
      cout << "The midpoint of the line segment between " << "(" << dist_x1 << ","
          << dist_y1 << ")" << " and " << "(" << dist_x2 << ", " <<
          dist_y2 << ")" << " is " << "(" << xmid << ", " << ymid << ")"
          << "." << "\n"; 
      

 return 0;
}
    
Last edited on
BUMPING! STILL NEED HALP
First learn to modularize your code. It makes it easier for you to debug, and it also makes it easier for us to debug.

Modularizing means taking all of the unrelated functions and making an external function for each of them.

https://en.wikipedia.org/wiki/Modular_programming
Last edited on
Topic archived. No new replies allowed.