![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 21
Rep Power: 0
![]() |
I am making a text-based rpg and I am having some trouble with this switch. It says that what I want to change playerRace to is incompatable(ex."Human", but I just can't figure out what to do. Can someone please help me? Here is my code:
Note: At the end it just sorts of drifts off after it tells about the quest. That is OK, as I have just started making the game. I just need help with the switch. #include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to Emerald.\n";
cout << "Programmed by David Gay\n\n\n";
char playerName[70];
char playerRace[70];
char playerProfession[180];
cout << "What is your name, hero?\n >" ;
cin >> playerName;
cout << "Please choose a race.\n";
cout << "1)Human\n 2)Elf\n 3)Dwarf\n 4)Half-Orc\n";
int raceNumber;
cin >> raceNumber;
switch(raceNumber)
{
case 1:
playerRace="Human";
cout << "Your race is a Human.\n";
break;
case 2:
playerRace="Elf";
cout << "Your race is an Elf.\n";
break;
case 3:
playerRace="Dwarf";
cout << "Your race is a Dwarf.\n";
break;
case 4:
playerRace="Half-Orc";
cout << "Your race is a Half-Orc.\n";
break;
default:
cout << "That is not an option.\n";
break;
}
cout << "Welcome to Emerald," << playerName << ". Your adventure begins.\n" ;
cout << "Your first quest is to kill a thief. He can be found in the next town over.\n You may want to rest and purchace items before you leave.\n";
system("PAUSE");
return 0;
}
__________________
Kids, every year, more than 400,000 people die from tobacco chewing, smoking, an- MY GOD, HERE COMES A ZOMBIE PIRATE NINJA!!!!!! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
PlayerRace is a char array. You cannot assign to arrays in their entirety -- you have to do it piecemeal. The construct, myArray = "DangMe"; that you see in many programs, near the beginning, is a favor from the compiler that is performed at compile time. Behind that is real, valid code. Investigate things like "strcpy" and its family, or move to C++ strings, which handle it for you (again, behind the scenes). You might also want to experiment with what your code does if the user puts in "ZY" instead of a valid number. You can't control your user, you can only control the effects of his/her actions. Read about how input streams fail, how to detect it, and think about what to do about it. It's a mark of learning to be a real programmer.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
You can follow the link below, that would help you understand better, how you could make things work in your switch case!
http://www.functionx.com/visualc/Lesson18.htm
__________________
Visit: http://www.somaiya.edu |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2006
Posts: 21
Rep Power: 0
![]() |
I'm still getting a parse error on a couple lines of code, like this one: playerRace= {'H', 'u', 'm', 'a', 'n', 0}; and this one:
playerRace= {'E', 'e', 'f', 0}; If someone could just tell me what is wrong with this code, that would be awesome. #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <strings.h>
using namespace std;
int main()
{
cout << "Welcome to Emerald.\n";
cout << "Programmed by David Gay\n\n\n";
char playerName[70];
char playerRace[70];
char playerProfession[180];
cout << "What is your name, hero?\n >" ;
cin >> playerName;
cout << "Please choose a race.\n";
cout << "1)Human\n 2)Elf\n 3)Dwarf\n 4)Half-Orc\n";
int raceNumber;
cin >> raceNumber;
switch(raceNumber)
{
case 1:
playerRace= {'H', 'u', 'm', 'a', 'n', 0};
cout << "Your race is a Human.\n";
break;
case 2:
playerRace= {'E', 'e', 'f', 0};
cout << "Your race is an Elf.\n";
break;
case 3:
{playerRace= {'D', 'w', 'a', 'r', 'f', 0};
cout << "Your race is a Dwarf.\n";
break;}
case 4:
playerRace= {'H', 'a', 'l', 'f', '-', 'O', 'r', 'c', 0};
cout << "Your race is a Half-Orc.\n";
break;
default:
cout << "That is not an option.\n";
break;
}
cout << "Welcome to Emerald, " << playerName << ". Your adventure begins.\n" ;
cout << "Your first quest is to kill a thief. He can be found in the next town over.\n You may want to rest and purchace items before you leave.\n";
system("PAUSE");
return 0;
}
__________________
Kids, every year, more than 400,000 people die from tobacco chewing, smoking, an- MY GOD, HERE COMES A ZOMBIE PIRATE NINJA!!!!!! |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You are going to have to distinguish between what the compiler does for you when you write the code and what you can do to get runtime action. I swear, it is not your fault, the compiler and its writers spoil us. Nevertheless, you can either learn what is what or just give it up. First thing you do, is you go read the material on pointers in my signature. It isn't about this stuff, but it gets mentioned in passing. The second thing you do is go back and read your basic material on how to write this language, and pay close attention. Far attention won't hack it. Just in passing: FILL IN YOUR DAMNDED ARRAY BYTE BY BYTE, DON'T ASK A FAERIE TO DO IT FOR YOU.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Did you read DaWei's post? Check out strcpy or use C++ strings.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: May 2006
Posts: 21
Rep Power: 0
![]() |
I'm sorry for bugging you. I'll look at it some more... Maybe read a couple more books...
__________________
Kids, every year, more than 400,000 people die from tobacco chewing, smoking, an- MY GOD, HERE COMES A ZOMBIE PIRATE NINJA!!!!!! |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It works like this. An array is a sequence of memory elements. The processor can only address them element by element (of a size depending upon the physical implementation), it can't dump a truckload of stuff in one swell foop. A lower level language has to deal with this. For higher levels, someone deals with it, wraps it in a package, and lets you order the package with an "=" sign. YOU have to accomodate the language or method you are faced with not the inverse. You don't get accommodated without moving up and using someone else's goodies, whether you realize there's a crankshaft under the hood, or not.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Newbie
Join Date: May 2006
Posts: 21
Rep Power: 0
![]() |
Man, I must be freaking stupid, but I don't get what the heck is wrong with my switch. I decared each variable in the array, didn't I?? I used the ' and the ,... Look, I have this program I got off the internet that sets a character array and it compiles fine. He puts the chars in the array just like I did, didn't he??? Here is his program:
// DisplayString - output a character array to
// standard output, the MS-DOS window
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// prototype declarations
void displayString(char stringArray[]);
int main(int nNumberofArgs, char* pszArgs[])
{
char charMyName[] =
{'S', 't', 'e', 'p', 'h', 'e', 'n', 0};
displayString(charMyName);
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
// displayString - display a character string
// one character at a time
void displayString(char stringArray[])
{
for(int i = 0; stringArray[i] != '\0'; i++)
{
cout << stringArray[i];
}
}
__________________
Kids, every year, more than 400,000 people die from tobacco chewing, smoking, an- MY GOD, HERE COMES A ZOMBIE PIRATE NINJA!!!!!! |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Why are you showing us his program? Does he need it fixed? You aren't paying attention.
char charMyName[] =
{'S', 't', 'e', 'p', 'h', 'e', 'n', 0};case 3:
{playerRace= {'D', 'w', 'a', 'r', 'f', 0};strcpy (playerRace, "Dwarf")
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|