2 Replies - 299 Views - Last Post: 08 April 2012 - 10:53 AM Rate Topic: -----

#1 khyowl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-April 12

Error with Declaring undefined size arrays as a class member

Posted 08 April 2012 - 10:31 AM

// practice.cpp : main project file.

#include "stdafx.h"
#include<iostream>
#include<string.h>
#include "conio.h"
#include<stdlib.h>
#include <windows.h>
#include<time.h>
using namespace std;

class www{
public:
	www();
	void FillTopInfo();
	void ArrangeTop();
	void DisplayTop();
	void getinfo();
	void info_lives_money();
	void cshilm();
	void instruction();	
	void header();
	void credits();
	char c; //for menu
	void menu();
	void pop(){top--;}
	void selans(int x);
	void correct(int x);
	void incorrect(int x);
	void easy();
	void average();
	void hard();
	void jackpot();
	char opt; //for jackpot option function
	void jackpot_opt();
	void game();
		
		struct StudentInfo{
        char Name[50];
        char StudNo[50];
        char Course[50];
        double Money;
}s;

struct StudentInfo T1,T2,T3,T4,T5,V;
char ans[11];
double aMoney;
int perfect;
static const char answer[101];
static const char Answer[101];
int top;
static char dlife[];

};

const char www::answer[101]={0,'a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d'};

const char www::Answer[101]={0,'A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D'};

char www::dlife[]={'@','@','@','@','@'};

void www:: FillTopInfo(){
StudentInfo V={"(Name)","(Student No.)","(Course)",0};
T1=V;
T2=V;
T3=V;
T4=V;
T5=V;
}

//Call it to arrange the TOP PLAYERS
void www::ArrangeTop(){
if(T1.Money<s.Money)
{        T5=T4;
        T4=T3;
        T3=T2;
        T2=T1;
        T1=s;
}
else if(T2.Money<s.Money)
{
        T5=T4;
        T4=T3;
        T3=T2;
        T2=s;
}
else if(T3.Money<s.Money)
{
        T5=T4;
        T4=T3;
        T3=s;
}
else if(T4.Money<s.Money)
{
        T5=T4;
        T4=s;
}
else if(T5.Money<s.Money)
{
        T5=s;
}
}


//Will Output the Top Players of the game(NOT CALLED IN THE PROGRAM YET)
void www::DisplayTop(){
system ("cls");
header();
cout<<"Name";  cout<<"Course"; cout<<"Student No.";  cout<<"Money";	
cout<<T1.Name;		cout<<T1.Course; 
cout<<T1.StudNo;    cout<<"P"<<T1.Money;
cout<<T2.Name;		cout<<T2.Course; 
cout<<T2.StudNo;	cout<<"P"<<T2.Money;
cout<<T3.Name;		cout<<T3.Course; 
cout<<T3.StudNo;	cout<<"P"<<T3.Money;
cout<<T4.Name;		cout<<T4.Course; 
cout<<T4.StudNo;	cout<<"P"<<T4.Money;
cout<<T5.Name;		cout<<T5.Course; 
cout<<T5.StudNo;	cout<<"P"<<T5.Money;
cout<<"\n\n\n\n\n\n\n\n Press ANY KEY to continue.";
getch();
}




//get information of the student
void www:: getinfo(){
cout<<"\nPlease enter the follwing information to start playing.\n"<<endl;
cout<<"\n Name: ";
cin.getline(s.Name,50);
cin.getline(s.Name,50);
cout<<"\n Student Number: ";
cin.getline(s.StudNo,50);
cout<<"\n Course: ";
cin.getline(s.Course,50);
}


void www::info_lives_money(){
cout<<" Name: "<<s.Name;
cout<<"Lives: ";
for(int i=0; i<top; i++)
cout<<dlife[i]<<" ";
cout<<"\n StNo: "<<s.StudNo;
cout<<"Money: P"<<s.Money;
cout<<"\n********************************************************************************";
}

//does clear screen, then outputs header and info_lives_money stats 
void www::cshilm(){
system ("cls");
header();
info_lives_money();
}

//game instructions
void www::instruction(){
cshilm();
cout<<"\nGENERAL DIRECTIONS:";
cout<<"\n\n Hello "<<s.Name<<"!";

cout<<"\n You have the chance to win P2,000,000! You are given 5 questions in easy";
cout<<"\n round, 3 questions in the average round, 2 questions in the difficult round,";
cout<<"\n and 1 question the for the jackpot round. Answering all the questions";
cout<<"\n correctly from easy to difficult round will give you P1,000,000.";
cout<<"\n The jackpot round costs another P1,000,000 giving you a chance to take home";
cout<<"\n P2,000,000! You are given 5 lives and each wrong answer will cost you 1 life";
cout<<"\n in each round. When your life is already depleted, the game is over.";
cout<<"\n Entering any character besides from the choices is considered wrong.";
cout<<"\n\n\n\nPress ANY KEY to proceed to easy round.";
getch();
}


//header function [menu]
void www::header(){
cout<<"********************************************************************************";
cout<<"WHO WANTS TO BE A WINNER		";

cout<<"********************************************************************************";
}

//credits function [the programmers]
void www::credits(){
system ("cls");
header();
//[group member goes here]
getch();
system ("cls");
menu();
}

void www::menu()
{
system ("cls");
header();
cout<<"\n\t\t      [1] PLAY GAME            [3] CREDITS\n"<<endl;
cout<<"\n\t\t      [2] HIGH SCORE           [4] EXIT\n"<<endl;
cout<<"\n\t\t               Enter your choice: ";
cin>>c;
}


//selecting answer
void www::selans(int x){
cout<<"\n Answer: ";
cin>>ans;
if(answer[x]==ans[0]||Answer[x]==ans[0])
correct(x);
else
incorrect(x);
}

void www::correct(int x){
cout<<"The correct answer is: "<<Answer[x]<<" / "<<answer[x]<<endl;
s.Money=s.Money+aMoney;
perfect++;
if(perfect==10){
        s.Money=1000000;
        cout<<"********************************************************************************";
        cout<<"Congratulations! You've got the correct answer!\nYou have now 1000000 for answering the 10 questions correctly!"<<endl;
        cout<<"Press ANY KEY to continue"<<endl;
        cout<<"********************************************************************************"<<endl;}
else{
cout<<"Congratulations! You've got the correct answer!"<<endl;
cout<<"You have now a total of P"<<s.Money<<endl;
cout<<"Press ANY KEY to continue.";}
getch();
}

//for every wrong answers
void www:: incorrect(int x){
pop();
cout<<"The correct answer is: "<<Answer[x]<<" / "<<answer[x]<<endl;
if(top<=0){
        cout<<"Sorry, your answer is incorrect"<<endl;
        cout<<"********************************************************************************";
        cout<<"You lost all your lives. The game is over. Thank you for playing!\nYou will take home P"<<s.Money<<"!"<<endl;
        cout<<"********************************************************************************"<<endl;
        getch();
        ArrangeTop();
        menu();}
else
        cout<<"Sorry, you're answer is incorrect."<<endl;
        cout<<"You lost a life."<<endl;
        cout<<"Pres ANY KEY to continue.";
getch();
}

//easy round questions!!!!!
void www::easy(){
cshilm();
srand(time(0));
int ran=rand()%8+1;
//question 1-------------------------
cout<<"\nEasy Round | Question 1 of 5:\n\n";
switch(ran){
        case 1-8}
selans(ran);
ran=rand()%8+9;
cshilm();
//question 2-------------------------
cout<<"\nEasy Round | Question 2 of 5:\n\n";
switch(ran){
        case 9-16}
selans(ran);
ran=rand()%8+17;
cshilm();
//question 3-------------------------
cout<<"\nEasy Round | Question 3 of 5:\n\n";
switch(ran){
        case 17-24}
selans(ran);
ran=rand()%8+25;
cshilm();
//question 4-------------------------
cout<<"\nEasy Round | Question 4 of 5:\n\n";
switch(ran){
        case 25-32}
selans(ran);
ran=rand()%8+33;
cshilm();
//question 5-------------------------
cout<<"\nEasy Round | Question 5 of 5:\n\n";
switch(ran){
        case 33-40}
selans(ran);
cshilm();
}


//average round!!!!!! questions number 41-70
void www::average(){
aMoney=100000;
cshilm();
srand(time(0));
int ran=rand()%10+41;
//question 1-------------------------
cout<<"\nAverage Round | Question 1 of 3:\n\n";
switch(ran){
        case 41-50}
selans(ran);
ran=rand()%10+51;
cshilm();
//question 2-------------------------
cout<<"\nAverage Round | Question 2 of 3:\n\n";
switch(ran){
        case 51-70}
selans(ran);
}


