# Whats wrong with my short C++ thing?



## Mac Osxtopus (Jun 5, 2002)

I said thing because its not an app yet. I was making a basic text-based RPG with C++ to see if I could do some basic multi-choice sort of stuff. I tried to put like choosing characters and such, but it would keep getting parse errors and stuff from project builder:

#include iostream
main()
{ 
 int a, b, c;
 a = 1;
 b = 2;
 c = 3;
 cout << "Welcome To My Experimental RPG!, choose a character class: ";
 cout << "1: Knight, 2: Wizard, or 3, Conjurer!";
 {
 if cin >> 1;
 cout << "you chose Knight";
 }
 {
 else
 cin >> 2;
 cout << "You chose wizard";
 }
 {
 else 
 cin >> 3;
 cout << "You chose conjurer";
 }
 return 0;
 }


----------



## Red Phoenix (Jun 5, 2002)

You might consider

---------

#include<iostream>
using namespace std;

main()
{
	int a = 1, b = 2, c = 3;
	char the_class;

	cout << "Welcome To My Experimental RPG!, choose a character class: ";
	cout << "1: Knight, 2: Wizard, or 3: Conjurer!";

	cin >> the_class;
	if (the_class == '1')
	{
		cout << "you chose Knight\n";
	}
	else if (the_class == '2')
	{
		cout << "You chose wizard\n";
	}
	else if (the_class == '3')
	{
		cout << "You chose conjurer\n";
	}

	return 0;
}

---------

Of course, this is very rudimentary and looks pretty ugly in the output. But it compiles and runs.


----------



## Mac Osxtopus (Jun 6, 2002)

thanks, now i plan to make lots of functions such as this for multiple choice sort of things, so i know people im going to test this on will want to be able to save....so how do i make it so people can save their progress?

Oh, and how can i make it so i can use 10 choices? Cuz i want lots of choices for my RPG


----------



## Red Phoenix (Jun 7, 2002)

My C++ class didn't cover file i/o, so someone will have to help you there. As for many choices, you might try a cases argument.


----------



## Mac Osxtopus (Jun 7, 2002)

mm...cases arguement? is that the thing with the switch command? my book im reading to learn the basics of c++ C++ From The Ground Up, By Herbert Shildt, learn C++ from the "master"  seems to always have the answer the next day i read it.


----------



## Red Phoenix (Jun 8, 2002)

Yes, that's the one.


----------



## Mac Osxtopus (Jun 8, 2002)

Ok i changed it so i used the switch command and it works fine, i use 10 different choices and a default that says you cant use letters. I tried to turn my local variable 'caste' into a global one, but project builder always gives me an error when i do. I want to be able to expand the RPG into something else besides choosing characters, how can i make it so the program "remembers" what caste/character the user chose? How do i make it so they can have a custom name? I'm sorry that i'm asking a lot of questions, im a real curious guy  .


----------



## Gwailo (Jun 8, 2002)

Am I the only one who uses _short_ or _char_ to conserve memory when the only variable you're entering is one character?


----------



## Mac Osxtopus (Jun 9, 2002)

no, i changed it to char. How do i make it so my default in the switch arguement says "enter a number, everything else is void" then it goes back to the text about character choosing.


----------



## nkuvu (Jun 9, 2002)

Put the whole shebang in a while loop.

Pseudo code (I've been programming in Perl a lot, so double check syntax before you use this code):

```
while (valid_input == false)
{
  cout << "Enter your class: ";
  cin >> classname;
  switch (classname)
  {
    case 'A':
      the_class = "Argonaut";
      valid_input = true;
      break;
    // More stuff here...  make sure to set valid_input to true
    // for any valid character class
    default:
      cout << "That was not a valid choice.\n";
      // Make it user friendly
      cout << "Valid choices are A, B, C, D, (whatever)\n";
  } // end switch
} // end while loop
```

Should do the trick...


----------



## nkuvu (Jun 9, 2002)

For file IO, use ifstream and ofstream data types.


```
// I think it's fstream, can't 'member for sure
#include < fstream >

int main()
{
  ifstream input_file;
  ofstream output_file;
  int stuff;

  // All of my references are at work and I'm kinda fuzzy headed
  // right now...
  input_file.open("Some arguments here");
  output_file.open("I hate forgetting things");

  // Read
  input_file >> stuff;

  // Write
  output_file << stuff;

  return 0;
} // end main
```


----------



## iMan (Jun 9, 2002)

Hi Mac Osxtopus

What compiler are you using to program in? I'm new to this. trying to learn C++ and I have never programmed before.
I got an O´reilly book but it's not for mac (se this post for mor info) 

I'm stuck and I haven't started yet...  

Would be greatfull for a bit of help.

Thanks

Viktor


----------



## Mac Osxtopus (Jun 9, 2002)

based on public opinion at his web site...oreilly SUCKS for beginners like me and you. I'm learning from a book called C++ From The Ground Up, By Herbert Shildt. It's quite beginner friendly. I use project builder ( mac os x developer tools) to write and compile my C++ things.


----------



## nkuvu (Jun 9, 2002)

O'Reilly is fantastic for beginners.  Just try the "Learning" books instead of the "Programming" books.

For example, "Learning Perl" gets you up to speed on how to program in Perl.  "Programming Perl" offers a _brief_ refresher, then goes way past the "Learning" book.


----------



## iMan (Jun 10, 2002)

Ops.. Guess I got the wrong book.....

It's a jungle of books, it's hard to know what to buy... I bought my book in the states, Here in italy ,where I live for now, it's hard to find any good books in english.
I'll see if I can pick upp a diffrent more basic book.


----------



## Mac Osxtopus (Jun 10, 2002)

try ordering one online, like from amazon.com


----------

