switch statement problems..

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1301 Views - Last Post: 25 August 2008 - 01:04 PM Rate Topic: -----

#1 circuspeanuts  Icon User is offline

  • D.I.C Head

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

switch statement problems..

Posted 25 August 2008 - 11:25 AM

I've been able to downgrade from 115 errors to about 13 or 14, and the errors that I'm having are the following..

here's my code:

Bookstore.h
#ifndef BOOKSTORE_H
#define BOOKSTORE_H
#include <string>
#include <iostream>
using namespace std;

class Bookstore
{
private:
	int iPrice;
	int iPurchNum;
	int iTotal;
	
public:
	Bookstore( void );
	~Bookstore();
	void SetPurchaseNum( int iPurchNum );
	int GetPurchaseNum( );
	void SetPrice( int iPrice );
	int GetPrice( );
};
#endif;



FictionBook.h
#ifndef FICTIONBOOK_H
#define FICTIONBOOK_H

#include <string>

using namespace std;


#include "Bookstore.h"

class FictionBook:public Bookstore
{
private:

public:

	void SetBookName( string & n );
	string GetBookName();

};
#endif;


NonFictionBook.h
#ifndef NONFICTIONBOOK_H
#define NONFICTIONBOOK_H


#include <string>

using namespace std;

#include "Bookstore.h"

class NonFictionBook:public Bookstore
{
private:

public:

	void SetBookName( string & n );
	string GetBookName();

};
#endif;


magazine.h
#ifndef MAGAZINE_H
#define MAGAZINE_H


#include <string>

using namespace std;

#include "Bookstore.h"

class Magazine:public Bookstore
{
private:

public:

	void SetMagazineName( string & n );
	string GetmagazineName();

};
#endif;


includes.h
#ifndef INCLUDES_H
#define INCLUDES_H

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

#include "Bookstore.h"
#include "FictionBook.h"
#include "Magazine.h"
#include "NonFictionBook.h"

#endif;


source files...

Main.cpp
#include "Includes.h"

void main ( void );

void main()
{

	string sName;
	int iChoice;

	cout << "Please enter your name: \n";

	cin >> sName;

	cout << endl << "Hello, " << sName << ", and welcome to Land'o'Books!\n\n";

	cout << "What would you like to purchase today?\n\n";

	cout << "\t 1. Fiction Book\n \t 2. NonFiction Book\n \t 3. Magazine\n\n";

	cin >> iChoice;

	switch ( iChoice )
	case 1:
		FictionBook *oFictionBook = new FictionBook;

		FictionBook->SetBookName(int);
		FictionBook->GetBookName();

		Bookstore->SetPurchNum(int);
		Bookstore->GetPurchNum();

		Bookstore->SetPrice(int);
		Bookstore->GetPrice();
		break;
}


Bookstore.cpp
#include "Includes.h"

Bookstore::Bookstore()
{

	cout << "Constructor firing off...\n";

}





and all I have in the other source files (FictionBook.cpp, NonFictionBook.cpp, Magazine.cpp) is the #include "Includes.h"

so here are my errors ( there are 13 of them ) and they're all the same thing...I can't seem to figure out what the problem is, and I know it's going to be something stupid..

Quote

C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(27) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(27) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(28) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(28) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(30) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(30) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(31) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(31) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(33) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(33) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(34) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(34) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(35) : error C2043: illegal break
Error executing cl.exe.



I'm not sure what I'm doing wrong, and when I go to correct it I get more errors. If I take my switch statement out I have no errors.

EDIT: I changed one line of code and fixed most of the errors. the line of code I changed is:

FictionBook *oFictionBook = new FictionBook


I put the * in front of the oFictionBook and it got rid of that problem. Now it tells me that I need to have a semicolon before my arrow ( -> ) operator when I'm declaring my functions and I'm not understanding that...

Is This A Good Question/Topic? 0
  • +

Replies To: switch statement problems..

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: switch statement problems..

Posted 25 August 2008 - 11:33 AM

This could just be my new found C# addiction speaking but:

