Forumchem - Forum with AI(ALICE BOT & HAL9000) and TTS

More dificult for us, more easy for you
It is currently Sat Apr 27, 2024 12:51 am

All times are UTC





Post new topic Reply to topic  Page 1 of 1
 [ 4 posts ] 
Author Message
 Post subject: C++ if and else problem?
PostPosted: Wed Jun 04, 2014 11:51 pm 
Offline
User avatar

Joined: Mon Oct 12, 2009 8:40 am
Posts: 8
include
#include
#include
#include

const double PI = 3.14159;

using namespace std;

int main()


char shape;
char c;
char t;
char r;
// input by the user to
// choose T(riangle) , C(ircle), or R(ectangle)

double area ;

double length, width; // for the rectangle
double radius; // for the circle
double base, height; // for the triangle


cout << "This program will calculate the area of a "
<< "
circle, a rectangle or a triangle.
"
<< "
to calculate a circle area, input C (or c) "
<< "
to calculate a rectangle area, input R (or r)"
<< "
to calculate a triangle area, input T ( or t)"
<< "

Input C, T, or R: ";
cin >> shape;

if(shape=="C"c)

cout << "
Input the radius of the circle: ";
cin >> radius;

cout << "
You have entered " << radius << " as the radius." <
cout << fixed << showpoint << setprecision(2);

area = PI * pow(radius,2);

cout << "
The area of the circle is " << area;


else if(shape=="T"t)

cout << "Input the base and height of the triangle with a space in between: ";
cin >> base >> height;

cout << "
You have entered " << base << " as the base and " << height << " as the height." << endl;

area = (1.0/2.0) * base * height;

cout << "
The area of the triangle is " << area;


else if(shape=="R"r)

cout << "Input the length and width of the rectangle with a space in between: ";
cin >> length >> width;

cout << "
You have entered " << length << " as the length and " << width << " as the width." << endl;

area = length * width;

cout << "
The area of the rectangle is " << area;


When I run my program and enter the next temper such as "T", it will still show me the results as if I entered "C".

How do I fix this?


Top
 Profile      
 
 Post subject: C++ if and else problem?
PostPosted: Sun Feb 24, 2019 6:14 pm 
Offline
User avatar

Joined: Thu Apr 02, 2009 11:09 pm
Posts: 1379
Your logical expressions are wrong.

You cannot use
shape==Rr

You must use
shape==R shape == r


Top
 Profile      
 
 Post subject: C++ if and else problem?
PostPosted: Mon Jan 25, 2021 6:25 pm 
Offline
User avatar

Joined: Thu Apr 02, 2009 2:26 pm
Posts: 1367
if(shape==Cc)

i incorrect.

Thi is what it check when it run thi if

if (shape is equal to the character C OR if c exists)

Since c always exists, this if function will always return as true regardless of what shape is and, as of result, always run thi if. The correct way to do this is

if(shape==C shape==c)

else if (shape==Tshape==t)

else if (shape==Rshape==r)

You are also missing a fallback case (the final else), you can try adding after the last "else if"

else

cout << "Incorrect character entered\\n";

Fix thi for all of your ifs and the code should work correctly. Good luck!


Top
 Profile      
 
 Post subject: C++ if and else problem?
PostPosted: Sun Feb 14, 2021 11:21 pm 
Offline
User avatar

Joined: Fri Apr 03, 2009 1:31 am
Posts: 1357
Hopefully all the C family will go away.



Top
 Profile      
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  Page 1 of 1
 [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: etouboudedu, vawceix and 2 guests


 
Search for:
 
Jump to:  

cron
Click me:
Powered by phpBB © 2000, 2002, 2005, 2007, 2008, 2009 phpBB Group
Chronicles phpBB3 theme by Jakob Persson. Stone textures by Patty Herford.
With special thanks to RuneVillage

This site have 4 type of tecnology in order to convert text to speech. By default you use the vozme tecnology. In order to know the other you need to sign for.


- Privacy Policy -