HELP PLEASE URGENT :(

please help i need to make a program that will cout as a menu to choose from converter or calculator here's my sample

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
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
	double a, b;
	char operation, more, use;

	cout << "Which do you want to use c1 for calculator c2 for converter\n\n";
	cin >> use;
	if (use == 'c1' || use=='C1')
		cout << "Calculater program by Ryu D. Nakasato" << endl;
	cout << "-----------------------------------------------------------------------" << endl;
	cout << "Copyright law protected if copied or edited without my permission person is \nsubjected to pay $100 per letter or space";
	cout << "\n\n";
	do
	{
		cout << " Please enter an operation which you like to calculate (+,-,*,/,s)";
		cout << " \n [s stands for swap]:";
		cin >> operation;
		cout << endl << endl;
		cout << " Please enter two numbers to apply your requested operation(";
		cout << operation << "):" << endl << " 1st num:";
		cin >> a;
		cout << " 2nd num:";
		cin >> b;
		cout << endl;
		switch (operation)
		{
		case'+':
			cout << "The addition of two numbers (" << a << "," << b << "):";
			cout << a + b << endl;
			break;
		case'-':
			cout << "The substraction of two numbers (" << a << "," << b << "):";
			cout << a - b << endl;
			break;
		case'*':
			cout << "The multiplication of two numbers (" << a << "," << b << "):";
			cout << a*b << endl;
			break;
		case'/':
			cout << "The division of two numbers (" << a << "," << b << "):";
			if (b == 0)
			{
				cout << "not valid" << endl;
			}
			cout << (a / b) << endl;
			break;
		case's':
			cout << "The swap of two numbers (" << a << "," << b << "):";
			swap(a, b);
			cout << "1stnumber=" << a << " and 2nd number=" << b << endl << endl;
			break;
		default:
			cout << "unknown command" << endl;

		}
		cout << "Continue[Y/N]? ";
		cin >> more;
		cout << endl << endl;
	} while (more == 'y' || more == 'Y');
	if (use == 'c2'||use=='C2')
		do{
			cout << "hehe";
		} while (more == 'y' || more == 'Y');
}
Last edited on
okay? whats the problem. And please edit your post and put the code between code tags - http://www.cplusplus.com/articles/jEywvCM9/
the proble is when ever i cin c2 it always cout the calculator program i want it to cout the "hehe"
thanks
You need to use {} in your if (use == 'c1' || use=='C1'). Right now it's compiling like:
1
2
3
4
if (use == 'c1' || use=='C1')
{
        cout << "Calculater program by Ryu D. Nakasato" << endl;
}
.

After that its just gonna keep on roaming down your rows and run your calculator. It should be :

1
2
3
4
if(calculator)
{
       //CALCULATOR
}


right now your code does:

1
2
3
4
5
if(calculator)
{
      cout<<"........"<<endl;
}
      //CALCULATOR 

Last edited on
i don't get it can you give me a better example?
Look at your row 12:
if (use == 'c1' || use=='C1')

An if-statement is always followed by a follow-up. For example if(5>4) "\n" cout<<"this will never happend";

As long as the next coming data is more than only 1 row. You need to use clams {} to tell the compiler where it stats and ends, because without clams, the if-statement will only be used on the next coming line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
EXAMPLE 1:
if(X+5==10)
	//Without clams, this row is the only row that will be applyed to the if-statement
	//This row will not apply to the if-statement, However this row will always execute after
        //the row above, no matter what.

EXAMPLE 2:
if(X+5==10)
{  
	//With clams, this row will be the first row to apply the if-statement
	//This row will also apply to the if-statement, because there is no end-clam yet.
	//This row will be the last applying row to the if-statement, because here comes the end-clam
}   

	//The if-statement is now ended and this row will be executed as normal without any exceptions.


Now if you look at your code and check row 12 and 13. What example does your code apply to? Example 1 or example 2? When you figured out that your row 12,13 looks like example 1 you may also see that nothing that happends after row 13 will be applyed to the if-statement.

The program will always be executed from the top to the bottom. In your case, the calculator will always run.

So:


1
2
3
4
5
6
7
8
9
10
11
if (use == 'c1' || use=='C1')
{ // <------ VERY IMPORTANT
        // HERE IS YOUR CALCULATOR
}
      // YOUR CALCULATOR CAN NOT BE HERE

else if(use == 'c2'||use=='C2')
{ //<------ VERY IMPORTANT
      // HERE IS YOUR CONVERTER
}
     // YOUR CONVERTER CAN NOT BE HERE 

cout << "Copyright law protected if copied or edited without my permission person is \nsubjected to pay $100 per letter or space"; I don't think that is how copyright laws work.. Otherwise I guess I would be paying you about 10,000 dollars.
Topic archived. No new replies allowed.