Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,651 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,156 people online right now. Registration is fast and FREE... Join Now!




if or switch statement?

 
Reply to this topicStart new topic

if or switch statement?, declare class objects...

circuspeanuts
post 19 Aug, 2008 - 08:37 PM
Post #1


D.I.C Head

**
Joined: 11 Apr, 2008
Posts: 68

ok so my last issue was solved, and because there's so much code posted in that topic, I figured I'd start a new one with fresh, up-to-date code for you guys so yall wouldn't get confused.

ok, so my updated code is as follows:

Bookstore.h
CODE

#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 & nr );
    int GetPurchaseNum( );
    void SetPrice( int & p );
    int GetPrice( );
};
#endif;


FictionBook.h
CODE

#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
CODE

#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
CODE

#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
CODE

#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;


and my cpp files.

Main.cpp
CODE

#include "Includes.h"

void main ( void );

void main()
{

    string sName;
    string sBookName;


    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 \t 2. NonFiction Book \t 3. Magazine\n";

    cin.get();
}


Bookstore.cpp
CODE

#include "Includes.h"

Bookstore::Bookstore()
{

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

}



ok, so what I'm thinking is I want to use a switch case. In other words, after this bit of code:
CODE

    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";


I want to go something like...


CODE

    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";

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

        FictionBook::SetBookName( string & n );
        FictionBook::GetBookName();

        FictionBook::SetPurchNum( int & nr );
        FictionBook::GetPurchNum();

        FictionBook::SetPrice(  int & p );
        FictionBook::GetPrice();
break;

case 2:

//Same thing for NonFictionBook;

break;

case 3:

//Same thing for Magazine;

break;




would something like that work?


EDIT: I just noticed I double posted a new topic. I didn't mean to do that. Sorry.

EDIT EDIT: such and such variables like 'n' and whatnot are undeclared. I was unsure exactly what those variables are supposed to be?

This post has been edited by circuspeanuts: 19 Aug, 2008 - 08:51 PM
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 20 Aug, 2008 - 04:41 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


Yes that would work other than you will have to declare various variables (such as iChoice) but it sounds like you are wanting to get user input for that so a cin will work.
User is offlineProfile CardPM

Go to the top of the page

circuspeanuts
post 20 Aug, 2008 - 07:33 AM
Post #3


D.I.C Head

**
Joined: 11 Apr, 2008
Posts: 68

yeah so I'll have...

CODE

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( string & n );
        FictionBook::GetBookName();

        FictionBook::SetPurchNum( int & nr );
        FictionBook::GetPurchNum();

        FictionBook::SetPrice(  int & p );
        FictionBook::GetPrice();



so I would have to set it to something along the lines of:

CODE

switch ( iChoice )
case 1://put something along the lines of "1" or something so that the switch case recognizes the user's decision?


and umm...I have to ask public account what variables he put in place of n, nr, and p because I'm not entirely sure where they come into play. I could pass the variable as
CODE


        FictionBook::SetBookName( string sName );

        FictionBook::SetPurchNum( int iPurchNum);

        FictionBook::SetPrice( int iPrice )


or something like that..
User is offlineProfile CardPM

Go to the top of the page

typedef
post 20 Aug, 2008 - 08:28 AM
Post #4


New D.I.C Head

*
Joined: 20 Aug, 2008
Posts: 1


My Contributions


I'm not sure what you're trying to do in that switch statement but you're not using the pointer oFictionBook.

CODE
oFictionBook->SetBookName("x");
oFictionBook->SetPurchNum(0);
oFictionBook->SetPrice(0);
User is offlineProfile CardPM

Go to the top of the page

circuspeanuts
post 21 Aug, 2008 - 06:53 PM
Post #5


D.I.C Head

**
Joined: 11 Apr, 2008
Posts: 68

QUOTE(typedef @ 20 Aug, 2008 - 09:28 AM) *

I'm not sure what you're trying to do in that switch statement but you're not using the pointer oFictionBook.

CODE
oFictionBook->SetBookName("x");
oFictionBook->SetPurchNum(0);
oFictionBook->SetPrice(0);


typedef, what I mean is basically this:

I want the user to choose what they want (ie nonfiction book, fiction book, or magazine) and then I want it to create an object of that class and run those function from that class.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:18AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month