Finding the problem in this basic calculator

Hey guys,
I've made a basic calculator, which was working perfectly. However, when I made the choice options
1
2
3
if(choice2 == 'y'||'Y')
while(choice1 != 'n'||'N')
if (choice1 == 'n'||'N')

it started to act weird. But when I remove the 'N' and the 'Y' (which leaves the 'n' and 'y') it works fine again.

To come to my question: how can I make the choices case sensetive? Because obviously this doesn't work :(


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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// This is a simple calculator, capable of computing sums with: +,-,/,* and powers
//
// to do: include the 'scientific' option after case 1...
//

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;

void PressEnterToContinue()
  {
  cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  int c;
  printf( "\nPress ENTER to continue... " );
  fflush( stdout );
  do c = getchar(); while ((c != '\n') && (c != EOF));
  }

int main()
        
{
    //declare all variables used
    double num1, num2, result;
    int choice;
    char choice1, choice2;
    char y, n, Y, N;

    //.........................Including help/ instruction manual...............................................................................    
    cout<<"\nWould you like to view the readme file? (y/n): ";
    cin>>choice2;
    if(choice2 == 'y'||'Y')
    {
//.................................Actually including the text...........................................
                cout<<"\n\nTHIS IS THE README FILE...\n\nYou're supposed to use this calculator is by awsnering various questions.";
                cout<<"\n1) The calculator will first ask you what you want to do.\n2) Then it'll ask you on which numbers you'll like to do the operation chosen.";
                cout<<"\n3) After that it'll show you the result and ask you to press enter to continue..";
                cout<<"\nAfter you've done that you can choose if you'd like to go on using the program.";
                cout<<"\n\nThis calculator was created for own purposes only. \nYou may distribute this, only if credited to Kaduuk Productions... \n\n";  
                cout<<"Have a good time! =)\n"<<endl;
                cout<<"=========================================================";
                cout<<"\nTHIS CALCULATOR WAS CREATED BY KADUUK PRODUCTIONS\n\n";
                PressEnterToContinue();
                system("cls");
     }
    
    while(choice1 != 'n'||'N')
    {
    //welcome the user
    cout << "****** Time to do some basic math!******" << endl;
    cout << "________________________________________" << endl;
    cout << endl;
    
    
    cout<<"\nPress:\n*\t1 to add,\n*\t2 to subtract,\n*\t3 to multiply,\n*\t4 to divide,\n*\t5 to raise powers,\n*\t6 for sqroots.\n"<<endl;
	cin>>choice;

	switch(choice)
	{
	case 1:
         cout << "Enter number 1: ";
         cin >> num1;
         cout<<"\nEnter number 2: ";
         cin >> num2;
         
         result = num1 + num2;
         cout<<num1<<" + "<<num2<<" = "<<result<<endl;
         if(result <= 10000)
         {
                   cout<<"\n This would be "<<scientific<<result<<endl;
         }
         else
         { 
             cout<< "\nThe rounded result = "<<setprecision(3)<< result<<endl;
         }
		 break;
		 
	case 2:
         cout << "\nEnter number 1: ";
	     cin >> num1;
         cout<<"\nEnter number 2: ";
         cin >> num2;
         
        result = num1 - num2;
        
        cout << setprecision(4) <<result<<endl;
		cout<<num1<<" - "<<num2<<" = "<<result<<endl;
        cout<< "\nThe rounded result = "<<setprecision(3)<< result<<endl;
		break;
		
	case 3:
         cout << "\nEnter number 1: ";
	     cin >> num1;
         cout<<"\nEnter number 2: ";
         cin >> num2;
         
		result = num1 * num2;

		cout<<num1<<" * "<<num2<<" = "<<result<<endl;
		cout<< "\nThe rounded result = "<<setprecision(3)<< result<<endl;
		break;
		
	case 4:
         cout << "\nEnter number 1: ";
	     cin >> num1;
         cout<<"\nEnter number 2: ";
         cin >> num2;

		if(num2 == 0)
			cout<<"Division by zero not allowed.\n"<<endl;
		else
		{
			result = num1 / num2;
			cout<<num1<<" / "<<num2<<" = "<<result<<endl;
			cout<< "\nThe rounded result = "<<setprecision(3)<< result<<endl;
		}
		break;
	case 5:
         
         cout << "\nEnter number 1: ";
	     cin >> num1;       
         cout<<"\nEnter the power you want to raise to: ";
         cin >> num2;
         
         result = pow (num1, num2);
         cout<<num1<<" to the power "<<num2<<" = "<<result<<endl;
         break;
    case 6:
         
         cout << "\nEnter the number: "<<endl;
         cin >> num1;
         if (num1 > 0)
            {
                  num2 = sqrt(num1);
                  cout<<"The sqroot of "<<num1<<" = "<<num2<< endl;
                  cout<<"\nRounded it is"<<setprecision(3)<< num2<<endl;
            }
         else
             {
               cout<< "The sqroot of a number < 0 is not possible..."<<endl;
             }      
          break;
	default:
		cout<<"Invalid input."<<endl;
        }
PressEnterToContinue();
cout << "Would you like to continue? (y/n)"<<endl;
cin >> choice1;
if (choice1 == 'n'||'N')
{
        return 0;
}                    
system ("cls");
}

return 0;

}


Thanks!
-Kaduuk
This:
 
if (choice1 == 'n'||'N')


Should be this:

 
if (choice1 == 'n' || choice1 == 'N')


Last edited on
If you add many more options, then using if on all the cases will get messy... In which case you can use tolower() to lowercase all the options that are input and then only if the lowercase options.

You might also consider replacing the prompts with command-line switches.
Last edited on
@ Galik: Thanks! It worked :) But could you please explain why it should be that way?
@ Ryan: how should I replace the prompts? (and what would it do?) And how would the tolower() function look like? Would it be placed in the main function, or as a function on its own?

Sorry, but I'm realy noob...
Last edited on
kaduuk wrote:
@ Galik: Thanks! It worked :) But could you please explain why it should be that way?


Order of precedence:

 
if (choice1 == 'n'||'N')


Here the first expression to be evaluated is the 'n' || 'N' (yes that is a valid expression)

So what does 'n' || 'N' mean?

|| is a boolean operator so the values on either side are promoted to type bool and then evaluated.

'n' and 'N' are type char and neither has a value of zero so the boolean value of each is 'true'

So the expression 'n' || 'N' is equivalent to (true || true). That evaluates to simply (true).

Only then does the compiler use that value in other expression == forming: if(choice1 == true)

That is not what you wanted.
Here the first expression to be evaluated is the 'n' || 'N' (yes that is a valid expression)

No. The first expression to be evaluated is choice1 == 'n'.
It's either true or false, doesn't matter since 'N' == true and (something) || true is true,
Ok thanks guys! :)
Topic archived. No new replies allowed.