Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C++
string charpos_calculation(string input){
	switch (character)
	{
	case 'up':
		f8 = "^";
		printmap();
	break;

	case 'down':
	break;
	
	case 'r':
	break;

	case 'l':
	break;
	

	return output1;
}

string enepos_calculation()
{
	return output3;
}

string keystroke(){
	string output;
	while (true==true) {
		KeyStroke =	_getch();
		if (KeyStroke = 0)
		{
			KeyStroke = _getch(); // Even though there are 2 getch() it reads one keystroke
			switch (KeyStroke)
			{
			case UP_ARROW:
				Keystroke1 = "up";
				character = "up";
				charpos_calculation(Keystroke1);
			break;
			}	
		}
	}
	return output2;
}
 
int main()
{
	string test;
	string end1;

	cout<< "             Welcome To Zombie Death Rampage"<<endl;
	cout<< "         This Program Was Created By: Nick McCann"<<endl;
	cout<< "" <<endl;
	cout<< "" <<endl;
	cout<< "" <<endl;
	cout<< "                      Type Yes To Play" <<endl;
	cin>> test;

	system ("cls");

	while (test=="yes"){
		keystroke();
	}

	cin>> end1;

	return 0;
}

________________________________________________________________________________________________
I get the following errors

1. Error 1 error C2440: '=' : cannot convert from 'const char [3]' to 'char' . It is reffering to char character;

2. Error 2 error C1075: end of file found before the left brace

3. IntelliSense: expected a ';' It is referring to the charpos_calculation function. Do I need a semicolon after the function before the opening brace of the function.

These errors all deal with the code above.

I appreciate any advice given..
Posted
Updated 7-Jan-14 15:11pm
v2
Comments
Member 10506844 7-Jan-14 21:02pm    
I'm sorry that it didn't show my code as code, but it showed my words as code.
Member 10506844 7-Jan-14 21:04pm    
Also it does not display my tabs for my braces.

well, #1

C++
switch (character)
	{
	case 'up':


change the 'up' to just 'u', similarly for the case 'down' label, that'll need to be 'd' (you're switching on a character, not a string (iirc c++ doesn't allow switches on strings)

#2 in charpos_calculation you're missing a terminating '}' for the switch

not sure I like your technique, it seems you want to pass in strings, switch on chars

'g'
 
Share this answer
 
v2
Comments
Ron Beyer 7-Jan-14 21:29pm    
Yes, but that won't fix it, he's missing the closing bracket for the switch. Add that to your answer and I'll 5 :)
Garth J Lancaster 7-Jan-14 21:31pm    
heh - was already updating it - but he's got far worse issues with his scheme, passing strings yet switching on char etc - doesn't seem well thought out

thanks Ron :-)
Ron Beyer 7-Jan-14 21:38pm    
5'd... There are a lot of problems here that could be solved by the building-blocks approach. Do a program that gets user input, then do one that switches on that input, then do one that prints the input, etc. Then when all the pieces work, put it together.
There are many problems with your code.

C++
while (true==true)

This is not necessary. A simple while(true) would suffice.

C++
if (KeyStroke = 0)

This is incorrect. You use = instead of ==, so KeyStroke is assigned value 0 instead of being tested against 0. It should be if (KeyStroke == 0).

C++
switch (character)
{
case 'up':

up is a string, not a character, so it should be enclosed in double quotes, i.e. "up". However, in C++ you cannot switch on strings. If you want to compare against strings then you have to use if-else statements. Like this:
C++
if(character == "up") {}
else if(character == "down") {}

However, you can simplify this a lot using just characters such as u,d,r,l (as already suggested). However, I think a better option would be to check directly the keystroke and don't convert it to a string or another char again.
 
Share this answer
 
v2
Comments
Marius Bancila 8-Jan-14 4:48am    
I'd be curious why was this answer downvoted to 1 star. I understand someone is not happy with it, but I'd like to know what's wrong with it.
Member 10506844 8-Jan-14 6:58am    
I'm sorry for all the errors. I'm fourteen and trying to learn programming. When Ron Beyer said " There are a lot of problems here that could be solved by the building-blocks approach. Do a program that gets user input, then do one that switches on that input, then do one that prints the input." I'm not sure I understand what he means. I have a function for each feature in my game. Int Main calls the keystroke function which gets the first user input. Then the charpos_calculation takes that input and calls the printmap function that displays the change based on the user's input. I would like to know what I need to do instead of passing strings and switching on chars. What is the scheme I should be doing? How do I get user input, is this the right way?
Marius Bancila 8-Jan-14 7:23am    
No need to be sorry. We all learn and make mistakes. I don't know exactly what you try to do, however, it makes no sense to transform the key code input into a verbatim string, such as "up" or "left" to check it in another function. Why don't you just pass the KeyStroke to charpos_calculation() and check it against your UP_ARROW value instead of the string "up"?
Member 10506844 8-Jan-14 7:01am    
How would I directly check the user's input?
Stefan_Lang 8-Jan-14 8:07am    
This suggestion refers to the use of getch(), and the value it returns: a single character. Because the original form of the input is of type char, and there is no advantage in converting it into some string or value of another type, you should be using variables of type char to store and pass on this input!

This is a good idea in most cases: when you assign a value of one type to a variable, make sure that the variable has the exact same type! This prevents conversion problems and misunderstandings later in the code.

On another note: do not use global variables if you can avoid them! If a function requires the information stored in a specific variable, then pass that variable as an argument to the function. There are many, many reasons why global variables are bad. Just avoid them. The benefits of knowing exactly what values affect the outcome of a function, of always seeing the variable types in a local context, of being able to call functions using direct values or expressions rather than having to assign them to predefined variables, of being reminded by the compiler when you forget to pass a value rather than wondering at runtime why your function doesn't work as expected - all of this and more easily makes up for the added effort of extending the function parameter list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900