Variable Including and int and char

I currently made a variable using
string dim;
This does not include both at once because I am trying to make it as 3-D or 2-D for a Volume calc but I can use a number and letter.
what?

Can you explain your problem a little better?
I want to create a variable that wen u use cin >>
it will store "3d" or "2d"
A std::string is a container that can hold chars. You can store "3d" in a string, but both '3' and 'd' will be stored as chars. If you want to keep the integer part for calculations, it would be better to store it as an int.
1
2
3
4
5
6
7
8
struct dim {
    char ch;
    int i;
};
std::istream& operator>>(std::istream& is, dim& d) {
    /*Implement this to read from 'is' to 'd'*/ 
   return is;
}
Last edited on
filipe I tried using a string but it does not work

romal were do I insert that?
this is my code so far
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() 
{
	double cube_a;
	cout << endl;
	cout << "Welcome To The Volume Calculator" << endl;
	sleep(1);
	cout << "Here are some variables you will be working with:" << endl;
	cout << endl;
	sleep(1);
	cout << "h = height" << endl;
	cout << "(vertical)" << endl;
	sleep(1);
	cout << "r = radius" << endl;
	cout << "(half the diameter of a circle)" << endl;
	cout << endl;
	sleep(1);
	string dim;
	while (dim != "3-D" && dim != "2-D" && dim != "3d" && dim != "2d" && dim != "3D" && dim != "2D" && dim != "3-d" && dim != "2-d") {
		cout << "Would You Like To Use 3-D or 2-D Shapes?" << endl;
		cin >> dim;
		
		if (dim == "3-D" || dim == "2-D" || dim == "3d" || dim == "2d" || dim == "3D" || dim == "2D" || dim == "3-d" || dim == "2-d")
		{}
		
		else {
			cout << "Please Try Again" << endl;
		}
	if (dim == "3-D" || dim == "3D" || dim == "3d" || dim == "3-d") {
		
		string shape;
		while (shape != "Cube" && shape != "cube" && shape == "cylinder" && shape == "Cylinder") {
			cout << "Please Choose The Shape That You Would Like To Find The Volume Of" << endl;
			cin >> shape;
			if (shape == "Cube" || shape == "cube") {
				cout << endl;
				cout << "The formula for the volume of a cube is:" << endl;
				cout << "V=a³" << endl;
				sleep(1);
				cout << "Enter a:" << endl;
				cin >> cube_a;
				cout << endl;
				cout << "V=" << pow(cube_a, 3) << endl;
				sleep(5);
			}
			if (shape == "cylinder" || shape == "Cylinder") {
				cout << endl;
				cout << "The formula for the volume of a cylinder is:" << endl;
				cout << "V=πr²h" << endl;
				sleep(1);
				cout << "Enter h:" << endl;
				
			}
			else {
			}

		}
	}
	if (dim == "2d" || dim == "2D" || dim == "2-D" || dim == "2-d") {
		cout << " " << endl;
	}
	else{
	}
}
	

}
for std::string 's you should be using:

string.compare(string2);

if the return value is 0, the two strings match, or in this case:

if(dim.compare("3d") == 0) {
do stuff
}

and so on.

I hope this helps.
for std::string 's you should be using:

string.compare(string2);


Operator == is a lot faster and more obvious IMO.
do I put
string.compare(string2);
after I create a string or replace
string dim;
??
Topic archived. No new replies allowed.