When you use the new keyword, don't you have to specify the constructor, using brackets?

FictionBook *oFictionBook = new FictionBook();
Was This Post Helpful? 0
  • +
  • -

#3 circuspeanuts  Icon User is offline

  • D.I.C Head

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

Re: switch statement problems..

Posted 25 August 2008 - 11:40 AM

I dont' think you do..
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: switch statement problems..

Posted 25 August 2008 - 11:47 AM

Hold on, i have a long post, need a minute
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: switch statement problems..

Posted 25 August 2008 - 11:56 AM

OK, here it is:

It's tradition to put the * next to the object name rather then the object, but its irrelevant because C++ ignores whitespace. Now onto your errors:

SetBookName takes a parameter of a string reference, but you are passing in an unnamed integer variable;

FictionBook->SetBookName(int);
//definition
 void SetBookName( string & n );



you also have several functions not defined:

 /* Bookstore->SetPurchNum();
        Bookstore->GetPurchNum(); */ //not defined so commented out



i made some adjustments. You need to have constructors and destructors for your objects, especially since you are incorporating pointers. also, you use the pointer name to the object to access stuff with the -> operator. you were using the class name instead.

also you must declare your constructors/destructors to be empty, even if you plan on adding stuff later. The linker needs to know where to look. Having that said, i would put a pointer to the object inside the class rather then creating one inside a switch structure, but I'll leave that up to you. Here is the modified code:

main.cpp
#include "Includes.h"


int main()
{

    string sName, bookName = "A Good Book";
    int iChoice;
	int temp = 3;

    cout << "Please enter your name: \n";

    cin >> sName;

    cout << endl << "Hello, " << sName << ", and welcome to Land'o'Books!\n\n";

    cout << "What would you like to purchase today?\n\n";

    cout << "\t 1. Fiction Book\n \t 2. NonFiction Book\n \t 3. Magazine\n\n";

    cin >> iChoice;

    switch ( iChoice )
	{
    case 1:
        FictionBook* oFictionBook = new FictionBook();
        oFictionBook->SetBookName(bookName);
        cout << oFictionBook->GetBookName() << endl;
       /* Bookstore->SetPurchNum();
        Bookstore->GetPurchNum(); */ //not defined so commented out
        oFictionBook->SetPrice(5) << endl; // must pass correct parameter, let's say 5 is equal to 5 dollars or something
        cout << oFictionBook->GetPrice();
		delete oFictionBook;
        break;
	}//end switch
	return 0;
}



Bookstore.h
#ifndef BOOKSTORE_H
#define BOOKSTORE_H
#include <string>
#include <iostream>
using namespace std;

class Bookstore
{
private:
    int iPrice;
    int iPurchNum;
    int iTotal;
    
public:
	Bookstore() {}; // constructor
	~Bookstore() {}; //destructor
    void SetPurchaseNum( int iPurchNum );
    int GetPurchaseNum( );
	void SetPrice( int num )  {iPrice = num;};
	int GetPrice() {cout << "The price is: "; return iPrice; };
};
#endif;



Fictionbook.h
#ifndef FICTIONBOOK_H
#define FICTIONBOOK_H

#include <string>

using namespace std;


#include "Bookstore.h"

class FictionBook : public Bookstore
{
private:
	string name;
public:
	FictionBook() {}; // empty constructor
	~FictionBook() {}; // destructor
	void SetBookName( string & n ) { name = n;};
	string GetBookName() {cout << "The name of the book is: "; return name;};

};
#endif;



Includes, Magazine and NonFictionBook, I didn't modify so you can later.

Was this post helpful? :) ->
Was This Post Helpful? 1
  • +
  • -

#6 circuspeanuts  Icon User is offline

  • D.I.C Head

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

Re: switch statement problems..

Posted 25 August 2008 - 12:06 PM

that post was helpful, thank you, but now oFictionBook is undeclared?

edit: although name is declared through the french braces, it tells me that it's not declared in my magazine header.

errors:

:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2065: 'name' : undeclared identifier
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocat
or<char> >' (or there is no acceptable conversion)
Main.cpp
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2065: 'name' : undeclared identifier
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocat
or<char> >' (or there is no acceptable conversion)
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(27) : error C2065: 'oFictionBook' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(27) : error C2227: left of '->SetBookName' must point to class/struct/union
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(27) : error C2275: 'string' : illegal use of this type as an expression
		c:\program files\microsoft visual studio\vc98\include\xstring(612) : see declaration of 'string'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(28) : error C2227: left of '->GetBookName' must point to class/struct/union
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(33) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(33) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(34) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(34) : error C2143: syntax error : missing ';' before '->'
C:\Documents and Settings\Administrator\Desktop\Schuth Tyler Final\Main.cpp(35) : error C2043: illegal break
NonFictionBook.cpp
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2065: 'name' : undeclared identifier
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocat
or<char> >' (or there is no acceptable conversion)
FictionBook.cpp
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2065: 'name' : undeclared identifier
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocat
or<char> >' (or there is no acceptable conversion)
Magazine.cpp
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2065: 'name' : undeclared identifier
c:\documents and settings\administrator\desktop\schuth tyler final\magazine.h(17) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocat
or<char> >' (or there is no acceptable conversion)
Error executing cl.exe.

19 error(s), 0 warning(s)



I'm not sure how it's undeclared, but name is...doesn't it declare it for the purpose of the function in the french braces?

This post has been edited by circuspeanuts: 25 August 2008 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: switch statement problems..

Posted 25 August 2008 - 12:12 PM

Shouldn't be, everything compiled and ran when I did it.

What is the exact error?
Was This Post Helpful? 0
  • +
  • -

#8 circuspeanuts  Icon User is offline

  • D.I.C Head

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

Re: switch statement problems..

Posted 25 August 2008 - 12:13 PM

see my above post

EDIT: and main MUST be void, because that's what our instructor wants.

This post has been edited by circuspeanuts: 25 August 2008 - 12:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: switch statement problems..

Posted 25 August 2008 - 12:17 PM

Kick your instructor. Main should always have a return type.
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: switch statement problems..

Posted 25 August 2008 - 12:18 PM

Did you modify anything since my code posting? If so i need to see what you did. you can model the other classes after Fictionbook as a basic idea, but each must have their own implementation. you don't inherit private data members.
Was This Post Helpful? 0
  • +
  • -

#11 circuspeanuts  Icon User is offline

  • D.I.C Head

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

Re: switch statement problems..

Posted 25 August 2008 - 12:22 PM

I figured it out. I didn't declare name in one of my other classes.

ok, but oFictionBook is still undeclared it says...

other than that I haven't changed anything.
Was This Post Helpful? 0
  • +
  • -

#12 circuspeanuts  Icon User is offline

  • D.I.C Head

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

Re: switch statement problems..

Posted 25 August 2008 - 12:35 PM

fixed that too, and now I need a ';' before the ' -> '
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: switch statement problems..

Posted 25 August 2008 - 12:39 PM

What line?

 FictionBook* oFictionBook = new FictionBook();
        oFictionBook->SetBookName(bookName);
        cout << oFictionBook->GetBookName() << endl;
       /* Bookstore->SetPurchNum();
        Bookstore->GetPurchNum(); */ //not defined so commented out
        oFictionBook->SetPrice(5); // must pass correct parameter, let's say 5 is equal to 5 dollars or something
        cout << oFictionBook->GetPrice();
		delete oFictionBook;
        break;



works. the pointer is initialized to a new FictionBook object and various transactions take place.

edited for a typo <<endl; shouldn't have been in there

This post has been edited by KYA: 25 August 2008 - 12:43 PM

Was This Post Helpful? 0
  • +
  • -

#14 circuspeanuts  Icon User is offline

  • D.I.C Head

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

Re: switch statement problems..

Posted 25 August 2008 - 12:59 PM

no no that works. lol it tells me that I need a semicolon before my arrow operator..
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: switch statement problems..

Posted 25 August 2008 - 01:02 PM

In main.cpp?

Are you missing a semicolon somewhere? :P
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2