8 Replies - 314 Views - Last Post: 01 February 2010 - 07:08 AM Rate Topic: -----

#1 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Query on using string

Post icon  Posted 01 February 2010 - 05:56 AM

I was wondering why i am having error for this code:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

i did #include<string>


#include <iostream>
#include <string>

using namespace std;

class Frog{

private:
	

public:
	int length;

	string movement0;
	string movement1;
	string movement2;
	string stage0;
	string stage1;
	string stage2;
	movement0="roll";
	movement1="swim";
	movement2="jump";
	stage0="egg";
	stage1="tadpole";
	stage2="adult";

	Frog(int age){
		length = age;
	}
	void showStage(int age){
		cout<<length;
	}
	void Move(int age){
		switch(age){
			case 1:
				cout<<"The Current Stage:"<<stage[0]<<endl;
				break;
			case 2:
				cout<<"The Current Stage:"<<stage[0]<<endl;
				break;
			case 3:
				cout<<"The Current Stage:"<<stage[0]<<endl;
				break;
			case 4:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			case 5:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			case 6:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			case 7:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			case 8:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			default:
				cout<<"The Current Stage:"<<stage[2]<<endl;
				break;

		}
	}

};

int main(){

	int age;
	int opt;
	cout<<"Enter Frog days";
	cin>>age;
	cout<<"Enter option";
	cin>>opt;

	Frog t1(age);
	if(opt==0){
		t1.showStage(age);
	}
	if(opt==1){
		t1.showStage(age);
	}
	if(opt==2){
		t1.showStage(age);
	}
	return 0;


}



i am new to c++ please advise

Is This A Good Question/Topic? 0
  • +

Replies To: Query on using string

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Query on using string

Posted 01 February 2010 - 06:17 AM

movement0="roll";
movement1="swim";
movement2 ="jump";
stage0="egg";
stage1="tadpole";
stage2="adult";


There is your problem. Unless the variables are declared using 'static' keyword, you can't initialize them, inside a class. That's what a constructor should do. So, move this part of the code, inside 'Frog' constructor.
EDIT:
 cout<<"The Current Stage:"<<stage[0]<<endl;


Where did you get 'stage' from? I can't see any variable with this name inside your class ...

This post has been edited by sarmanu: 01 February 2010 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Query on using string

Posted 01 February 2010 - 06:22 AM

View Postsarmanu, on 1 Feb, 2010 - 05:17 AM, said:

movement0="roll";
movement1="swim";
movement2 ="jump";
stage0="egg";
stage1="tadpole";
stage2="adult";


There is your problem. Unless the variables are declared using 'static' keyword, you can't initialize them, inside a class. That's what a constructor should do. So, move this part of the code, inside 'Frog' constructor.
EDIT:
 cout<<"The Current Stage:"<<stage[0]<<endl;


Where did you get 'stage' from? I can't see any variable with this name inside your class ...



oh thanks. i got what u mean. btw how to i assign value to array in constructor ?


let say i use

movement[2]

Frog(string movement): ????{}




how to assign ???


and for the stage[0] was a typo.. sorry.. because initially i tried array stage[2]; den i got the error, though was because i used array wrgly and change the declaration but forgot the rest..

This post has been edited by tnecniv: 01 February 2010 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Query on using string

Posted 01 February 2010 - 06:27 AM

I'm not sure that I understand you. Do you want to create an array, containing stage0, stage1 and stage2? If yes, just declare a 'normal' array, with a maximum of three elements, capable of holding strings:
string stage[3]; // declare an array with maximum of 3 elements
stage[0] = stage0; // stage[0] contains stage0
stage[1] = stage1; // stage[1] contains stage1
etc ..


Or you could use the STL vector, if it's not too advanced for you:
vector<string> stage;
stage.push_back(stage0); // stage[0] contains stage0
stage.push_back(stage1); // stage[1] contains stage1
etc..


If you chose the second part, be sure to #include <vector> at the top of your program.
More on 'normal' arrays: http://www.cplusplus...utorial/arrays/
STL vector, great tutorial by one of DIC users: http://www.dreaminco...wtopic33631.htm

