problem with my semple calculator

Feb 3, 2012 at 2:14pm
hello all i'm write simple calculator and i have some problem at verfiy the input and using functions and need some advice to make the code better
at this code if user input bad char at operathion the program crash how i can solve it ?
and how i can use cin.ignore and cin.clear any one can explains all kind of cin


the 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
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
/*
  Name: mohamed elhababshe
  Copyright: rinGawy Team
  Author: mohamed elhabashe
  Date: 24/01/12 02:27
  Description: simpel calculator
*/

#include <cstdlib>
#include <iostream>
float a,b;
int e,f,c;

using namespace std;
void Errorinput(void)
{
cout<<"-------------------------"<<endl<<"Error Enter your number again"<<endl<<"-------------------------"<<endl;
}

void Errormethod(void)
{
cout<<"-------------------------"<<endl<<"Error "<<a<<"/0 Wrong method cannot divide by zero please correct your method"<<endl<<"-------------------------"<<endl;
}

void Erroroperathion(void)
{
cout<<"-------------------------"<<endl<<"Error please Enter your operathion again"<<endl<<"-------------------------"<<endl;
}

int main()
{
    char d;
    do{
         first:
    cout<<"hello this main menu please Enter your choice number"<<endl<<"1-Enter calculating program"<<endl<<"2-information about me"<<endl<<"3-no thanks get me out of here"<<endl<<"=>>";
    cin>>c;
    if(cin.fail()==true)
    {
    cin.clear();
    cin.ignore(1);
    Errorinput();
    goto first;
    }
    else
    switch(c)
    {
             case 1:{
                  cout<<"welcome to my simple calculator"<<endl<<"Enter frist number =";
                  cin>>a;
                  if(cin.fail()==true)
                  {
                  cin.clear();
                  cin.ignore(1);
                  Errorinput();
                  goto first;
                  }
                  else
                  cout<<"Enter second number =";
                  cin>>b;
                  if(cin.fail()==true)
                  {
                  cin.clear();
                  cin.ignore(1);
                  Errorinput();
                  goto first;
                  }
                  else
                  cout<<"Enter your arithmetic operathion like ( + , - , * , / ,% ) =";
                  cin>>d;
                  if(cin.fail()==true)
                  {
                  cin.clear();
                  cin.ignore (10);
                  Erroroperathion();
                  goto first;
                  }
                  else
                      
                      switch(d)
                      {
                      case '+':cout<<"------------------"<<endl<<a<<"+"<<b<<"="<<a+b<<endl<<"------------------"<<endl;break;
                      case '-':cout<<"------------------"<<endl<<a<<"-"<<b<<"="<<a-b<<endl<<"------------------"<<endl;break;
                      case '*':cout<<"------------------"<<endl<<a<<"*"<<b<<"="<<a*b<<endl<<"------------------"<<endl;break;
                      case '/':{
                           if(b==0)
                           {
                                  Errormethod();
                                  goto first;
                           }
                           else
                           cout<<"------------------"<<endl<<a<<"/"<<b<<"="<<a/b<<endl<<"------------------"<<endl;break;
                           }
                      case '%':{
                           if(b==0)
                           {
                                  Errormethod();
                                  goto first;
                           }
                           else
                           e=(int)a;
                           f=(int)b;
                           cout<<"------------------"<<endl<<e<<"%"<<f<<"="<<e%f<<endl<<"------------------"<<endl;break;
                               }
                      default:Erroroperathion();
                      }
                      };break;
               case 2:cout<<"---------------------------------"<<endl<<"my name: mohamed elhabashe"<<endl<<"my age: 25"<<endl<<"from: Egypt (country of fredoom)"<<endl<<"---------------------------------"<<endl;break;
               case 3:cout<<"bye bye see you later"<<endl;break;
               default:{
                  Errorinput();
                  goto first;
                  };
               }        
}while(c!=3);
    system("PAUSE");
    return 0;
}


Feb 3, 2012 at 2:34pm
cin::ignore() extracts characters from the input buffer and simply discards them. If there are more characters in the buffer than you supply with the argument then they will stay in the buffer. Try using this instead:

1
2
3
// need to #include <limits>
cin.ignore(std::numeric_limits<int>::max(),'\n');
cin.clear();


This should suffice for clearing out the input stream.
Feb 3, 2012 at 3:26pm
Just out of curiosity, is there any reason for why you made classes for the error messages or is it to make it cleaner?
Feb 3, 2012 at 4:00pm
Texan40 thx for help i will try your instead and comment back

Vortex47 thx for help yea i'm using classes to make it cleaner and if i need chinge any message wiil be easy for all program and i'm want develop this project more and more

be back



Topic archived. No new replies allowed.