Decision making program - problem

Hello,
This program is supposed to calculate the surface/perimeter of a triangle. (too lazy to add the function for surface lol, I did this code only for the decision making).
Can any1 help me so if a user enters perimeter/surface it calculates what it is asked? Also, can you explain how do you fix it?
Ty a lot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
    cout<<"Numbers?";
    float a,b,c,d;
    cin>>a>>b>>c;
    cout<<"What do you want to calc?";
    bool perimeter;
    bool surface;
    if (perimeter)
        d==a+b+c;
        cout<<d;
    if (surface)
        cout<<"lazy";
    return 0;
}
Hey Forta2k,
You haven't asked the user to do anything as yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout<<"What do you want to calc?";
    bool perimeter; // justify your variable. Is it true or false?
    bool surface;    // justify your variable. Is it true or false?
//some where here you have to ask the user to make a choice 
//since you are using bool data types. The options can only be true or false. 

if (perimeter) //
        d==a+b+c; // '==' you are checking if 'd' is the same as 'a+b+c' here. 
                          // '=' to give the value of 'a+b+c' to 'd'. 
        cout<<d; // If you are beginner coder. Use {} till you are proficient 
 if (surface)
        cout<<"lazy";



1
2
//some where here you have to ask the user to make a choice 
//since you are using bool data types. The options can only be true or false. 

How?
13
14
15
16
17
18
19
20
21
22
// Get rid of your boolean variables.  They're not doing what you want.
  char ans;
  cout << "p=perimeter s=surface";
  cin >> ans;  /'/ Get answer from user
  if (ans == 'p')
  {  // Do perimeter stuff
  }
  if (ans == 's')
  {  // Do surface stuff
  } 


Lines 16-17 need to be enclosed in {}, otherwise the cout will be done unconditionally.

forta2K, as AbstractionAnon said, you don't need the
bool
for this exercise.

Also, the use of cin>>a>>b>>c; is not nice as it doesn't tell the user to input the other values. One can just keep on pressing ENTER (as is often the case).

Use
1
2
3
4
5
6
7
 cout << " Give the sides of the triangle: " << endl; 
	cout << " side A = ";
	cin >> sideA;
	cout << " side B = ";
	cin >> sideB;
	cout << " side C = ";
	cin >> sideC;
instead.

My proposal for your problem
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
#include<iostream>
#include <string>

using namespace std;

int main()
{
	cout << endl << endl;
	cout << "*** CALCULATING THE PERIMETER / SURFACE OF A TRIANGLE *** "<< endl << endl;

	//Variable declaration
	double sideA = 0, sideB = 0, sideC = 0; 
	double perimeter;
	string surface = "Lazy!";

	cout << " Give the sides of the triangle: " << endl; 
	cout << " side A = ";
	cin >> sideA;
	cout << " side B = ";
	cin >> sideB;
	cout << " side C = ";
	cin >> sideC;

	
	cout << endl;
	cout << " What do you want to calculate? p=perimeter, s=surface? ";

	char answer;
	cin >> answer;

	if (answer == 'p' || answer == 'P') //takes both p and P as answers
	{
		//perimeter = "PERIMETER FORMULAR HERE";
		cout << " The perimeter of the triangle is " << perimeter << endl << endl;
	}
	else if (answer == 's' || answer == 'S')
	{
		cout << surface << endl << endl; //We already defined the surface as lazy. 
	}
	else
	{
		cout << " Invalid option! You have to re-run the program!" << endl << endl;
	}
	/*
	There are many ways of doing this such as using the while loop or do... while 
	but for now, this one works and i hope you are happy with it. 
	*/
	return 0;
}
Topic archived. No new replies allowed.