This post has been edited by sarmanu: 01 February 2010 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Query on using string

Posted 01 February 2010 - 06:32 AM

View Postsarmanu, on 1 Feb, 2010 - 05:27 AM, said:

I'm not sure that I understand you. Do you want to create an array, containing stage0, stage1 and stage2? If yes, just declare a 'normal' array, with a maximum of three elements, capable of holding strings:
string stage[3]; // declare an array with maximum of 3 elements
stage[0] = stage0; // stage[0] contains stage0
stage[1] = stage1; // stage[1] contains stage1
etc ..


Or you could use the STL vector, if it's not too advanced for you:
vector<string> stage;
stage.push_back(stage0); // stage[0] contains stage0
stage.push_back(stage1); // stage[1] contains stage1
etc..


If you chose the second part, be sure to #include <vector> at the top of your program.
More on 'normal' arrays: http://www.cplusplus...utorial/arrays/
STL vector, great tutorial by one of DIC users: http://www.dreaminco...wtopic33631.htm


erm the 2nd part is too advance for me.. but i will go read up abit..


yes i wanted to create a array movement contain 3 thing

roll swim and jump

then another array stage holding egg tadpole and adult

but all these need to be declared in the class.

which u mention that initializing need to be in the constructor, which i am not sure how to initialize using constructor


Thank you
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Query on using string

Posted 01 February 2010 - 06:42 AM

First of all, declare two arrays of strings, with a maximum of three elements, in your public/private zone of your class (depending on where are you going to use them):
string movements[3];
string stages[3];


Then move to your constructor, and initialize the arrays:
movements[0] = movement0;
.
.
.
stages[3] = stage3;


And that's all ... I hope you get the idea on how to assign elements to an array.
Was This Post Helpful? 0
  • +
  • -

#7 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Query on using string

Posted 01 February 2010 - 07:00 AM

As advised i have tried but when its compiling it has no error but stopped halfway indicate in my code the line having problem below


#include <iostream>
#include<string>

using namespace std;

class Frog{

private:
	

public:
	int length;

	string movement[2];
	
	string stage[2];

	

	Frog(int age){
		length = age;
		movement[0]="roll";
		movement[1]="swim";
		movement[2]="jump";
		stage[0]="egg";
		stage[1]="tadpole";
		stage[2]="adult";//<----------------------After this line it came a error" Access violation "
	};

	void showStage(int age){
		cout<<length;
	}
	void Move(int age){
		switch(age){
			case 1:
				cout<<"The Current Stage:"<<stage[0]<<endl;
				break;
			case 2:
				cout<<"The Current Stage:"<<stage[0]<<endl;
				break;
			case 3:
				cout<<"The Current Stage:"<<stage[0]<<endl;
				break;
			case 4:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			case 5:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			case 6:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			case 7:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			case 8:
				cout<<"The Current Stage:"<<stage[1]<<endl;
				break;
			default:
				cout<<"The Current Stage:"<<stage[2]<<endl;
				break;

		}
	}

};

int main(){

	int age;
	int opt;
	cout<<"Enter Frog days";
	cin>>age;
	cout<<"Enter option";
cin>>opt;

	Frog t1(age);
	if(opt==0){
		t1.showStage(age);
	}
	if(opt==1){
		t1.showStage(age);
	}
	if(opt==2){
		t1.showStage(age);
	}
	return 0;


}

Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Query on using string

Posted 01 February 2010 - 07:02 AM

string movement[2];
string stage[2];


Should be:
string movement[3];
string stage[3];


because both of arrays are storing three values, not two. You were confused because of indexing from 0 I suppose ...
Was This Post Helpful? 0
  • +
  • -

#9 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Query on using string

Posted 01 February 2010 - 07:08 AM

View Postsarmanu, on 1 Feb, 2010 - 06:02 AM, said:

string movement[2];
string stage[2];


Should be:
string movement[3];
string stage[3];


because both of arrays are storing three values, not two. You were confused because of indexing from 0 I suppose ...


oh ok.. i got it working thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1