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.
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";
// 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;
#include<iostream>
#include <string>
usingnamespace 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;
}
elseif (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;
}