Halp

Pages: 12
closed account (Dj15fSEw)
I'm making an RPG, kinda world diplo, and I'm having problems just getting the program to acknowledge that I've picked a country.

The nukes and votingpower are going to be implemented somehow, I'm just curious onto how the program can recognize my input.
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
#include <string>
#include <fstream>
#include <windows.h>
#include <time.h>
#include <cstdlib>
#include <iostream>

using namespace std;

int player,usa,china,russia,france,northk,iran,nukes,votingpower;

int main ()
{
	cout << "Pick a country.\n";
	cout << " usa\nfrance\nchina\nrussia\nnorthk\niran\n";
	if (cin >> usa)
		{
		nukes=1000;
		votingpower=10;
		cout<< "You have picked America.\n";
	}

	if (cin >> china){
		nukes = 500;
		votingpower=15;
		cout<<"You have picked China.\n";

	}

	if (cin >> russia)
	{
			nukes=2000;
			votingpower=5;
	}
	
	if (cin >> france)
	{
		nukes=200;
		votingpower=5;
	}

	if (cin >> northk)
	{
		nukes = 1000;
		votingpower=2;
	}

	if (cin >> iran)
	{
		nukes=0;
		votingpower=1;
	}


	cin.get();
	cin.get();
	return 0;

}
I've never seen cin used like the before, but what i would do( and IMO it makes it look nice ) is to make a variable called choice. Then do the code below. Of couse choice would have to be string.

1
2
3
4
5
6
7
8
cin>> choice;

switch( choice )
{
case "usa":
case "china" 
etc..
}
closed account (Dj15fSEw)
Yeah, i tried to assign each one a number but it didn't work, so I'll try your method.
closed account (Dj15fSEw)
I tried what you said, did not work.
Also, a different site said that choice had to be an int var, not a string. I tried it with a string and it also didn't work.
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
#include <string>
#include <fstream>
#include <windows.h>
#include <time.h>
#include <cstdlib>
#include <iostream>

using namespace std;
int choice;


int player,nukes,votingpower,usa,china,russia,northk,iran,france;


int main ()
{
	cout << "Pick a country.\n";
	cout << " usa\nfrance\nchina\nrussia\nnorthk\niran\n";
	cin>> choice;
	switch( choice )
	{
	case "usa":
		nukes=1000;
		votingpower=10;
		cout<< "You have picked America.\n";

		break;
	case "china":
		nukes = 500;
		votingpower=15;
		cout<<"You have picked China.\n";
		break;
	case "russia":
		nukes = 2000;
		votingpower=5
		cout << "You have picked Russia.\n";
		break;
	case "france":
		nukes=200;
		votingpower=5;
		cout << "You have picked France.\n";
		break;
	case "northk":
		nukes = 1000;
		votingpower=2;
		cout << "You have picked North Korea.\n";
		break;
	case "iran":
		nukes=0;
		votingpower=1;
		cout << "You have picked Iran.\n";
		break;
	}


	cin.get();
	cin.get();
	return 0;

}
You can only use switch on integral types, as you say. You'll need to use if/else chains for strings.
closed account (Dj15fSEw)
but how can I make this work? It's giving me the error message of:
"Case expression not constant."
closed account (zb0S216C)
You cannot use string literals as case labels. You're gonna' have to use if statements. Also, use std::strcmp( ) to compare string literals or string::compare( ) is you're using a string type.

Wazzak
Oh lol i was thinking of chars. My bad. In this case i would go with making the user choose a number
closed account (Dj15fSEw)
I did that, but it completely gave me the wrong results.
i.e. if usa was 1, i would type in 1 and it would give me north korea. >_>
You must have done something wrong then. I would do it like this (*NOTE i havent tested this code ):

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
	enum Country
	{
		USA,
		China,
		Russia,
		France,
		NorthK,
		Iran
	};

	Country country;
        int choice;
 
        cout << "Pick a country.\n";
	cout << " 1. usa\n 2.france\n 3.china\n 4.russia\n 5.northk\n 6.iran\n";
        cin >> choice;

	switch( choice )
	{
	case 1:
		country = USA;
		break;
	case 2:
		country = China;
		break;
	etc...
	}
