Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 20th, 2006, 9:45 AM   #1
Varrickmana
Newbie
 
Varrickmana's Avatar
 
Join Date: May 2006
Posts: 21
Rep Power: 0 Varrickmana is on a distinguished road
Post Help with a switch

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!!!!!!
Varrickmana is offline   Reply With Quote
Old May 20th, 2006, 10:02 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 20th, 2006, 1:34 PM   #3
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
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
java_roshan is offline   Reply With Quote
Old May 20th, 2006, 4:31 PM   #4
Varrickmana
Newbie
 
Varrickmana's Avatar
 
Join Date: May 2006
Posts: 21
Rep Power: 0 Varrickmana is on a distinguished road
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!!!!!!
Varrickmana is offline   Reply With Quote
Old May 20th, 2006, 5:08 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 20th, 2006, 5:12 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Did you read DaWei's post? Check out strcpy or use C++ strings.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 22nd, 2006, 12:02 PM   #7
Varrickmana
Newbie
 
Varrickmana's Avatar
 
Join Date: May 2006
Posts: 21
Rep Power: 0 Varrickmana is on a distinguished road
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!!!!!!
Varrickmana is offline   Reply With Quote
Old May 22nd, 2006, 12:22 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 22nd, 2006, 7:02 PM   #9
Varrickmana
Newbie
 
Varrickmana's Avatar
 
Join Date: May 2006
Posts: 21
Rep Power: 0 Varrickmana is on a distinguished road
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];
    }
}
This program displays his name. Can you please tell me what he did that I didn't do?
__________________
Kids, every year, more than 400,000 people die from tobacco chewing, smoking, an-
MY GOD, HERE COMES A ZOMBIE PIRATE NINJA!!!!!!
Varrickmana is offline   Reply With Quote
Old May 22nd, 2006, 7:11 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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};
You may write this before your program runs. At compile time. The compiler will fill the array for you.
case 3:
          {playerRace= {'D', 'w', 'a', 'r', 'f', 0};
This is runtime action. The compilation was done 5 minutes ago or last week or last year or whatever. Too fucking late. YOU have to stuff playerRace.
strcpy (playerRace, "Dwarf")
Don't ask me if you're stupid. We told you inattentive. Settle for that. Please.
__________________
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC