failed result function sqrt

can anybody help me?

1. Why in calculating the discriminant is the result failed?

2. How can i do this: If D < 0 program , program stops and prints e.g: "D cant be less than 0, please choose another a,b,c. "
When I tried it program continued and calculated x1 and x2.
thanks a lot.

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
#include <iostream>
#include <math.h>
using namespace std;
enum VOLBA { ZadajHodnoty=1, End};

class rovnica
{
      public:
             rovnica (int ZmenaA,int ZmenaB, int ZmenaC);
             ~rovnica();
             int ziskajA() const {return a;}
             int ziskajB() const {return b;}
             int ziskajC() const {return c;}
             int ziskajD() const {return D;}
             void NastavVelkost(int A,int B,int C);
             int VypocetDiskriminant() {D = (b*b) - ((4*a)*c); return D;}
             float VypocetKorenaX1() {X1 =  sqrt (D) ; return X1;} 
             float VypocetKorenaX2() {X2 = ((-b) - sqrt (D)) / 2*a; return X2;} 
             void KontrolaD() { if(D<0) {cout<<"Diskriminant je zaporny rovnica nema riesenie.\n\n";}
                             
            
               }  
      private:
              int a;
              int b;
              int c;
              int D;
              float X1,X2;
              }
;

void rovnica::NastavVelkost(int A,int B,int C)
{
     a=A;
     b=B;
     c=C;
     }
     
rovnica::rovnica(int ZmenaA,int ZmenaB, int ZmenaC)
{
                 a = ZmenaA;
                 b = ZmenaB;
                 c = ZmenaC;
}

rovnica::~rovnica()
{}

int ponuka();
int Diskriminant(rovnica);
float VypocetKorenaX1(rovnica);
float VypocetKorenaX2(rovnica);

int main()
{
    int volba;
    bool end = false;
    rovnica MojaRovnica(1,1,1);
    
   while(true)
   {
   int volba = ponuka();
       
        switch(volba)
                {
                     case ZadajHodnoty:
                          int A,B,C;
                          cout << "\nZakladny tvar rovnice: ax2 + bx + c = 0\n\n";
                          cout << "a: ";
                          cin >> A; cout << endl;
                          cout << "b: ";
                          cin >> B; cout << endl;
                          cout << "c: "; 
                          cin >> C; cout << endl;
                          MojaRovnica.NastavVelkost(A,B,C);
                          Diskriminant(MojaRovnica);
                          VypocetKorenaX1(MojaRovnica);
                          VypocetKorenaX2(MojaRovnica);
                          break;
                     case End:
                          end = true;
                          break;
                     
                     default:
                             cout << "prosim inu volbu.";
                             break;
                             }
        if (end)
        break;
        }
        return 0;


    
    cin.get();
    cin.get();
    return 0;
}

int ponuka()
{
    int volba;
    cout << "[1] Zadaj hodnoty. " << endl;
    cout << "[2] End. " << endl;
    cin >> volba;
    return volba;
}

int Diskriminant(rovnica MojaRovnica)
{
      cout << "D: " << MojaRovnica.VypocetDiskriminant() << "\n";
      MojaRovnica.KontrolaD();  
    }         

float VypocetKorenaX1(rovnica MojaRovnica)
{
      cout << "X1: " << MojaRovnica.VypocetKorenaX1() << "\n\n"; }

float VypocetKorenaX2(rovnica MojaRovnica)
{
      cout << "X2: " << MojaRovnica.VypocetKorenaX2() << "\n\n"; }
Thank you for using caps in your variable definitions, otherwise I never would have been able to translate for Czech (It helps a little to know what you are saying in the program).

The problem is in your switch statment starting in line 64. Switch is checking volba to see if it is equal to ZadajHodnoty (Set ???). If these are always equal for some reason then you will get the problem you are describing.

Also the function on Line 119, I haven't seen that before does that loop ever end?
Last edited on
Topic archived. No new replies allowed.