14 Replies - 1684 Views - Last Post: 21 July 2008 - 11:37 AM Rate Topic: -----

#1 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Final Project for my int c++ class

Post icon  Posted 21 July 2008 - 09:20 AM

ok, so I've got a final project that I need to make for my class, and i want to do it in the form of a story (ie charname, charclass, chartype, etc.

#ifndef MyStory_H
#define MyStory_H

class MyStory
{
	public:
		MyStory();
		~MyStory();
		void SetCharClass(string sCharClass);
		void GetCharClass(string sCharClass);

	private:
		string sCharType[50];
		string sCharName[50];
		string sCharClass[50];

};

#endif
;



^^ that's my header, and below is my source file so far...

#include <iostream>
#include "MyStory.h"
#include <string>
using namespace std;


MyStory::MyStory()
{ 
	cout<<"MyStory constructor firing off....\n";
}

MyStory::~MyStory()
{
	cout<<"Goodbye....\n";
}

	
	void MyStory::SetCharClass(sCharClass)
{
		cout << "Please choose a class:\n";
		cin >> CharClass;

}

	void MyStory::GetCharClass(sCharClass)
{

	cout << "Your chosen Class is: " << CharClass << ".";
}



I get problems with my strings because it doesn't recognize them, although this is how I was accustomed to doing them...any help with this? I have a feeling because I don't understand this at all that well that I'm going to be on here for a step by step walkthrough of this project. Really appreciated. Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Final Project for my int c++ class

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Final Project for my int c++ class

Posted 21 July 2008 - 09:39 AM

Few things first off. It is common practice to use all capital letters when using macros, i.e.:

#ifndef MYSTORY_H
#define MYSTORY_H



Also, your .h file doesn't recognize strings because it does not have access to the string library. Try putting #include <string> inside your header file and removing it from your cpp file.
Was This Post Helpful? 1
  • +
  • -

#3 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: Final Project for my int c++ class

Posted 21 July 2008 - 09:43 AM

Ok so I changed it and I've got 23 errors now:


c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(14) : error C2146: syntax error : missing ';' before identifier 'sCharType'
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(14) : error C2501: 'string' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(14) : error C2501: 'sCharType' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(15) : error C2146: syntax error : missing ';' before identifier 'sCharName'
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(15) : error C2501: 'string' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(15) : error C2501: 'sCharName' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(16) : error C2146: syntax error : missing ';' before identifier 'sClass'
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(16) : error C2501: 'string' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(16) : error C2501: 'sClass' : missing storage-class or type specifiers
Main.cpp
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(14) : error C2146: syntax error : missing ';' before identifier 'sCharType'
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(14) : error C2501: 'string' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(14) : error C2501: 'sCharType' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(15) : error C2146: syntax error : missing ';' before identifier 'sCharName'
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(15) : error C2501: 'string' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(15) : error C2501: 'sCharName' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(16) : error C2146: syntax error : missing ';' before identifier 'sClass'
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(16) : error C2501: 'string' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\mystory.h(16) : error C2501: 'sClass' : missing storage-class or type specifiers
c:\documents and settings\05312560\desktop\schuth tyler final\main.cpp(9) : error C2182: 'main' : illegal use of type 'void'
c:\documents and settings\05312560\desktop\schuth tyler final\main.cpp(9) : error C2373: 'main' : redefinition; different type modifiers
		c:\documents and settings\05312560\desktop\schuth tyler final\main.cpp(6) : see declaration of 'main'
c:\documents and settings\05312560\desktop\schuth tyler final\main.cpp(9) : error C2239: unexpected token '{' following declaration of 'main'
c:\documents and settings\05312560\desktop\schuth tyler final\main.cpp(10) : error C2017: illegal escape sequence
c:\documents and settings\05312560\desktop\schuth tyler final\main.cpp(10) : error C2001: newline in constant
Error executing cl.exe.

Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Final Project for my int c++ class

Posted 21 July 2008 - 09:47 AM

You need to pick whether you want to use c style "strings" or C++ strings. The former is an array of chars and the latter is simply a string object, not an array of string objects, ex:

char cString[11] = "Hello World";
string cPlusString = "Something";


Was This Post Helpful? 0
  • +
  • -

#5 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: Final Project for my int c++ class

Posted 21 July 2008 - 09:51 AM

