Case sensitive and looping

How do I make this not case sensitive? If I put in Pyramid, it will run the pyramid function, but if I put in pyramid, it recognizes it as else and runs the cube function

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
//If volume, ask is cube or pyramid
	
	else
	{
		string shapeVolume = "";
		cout << "What is the shape (Pyramid or Cube)?\n";
		cin >> shapeVolume;

		//If pryamid, ask for 2 values
		
		if (shapeVolume == "Pyramid")
		{
			double basePyramid = 0;
			cout << "What is the base length?\n";
			cin >> basePyramid;

			double heightPyramid = 0;
			cout << "What is the height?\n";
			cin >> heightPyramid;

			//Multiply 2 values, divide by 3

			double pyramidProduct = (basePyramid * heightPyramid) / 3;

			//Give result

			cout << "The volume of the pyramid is " << pyramidProduct << ".\n";
			return 0;
		}

		//If cube, ask for the value

		else
		{
			double baseCube = 0;
			cout << "What is the base length?\n";
			cin >> baseCube;

			//multiply base^3

			double cubeProduct = baseCube * baseCube * baseCube;

			//Give result

			cout << "The volume of the cube is " << cubeProduct << ".\n";
			return 0;


Also, I'm trying to make it so that if the user does not choose "area" or "volume" it will lopp around and ask again but I get an error on line 16 with the &&

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <math.h>
using namespace std;

int main()
{
	//Get area or volume

	string areaVolume = "";
	
	do
	{
		cout << "Are you trying to find the area or volume?\n";
		cin >> areaVolume;
	} while (areaVolume = "area" && areaVolume = "volume");
Last edited on
For the case sensitivity, you may use this :
if (tolower(shapeVolume) == "pyramid")

What it does is that first it converts all the characters of shapeVolume to lower case and then compares it without actually changing it.

And include the header ctype.h to use the tolower() function.


If you are trying to re-run the do-while loop when the user fails to enter "area"/"volume"?..
Then you have to use
!=
in place of a single
=
. A single "=" is used for assignment. For comparision, we use "==" , "!=" , "<" , ">" , "<=", ">=".
Last edited on
Alright thank you that worked, do you know about the looping error with the &&?
yeah, I've already mentioned it . :)

ALOK R wrote:
If you are trying to re-run the do-while loop when the user fails to enter "area"/"volume"?..
Then you have to use
!=
in place of a single
=
. A single "=" is used for assignment. For comparision, we use "==" , "!=" , "<" , ">" , "<=", ">=".


That error is coming because you are assigning instead of comparing. Use
!=
in place of
=
Thank you that fixed everything!
Topic archived. No new replies allowed.