Calculator

First of all, any help would be appreciated.
I wanna use for-loop, and I want it to keep repeating until the user inputs "="

1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char **argv)
{
   float x;
   char m = '=';
   for( ; WHAT DO I ADD HERE? ; )
   {
      cout << "Enter Number";
      cin >> x;
   }
   
}


Thank you
(x!=m) so when x value is same as m it end the loop
Last edited on
don't forget to initialize x.

1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char **argv)
{
   float x = 0;
   char m = '=';
   //for( ;x != (m) ; )
   while(x != m)
   {
      cout << "Enter Number";
      cin >> x;
   }
}
Last edited on
I tired this, but it didn't work. Whenever I enter "=", the program bugs out and keeps repeating "Enter Number" forever. Any other solutions?
take the operation sign as input. (+,-,*,/,=) continue until = is inputted.
How do I continue until = is inputted? This is my whole question.
closed account (2LzbRXSz)
1
2
3
4
5
char input = ' ';
while(input != '=')
{
   // Code here...
}

You could use a for loop like Gamer2015 said.
Gamer2015 wrote:
// for(; x != (m) ;)


(Except just replace x with input in his example)
Last edited on
This is what I've done so far, and it still gives me endless Enter Number whenever I enter =

1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char **argv)
{
	float x=0;
	char input = ' ';
    while(input != '=')
	{
		cout << "Enter Number";
		cin >> x;
	}

}
when doing std::cin >> x; std::cin think it gets a float as input, since the character is no float std::cin says it can't read the data and sets the failbit and the badbit.

to be more concrete: for number types std::cin only accepts characters between '0' and '9' and for float it also accepts '.'
Since '=' is none of those characters std::cin sets its badbit and failbit and just does nothing when asked for data.
if failbit or badbit are set then std::cin will be skipped.

You can check those.
If the failbit or the badbit is set it means that something failed (for example there is an invalid input)
You have to clear the badbit and the failbit before you can read from the buffer again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <string>

int main()
{
    float x = 0;
    char input = '\0';
    while(input != '=')
    {
        if(std::cin.good()) { // if the state is good
            std::cout << "Enter Number"; // ask for a number
            std::cin >> x; // read a float
        }
        else { // if either badbit or failbit are set
            std::cin.clear(); // clear badbit and failbit
            std::cin >> input;  // read the character that has been written
        }
    }
}


It seems like this simple question was quite complicated.
Last edited on
closed account (2LzbRXSz)
Sorry I forgot to mention that cin >> x needed to be replaced with cin >> input in my example.

Gamer2015's example is wonderfully explained and very well put. It works perfectly for me now.
What type of calculator are you making??

If a simple one then I created one some time ago Here is the source 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

#include <iostream>

using namespace std;

int main()
{
	int x , y , z;
	
	cout<<"Please enter value of x"<<endl;
	
	cin>>x;
	
	cout<<"Please enter value of y"<<endl;
	
	cin>>y;
	
	
	char op;
	
	
	cout<<"Enter arithematic operation"<<endl;
	cin>>op;
	
	
	switch(op)
{
	case '+' :
	           z = x+y;	cout<<"The value of z is : "<<z<<endl;break;
	    case '-' :
	         z=x-y;
	         
	         case '*' :
	          z= x * y ;	cout<<"The value of z is : "<<z<<endl;break;
	          
	           case '/' :
	             z = x/y;	cout<<"The value of z is : "<<z<<endl;break;
	             
	             
	             
	             default : 
	              cout<<"Invalid operator " ;break;
	}
	
	
	cin.get();
	cin.ignore();
	return 0; 







	

}
Topic archived. No new replies allowed.