//hard round!!!!!!!!!! questions 71-90
void www::hard(){
aMoney=150000;
cshilm();
srand(time(0));
int ran=rand()%10+71;
//question 1-------------------------
cout<<"\nDifficult Round | Question 1 of 2:\n\n";
switch(ran){
        case 71-80}
selans(ran);
ran=rand()%10+81;
cshilm();
//question 2-------------------------
cout<<"\nDifficult Round | Question 2 of 2:\n\n";
switch(ran){
        case 81-90}
selans(ran);
}


//jackpot round!!!!!!!!!!!!!   questions 91-100
void www::jackpot(){
cshilm();
srand(time(0));
int ran=rand()%10+91;
cout<<"\n 1 Million Round Question:\n\n";
switch(ran){
        case 91 - 100}

cout<<"\n Answer: ";
cin>>ans;
cout<<"The correct answer is: "<<Answer[ran]<<" / "<<answer[ran]<<endl;
if(answer[ran]==ans[0]||Answer[ran]==ans[0])
{  s.Money=s.Money+1000000;
        

cout<<"********************************************************************************";
cout<<"Congratulations! You've got the correct answer!"<<endl;
cout<<"You will take home P"<<s.Money<<"! You're now a millionare!"<<endl;
cout<<"Thank you for playing "<<endl;
cout<<"********************************************************************************";}
else{
        s.Money=s.Money-200000;
        

cout<<"\n********************************************************************************";
cout<<"Sorry, your answer is incorrect."<<endl;
cout<<"The good news is you will take home P"<<s.Money<<"!"<<endl;
cout<<"Thank you for playing "<<endl;
cout<<"********************************************************************************";}
}

//option to procede to jackpot round!
void www::jackpot_opt(){
do{
cshilm();
cout<<"\nYou have already finished the easy, average and difficult round!";
cout<<"\nYou are now given the choice to continue or not to the jackpot round.";
cout<<"\nIf you answer the question correctly you will take home P1000000 plus your current balance but if not, P200,000 will be deducted from your current money.";
cout<<"\n\nDo you want to proceed to the JACKPOT ROUND [Y|N]? ";
cin>>opt;
switch(opt){
case'Y':case'y':jackpot();
        break;
case'N':case'n':
        

cout<<"\n********************************************************************************";
        cout<<"Thank you for playing"<<endl;
        cout<<"Congratulations! You will take home P"<<s.Money<<""<<endl;
        

cout<<"********************************************************************************";
        break;
default:
        cout<<"\nYou have entered an invalid Input!";
}
}while(opt!='Y'&&opt!='y'&&opt!='N'&&opt!='n');
getch();
ArrangeTop();
menu();
}


void www::game(){
s.Money=0;
top=5;
s.Money=0;
aMoney=60000;
perfect=0;
system("cls");
header();
getinfo();
instruction();
easy();
average();
hard();
jackpot_opt();
getch();
}




int main()
{
www x;
x.FillTopInfo();
x.menu();
switch(x.c){
        case '1':x.game();         break;
        case '2': x.DisplayTop();   break;
        case '3': x.credits();      break;
        case '4': cout<<"\n\t                  Thank You for Playing. Goodbye!"; getch(); exit(0); break;
        default:  cout<<"\n\t             Invalid input, Press ANY KEY to continue"; getch();}
while(x.c!='4');
getch ();
return 0;
}



So, I am a beginner in c++, specifically in using classes. I'm having difficulty in declaring an undefined size array as a class. Here's my complete code. I have not made any header file and declared all functions as public since I can't figure out the mistake. Sorry if my program is such an eyesore. Please help. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Error with Declaring undefined size arrays as a class member

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: Error with Declaring undefined size arrays as a class member

Posted 08 April 2012 - 10:52 AM

I think one of the first things you need to do is study up on the switch statement, Stop using the functions in the conio.h header file. Also reviewing Arrays might also help.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Error with Declaring undefined size arrays as a class member

Posted 08 April 2012 - 10:53 AM

That error message is very clear.

On line 52 you declared
    static char dlife[];


You must specify a size for the array dlife.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1