closed account (Dj15fSEw)
whats the
Country country

for?
It's where the country the user picked is stored.
closed account (Dj15fSEw)
Alright, and I also tried his code, there was only one error that said "missing function header".
What does that mean?
closed account (Dj15fSEw)
Alright, I got it to work, with the help of nano and the standard libraries -----> these -
algorithm complex exception list stack
bitset csetjmp fstream locale stdexcept
cassert csignal functional map strstream
cctype cstdarg iomanip memory streambuf
cerrno cstddef ios new string
cfloat cstdio iosfwd numeric typeinfo
ciso646 cstdlib iostream ostream utility
climits cstring istream queue valarray
clocale ctime iterator set vector
cmath deque limits sstream



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
enum Country
	{
		USA,
		China,
		Russia,
		France,
		NorthK,
		Iran
	};

	Country country;
        int choice;
 
        cout << "Pick a country.\n";
	cout << " 1. usa\n 2.france\n 3.china\n 4.russia\n 5.northk\n 6.iran\n";
        cin >> choice;

	switch( choice )
	{
	case 1:
		country = USA;
		break;
	case 2:
		country = China;
		break;
	etc...
	}


BUT.

Is there any way for me to be able to just type in say
USA or France and it be able to recognize it and match with "Usa" or "France"?
I said this in another thred, im no good with strings. I've read through the chapters on them in books but i never really went deep. This may not be right, but i believe if you did this..
1
2
3
4
5
6
7
8
std::string choice;

getline( cin, choice );

if( choice == 'usa' )
{
blahblahbalh
}



it would work. if it doesnt work like that, try "usa" instead of 'usa'
Try this:

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
#include <string>
#include <fstream>
#include <windows.h>
#include <time.h>
#include <cstdlib>
#include <iostream>

using namespace std;

string country; // New

int player,usa,china,russia,france,northk,iran,nukes,votingpower;

int main ()
{
	cout << "Pick a country.\n";
	cout << " usa\nfrance\nchina\nrussia\nnorthk\niran\n";
        cin  >> country;
	if (country == "usa")
		{
		nukes=1000;
		votingpower=10;
		cout<< "You have picked America.\n";
	}

	else if (country == "china"){
		nukes = 500;
		votingpower=15;
		cout<<"You have picked China.\n";

	}

	else if (country == "russia")
	{
			nukes=2000;
			votingpower=5;
	}
	
	else if (country == "france")
	{
		nukes=200;
		votingpower=5;
	}

	else if (country == "northk")
	{
		nukes = 1000;
		votingpower=2;
	}

	else if (country == "iran")
	{
		nukes=0;
		votingpower=1;
	}


	cin.get();
	cin.get();
	return 0;

}


and i believe this should work :)
closed account (Dj15fSEw)
Yep it worked, thanks :D

Just, can you explain it to me, l
ike youre teaching a retard
who knows very little C.

Thanks :D
First, that is C++
Now the explanation:

1
2
3
4
5
6
if (country == "usa")
{
	nukes=1000;
	votingpower=10;
	cout<< "You have picked America.\n";
}


country is a variable of type string (char* in C)
Lets say you enter usa when asked for the country.
It will check that whether country the country you entered is usa.
If it is it will go into the if statement.
The=is happens for all the cases.
closed account (Dj15fSEw)
Alright, but why double equals?
To check if they are the same.
= is the assignment operator. It will assign the value on the right of the operator to the one on the left.

== us the equality operator. It will check whether the value on its right is the same as the one on it left. It will return a boolean value which is used in the if/else statements in the above code.


Last edited on
Pages: 12