well if I implement char sCharType [51] in my header file, so that it allows 50 character for the user to choose a class (ie paladin, priest, etc.), and I try to pass it over to my SetCharClass and GetCharClass functions, it doesn't recognize that I'm passing it over. I've started a main.cpp in which I have a voided main (as per our instructors directions, and I have a cout in the beginning welcoming the user, and I get as well an error that claims it's returning some sort of value, but I can't find a way around it.
Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Final Project for my int c++ class

Posted 21 July 2008 - 09:54 AM

Posting all of your code would be enormously helpful.

Quote

well if I implement char sCharType [51] in my header file, so that it allows 50 character for the user to choose a class (ie paladin, priest, etc.)


I misread what your code was trying to implement. The array of chars/strings is empty. How does it help determine what a player selected? (Unless you want to fill it with character data?)

Passing arrays can be tricvky, but since its one dimensional all you have to do is put it as a parameter; you don't even have to declare its size or such in passing.
Was This Post Helpful? 0
  • +
  • -

#7 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: Final Project for my int c++ class

Posted 21 July 2008 - 10:06 AM

resolved: Thanks :D I'll post more WHEN (not if) I run into problems :D:D:D
Was This Post Helpful? 0
  • +
  • -

#8 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: Final Project for my int c++ class

Posted 21 July 2008 - 10:17 AM

ok, so I've done that, and now I want to call my function SetCharClass and GetCharClass in main. I know it's noobish, and the book we're using is way to advanced, so I'm going to be relying on you guys to help me out here (and I've gone through some of the tutorials, and I've printed off the ref sheet (it doesn't go over how to call functions, etc.)). Also, I'd like to make a SetCharType and GetCharType function with switch cases.
Was This Post Helpful? 0
  • +
  • -

#9 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Final Project for my int c++ class

Posted 21 July 2008 - 10:18 AM

Create a new object of MyStory within main. Call functions off of this object to do whatever it is you need to do.
Was This Post Helpful? 0
  • +
  • -

#10 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: Final Project for my int c++ class

Posted 21 July 2008 - 10:33 AM

umm...example...? hmmm something like

MyStory oMyStory
{ 
		  SetCharClass(string);
		  GetCharClass(string);
}



?

This post has been edited by circuspeanuts: 21 July 2008 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#11 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Final Project for my int c++ class

Posted 21 July 2008 - 10:52 AM

Something like this if I'm reading correctly:

int main()
{
     MyStory theStory; //object creation
     theStory.functionName; //call a function to do stuff
     //other stuff
}


Was This Post Helpful? 0
  • +
  • -

#12 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: Final Project for my int c++ class

Posted 21 July 2008 - 10:57 AM

so can I use the arrows operator then? like:

oMyStory->SetCharClass;
oMyStory->GetCharClass;



nvm that didn't work, but I get a missing argument error when I do that take your suggestion

This post has been edited by circuspeanuts: 21 July 2008 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#13 polymath   User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Final Project for my int c++ class

Posted 21 July 2008 - 11:12 AM

Use arrow operators when you make a pointer to classes, :: operators when you are using static members of classes (or are declaring functions outside of a class), and the dot operator when you just need plain old member access. Example:

#include <iostream>
#include <string>
using namespace std;

class HelloWorld {
   public:
      static void printmsg();
      static string msg;
      string usable;
}

void HelloWorld::printmsg() {
   msg="Hello World!\n";
   cout << msg;
};

int main() {
   HelloWorld regular;
   HelloWorld * pObj;
   regular.usable="How Are You?";
   pObj->usable="Fine thanks.";
   HelloWorld::printmsg();
   cout << regular.usable << endl << pObj->usable << "\nPress enter to exit. ";
   cin.get();
   return 0;
   delete pObj;
}


EDIT: Whoops, I forgot to allocate memory. Oh well, you get the idea.

This post has been edited by polymath: 21 July 2008 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#14 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Final Project for my int c++ class

Posted 21 July 2008 - 11:13 AM

-> is used for pointers. You could create a pointer to your class object, but at this point I think it will over complicate things.

Argument missing--you need to use whatever parameters you have set for those functions:

theStory.functionName(parameters);



edit: meh poly beat me to most of it

This post has been edited by KYA: 21 July 2008 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#15 circuspeanuts   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: Final Project for my int c++ class

Posted 21 July 2008 - 11:37 AM

resolved! Thanks guys! your help is much appreciated. Throughout the course of this final project I've got to implement the 5 OOP so that'll be fun. I struggle the most with Inheritance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1