Problem when user gives answer

Hi there. I am new to this wonderful community! :)

I am facing a problem on my code.This is my code which solves second degree equation.
The problem is that if user gives a letter or a pretty high number (like 8888888888) instead of a normal number,then the console gets crazy and displays messages too fast. So i need to disable all the buttons except the number ones and have no problem with big numbers.

Here is my code.

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
#include <iostream>
#include <math.h>



using namespace std;

  /////////////////////////////////////////////////////////////////
 ///////////////////////_____main()_____//////////////////////////
/////////////////////////////////////////////////////////////////
void process();
void normal();
void complex_function();


main()
  {
      process();
  }



  /////////////////////////////////////////////////////////////////
 ///////////////////_____Process function()_____//////////////////
/////////////////////////////////////////////////////////////////
void process ()
 {
      // Variable Statement
      int a;
      int b;
      int c;
      int D;
      int Ap;
      float X1;
      float X2;

   do
   {

      cout << "\t\t **** Second Degree Equation ****" << endl;
      cout << "\n\nGive number for x^2: "; cin >> a;
          if ( a == 0)
            {
                do
                  {
                      cout << "You can't give the 0 number. Give another number." << endl;
                      cout << "New number for x^2: "; cin >> a;
                  }while ( a == 0);
            }
      cout << "Give number for x: "; cin >> b;
      cout << "Give a constant number: "; cin >> c;

      D = pow(b,2) - (4 * a * c);
      cout << "\n\nThe Discriminant is: " << D << endl;
      if (D > 0)                               // D > 0
         {
             X1 = (-b +  sqrt(D)) / (2 * a) ;
             X2 = (-b -  sqrt(D)) / (2 * a) ;
             cout << "\n\n\n1st solution is: x = " << X1;    //1st Solution
             cout << "\n2nd solution is: x = " << X2;       //2nd Solution
         }
      else if (D == 0)                         //D = 0
           {
             X1 = (-b) / 2 * a;
             cout << "\n\n\nUnique solution is: x = " << X1;   //Unique Solution
           }
      else                                             //No Solutions (D<0)
           {
               cout << "\nThere are no solutions (D>0)." << endl;
           }
   cout << "\n\n\nNew Equation?" << endl;
   cout << "\n 1.Yes \t2.No" << endl;
   cout << "\nGive number: "; cin >> Ap;
   if (Ap != 1 && Ap != 2)
       do{
           cout << "Please select Number 1 (for Yes) or Number 2 (for No)." << endl;
           cout << "\nGive number: "; cin >> Ap;
         }while (Ap != 1 && Ap != 2 );
  }while (Ap == 1);
   cout << "\n\n\n\n\n\nHave a nice day :) " << endl;
 }


Thank you in advance!!
This isn't really a programming problem, more a logic problem.

Think about it, either cap the size of the number allowed, or let it output less information with a larger number.
closed account (jwkNwA7f)
cin.fail() my work to keep it from going crazy.

This is the best link I could find:
http://www.dreamincode.net/forums/topic/161499-cinfail/

You could google it to find better ones.
Last edited on
Topic archived. No new replies allowed.