So, I joined DaniWeb in february, and posted a problem regarding this code. I then quit learning for a while, and started again a week ago or so, continuing this. I've made it better, and its almost working, except for the If statement doesnt work. It asks you what you want to do, and no matter what letter you put in, it will run the rectangle area function, then right after, run the volume function, then end the program.
I really dont know whats up, but it could be the getline(cin, chooseType); thats messing up, im not sure.
also if theres any thing I can do to make my code look better, will be appreciated ;)
help please? :P
(remember im a beginner)

#include <iostream>  
#include <conio.h>
#include <string>
using namespace std;  

int triangularPrism();					// global variables
int rectangleArea();					// global variables
		


int main() {   
 								
 string chooseType;						// variables
    system("color 0c"); 
	cout << "What would you like to do?" << endl << endl;
	cout << "Press A to work out the area of a rectangle,\nor V to work out the volume of a triangular prism. \n";   // choice
	getline(cin, chooseType);
	cin.clear();
	if(chooseType == "a" || "A"){            //if statement, "a" chooses area of rectangle
		rectangleArea();
	}
	if(chooseType == "v" || "V"){        //second if, "v" chooses volume of triangular prism
		triangularPrism();
	}
return 0;
}

int rectangleArea() {
	int rectangleLength, rectangleWidth, rectangleArea;
	cout << "Enter the length of the rectangle: ";
     cin >> rectangleLength;
     cout << "Enter the width of the rectangle: ";
     cin >> rectangleWidth;
     rectangleArea = rectangleLength * rectangleWidth;
     cout << "\n \n";
     cout << rectangleLength << " x " << rectangleWidth << " = " << rectangleArea;
     cout << "\nThe area of the rectangle is: " << rectangleArea << "\n \n";
     cout << "Press any key to exit. \n" << endl;
	 getch();
	 return 0;

}
int triangularPrism() {
	double triangularLength, triangularBase, triangularHeight;
	cout << "Enter the length of the Triangular Prism: ";
	cin >> triangularLength;
	cout << "Enter the base, otherwise known as width: ";
	cin >> triangularBase;
	cout << "Enter the height: ";
	cin >> triangularHeight;
	cout << "\n" << triangularLength << " times " << triangularBase << " times " << triangularHeight << " divided by 2 " << "\n" "(" << triangularLength << "*" << triangularBase << "*" << triangularHeight << "/2" << ")" "\n";
	cout << "The volume of the Triangular Prism is: " << triangularLength * triangularBase * triangularHeight / 2 << endl;
	cout << "Press any key to exit. \n" << endl;
	getch();
	return 0;
}

Edited 5 Years Ago by FriXionX: n/a

>if(chooseType == "a" || "A"){
C++ parses it like this:

if(chooseType == "a" || "A" != 0){

Adjust accordingly.

I dont get it, so i have to add something to make it check if its not equal to something?

No. That's what you've already done. You have to make something else.

Right now, your if statement has the following logic:

If either of the following two things are true, then triangularPrism:
1) chooseType == "v"
2) "V"

Option one there will sometimes be true, sometimes not be true.
Option two will ALWAYS be true, because only zero is evaluated to false.

What you meant to say was

If either of the following two things are true, then triangularPrism:
1) chooseType == "v"
2) chooseType == "V"

Comments
Explained clearly.

No. That's what you've already done. You have to make something else.

Right now, your if statement has the following logic:

If either of the following two things are true, then triangularPrism:
1) chooseType == "v"
2) "V"

Option one there will sometimes be true, sometimes not be true.
Option two will ALWAYS be true, because only zero is evaluated to false.

What you meant to say was

If either of the following two things are true, then triangularPrism:
1) chooseType == "v"
2) chooseType == "V"

So this will work?

if(chooseType == "a" || chooseType == "A")

Edited 5 Years Ago by FriXionX: n/a

seriously, why cant I edit my posts? ¬_¬
and yeah, I did what I posted up there and it works.

Thanks for explaining it so well & clear Moschops, +1 rep. :)

Edited 5 Years Ago by FriXionX: n/a

It's not necessary to use a string class, You should have made chooseType a char.
If it were a char,
Use a single quote...

if(chooseType == 'a' || chooseType == 'A')

But its all good... you are trying.

It's not necessary to use a string class, You should have made chooseType a char.
If it were a char,
Use a single quote...

if(chooseType == 'a' || chooseType == 'A')

But its all good... you are trying.

Yeah, originally I was gonna put "Area" or "Volume" so it was gonna be a string, but thanks, i will change it to char to save space. :)

This question has already been answered. Start a new discussion instead.