switch and if else statement help

When I reach case 2 and type "n" my complier prints out my cout statement but just continues running until the end of the program. I commented out a few statements to make sure that it actually did run till the end. What am I doing wrong?

Also i'm not really worried about the math so you don't really have to check that. I just want to get it working properly before I deal with the logic 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double x1,x2,y1,y2,slope;
    int choice;
    
    cout<<"Type one of the following numbers "<<endl;
    cout<<"1. Are you trying to find the slope ?"<<endl;
    cout<<"2. Are you trying to find the y intercept ? "<<endl;
    cout<<"3. Are you trying to find the x intercept ? "<<endl;
   
    cin>>choice;
    
    switch (choice) {
        case 1:
            cout<<"Enter the points you would like to find the slope for "<<endl;
            cin>>x1>>y1;
            cout<<"Enter the other set of points "<<endl;
            cin>>x2>>y2;
            slope=(y2-y1)/(x2-x1);
            cout<<"Your slope is "<<slope<<". In other words m="<<slope<<"x"<<endl;
            break;
        case 2:
            double slope, b;
            int y;
            cout<<"Enter you slope (as a decimal if it's a fraction) "<<endl;
            cin>>slope;
            cout<<"Do you know more than one point? (x,y) is one point. If so type y for yes and n for no. "<<endl;
            cin>>y;
            
            if (y=='n' || y=='N')
            {
            double x1,y1;
            
            cout<<"Enter the point you know "<<endl;
            cin>>x1>>y1;
            b=(((slope*x1)-y1)*(-1));
            //cout<<"Your y-int is "<<b<<endl;
            
            }
            
            else
            {
                cout<<"Enter the points you know "<<endl;
                cin>>x1>>y1>>x2>>y2;
                b=(((slope*x1)-y1)*(-1));
                //cout<<"Your y-int is "<<b<<". b="<<b<<endl;
                
            }
            
    }
    
    return 0;
}
Last edited on
Characters cannot be stored in integers - you're putting the standard input stream into an error state and so all subsequent read operations are ignored. On line 27 use char instead of int.
Thanks works just perfectly.
Topic archived. No new replies allowed.