sequence

I am having trouble structuring my programs, with cascading if statements and nested if statements. This program adds subtracts multiplys and divides. But you have to enter a or A for addition. S or s for subtraction, and so on. If you don't enter a correct letter the program should output INPUT ERROR. Also when subtracting, the smaller number should always be subtracted from the larger. Divison the larger number will always be divided by the smaller.
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
#include <iostream>
#include <string>
using namespace std;

int main ()

{
	// Declaring Varibles.
	char method = ' ';
	float first, second;
	int add, sub, div, multi ;
	

	cout<<"Please enter the method of factoring you would like to use,"<<endl;
	cout<<"(A = Addition),(S = Subtraction),(D = Divide),(M = Multiply)"<<endl;
	cin>> method;
	method = toupper(method);
	
	
    if (method == 'A' || 'a')
	{
		cout<<"Please enter the first number"<<endl;
		cin>>first;

		cout<<"Please enter the second number"<<endl;
		cin>>second;

		add = first + second;

		cout<<"The total is:"<<add<<endl;
	
	}
    else if (method == 'M' || 'm')
	{
		cout<<"Pleae enter the first number"<<endl;
		cin>>first;

		cout<<"Please enter the second number"<<endl;
		cin>>second;

		multi = first * second;

		cout<<"The total is:"<<multi<<endl;
    
	}
	else if (method == 'S' || 's')
	{
	cout<<"Please enter the first number"<<endl;
	cin>>first;

	cout<<"Plese enter the second number"<<endl;
	cin>>second;

	if (second > first)
	
		sub = first - second;
	
	else
		
		sub = second - first;
	cout<<"the total is:"<<sub<<endl;
	
	}

	else if (method == 'D' || 'd')
	{
		cout<<"Please enter the first number"<<endl;
	cin>>first;

	cout<<"Plese enter the second number"<<endl;
	cin>>second;

	if ( second > first)

		div = second / first;
	else 
		div = first / second;
	
	cout<<"The total is:"<<div<<endl;
	}

	else 
		cout <<"Input Error"<<endl;



	}





	return (0);
	}
Last edited on
The only issue I see with the structuring is your somewhat inconsistent spacing. You should work on lining up the code based on how many brackets deep it is.
when i run the program is always adds, never gets past the first if statement
Because you're "OR'ing" method, and when you do that the result will equal 'A'. You want your if evaluations to look like this:
 
if (method == 'A' || method == 'a')


EDIT: Or you can forget about the "||" since you transform 'method' with 'toupper()' on Line 17 anyway.
Last edited on
Is that the only way to write them?
Is there a way to use an and in the (test)
Doesn't matter, you've already done the correct thing but using "toupper()", see my edit in the previous post. But to answer you question, yes, that is the only way to do it because those logical evaluations work on the result of the operations on either side of them.
Last edited on
ty
Topic archived. No new replies allowed.