11 Replies - 686 Views - Last Post: 05 June 2009 - 12:47 AM Rate Topic: -----

#1 xnewix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 204
  • Joined: 23-May 09

Strings and char

Posted 03 June 2009 - 04:22 AM

I have this program that is supposed to display a list of all the strings in name however it only displays the first one, "swordmen", the hing is though I I change the array type to a char it displays all the text but only the first letter, ie the out put is something like, s,a,s,m,h

Dose anyone have any suggestions on how I can get it to display the whole set of strings??

#include <iostream>
#include <string>

using namespace std;

int main() 
{
int i; i = 0;
int k; k =-1;

string name[5][80] = { "swordmen", 
					   "archers", 
					   "spearmen", 
					   "macemen", 
					   "horsemen", 
					  };

int unitstatslife[5] = { 120, 55, 85, 45, 230, };
int unitstatsattack[5] = { 12, 5, 7, 6, 14, };

for(i; i < 5; i++, k++)
{
cout <<  name[k][80] << "  Life: ";
cout << unitstatslife[i] << "  Attack: ";
cout << unitstatsattack[i] << endl;
}

cout << name[-1][80];

// Specific Army, discover what units are pressant.

//ie 1 = swordmen

// units
// each one needs an ID, this is then what is stored in the Armies arry now, not the units name. 
// a number identifying the unit type is then stored in the unit array. This uit array also contains all the units attributes for combat..

// opens the file called "swordmen" gets the first number as the id.
// if 
const int max_units = 10;


int NationalUnits[max_units][2] = 
{ 1, 1, 
  1, 2,
  1, 2, 
  }; // army 1 unit type 1 = "Swordmen";
		   // army 1 unit type 2 = "Archers";
		   // army 1 unit type 2 = "Archers";

int x;
cout << endl << "Enter X: ";
cin >> x;
int j;
j = x;
if(x >=1)
{
 cout << NationalUnits[x-2][1] << " ", 
 cout << name[x-2][80] << "  Life: ",
 cout << unitstatslife[j-1] << "  Attack: ",
 cout << unitstatsattack[j-1] << endl;
}
 else {
 cout << "Incorrect Input please try again: ",
 cin >> x;
}
// to view output
 cin >> x;
return 0;
};


Is This A Good Question/Topic? 0
  • +

Replies To: Strings and char

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,563
  • Joined: 25-January 07

Re: Strings and char

Posted 03 June 2009 - 05:24 AM

Try...

for(i; i < 5; i++, k++)
{
cout <<  name[k] << "  Life: ";
cout << unitstatslife[i] << "  Attack: ";
cout << unitstatsattack[i] << endl;
}



I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Strings and char

Posted 03 June 2009 - 06:19 AM

Is there a reason you are establishing a two dimensional array of strings?
string name[5][80] = { "swordmen", 
                       "archers", 
                       "spearmen", 
                       "macemen", 
                       "horsemen", 
                      };



Have you become confused between C-style character array strings and C++ strings?

Please explain the purpose of the above array so we can appropriately advise you on what might be a better approach.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Strings and char

Posted 03 June 2009 - 06:21 AM

just a note here:
string name[5][80] = { "swordmen",
					   "archers",
					   "spearmen",
					   "macemen",
					   "horsemen",
					  };


this should be:
string name[5] = { 
					   "swordmen",
					   "archers",
					   "spearmen",
					   "macemen",
					   "horsemen"
					  };


C++'s string class does not require a 2D array to hold such a list. The strings will fit themselves to the size of their data.

lol-- yea see I am not the only one to notice.
Was This Post Helpful? 0
  • +
  • -

#5 xnewix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 204
  • Joined: 23-May 09

Re: Strings and char

Posted 03 June 2009 - 06:26 AM

ahhh I did not see that. I must have forgoten to take it out....
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Strings and char

Posted 03 June 2009 - 06:34 AM

OK so having set that back to a 1D array of strings, as NickDMax explained, you need to look at these:

cout << name[-1][80];


cout << name[x-2][80] << "  Life: ",



What are they intended to do?
Was This Post Helpful? 0
  • +
  • -

#7 xnewix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 204
  • Joined: 23-May 09

Re: Strings and char

Posted 04 June 2009 - 12:37 AM

I tthink ive solved that now, it was just the [80] on the end of the string array, I figured i'd then have to change the bit at the bottem as well.

But now ive also discoverd another problem,

if I enter 3 for X it should bring up the following;

2 archers life: 55
It dose the first 2 parts fine but then when it reaches life it outputs it as [b]7 not 5
I have no idea why because 7 isnt even in that array.

#include <iostream>
#include <string>

using namespace std;

int main() 
{
int i; i = 0;
int k; k = 0;

string name[5] = { "swordmen", 
					   "archers", 
					   "spearmen", 
					   "macemen", 
					   "horsemen", 
					  };

int unitstatslife[5] = { 120, 55, 85, 45, 230, };
int unitstatsattack[5] = { 12, 5, 7, 6, 14, };

for(i; i < 5; i++, k++)
{
cout <<  name[k] << "  Life: ";
cout << unitstatslife[i] << "  Attack: ";
cout << unitstatsattack[i] << endl;
}

cout << name[0];

// Specific Army, discover what units are pressant.

//ie 1 = swordmen

// units
// each one needs an ID, this is then what is stored in the Armies arry now, not the units name. 
// a number identifying the unit type is then stored in the unit array. This uit array also contains all the units attributes for combat..

// opens the file called "swordmen" gets the first number as the id.
// if 
const int max_units = 10;


int NationalUnits[max_units][2] = 
{ 1, 1, 
  1, 2,
  1, 2, 
  }; // army 1 unit type 1 = "Swordmen";
		   // army 1 unit type 2 = "Archers";
		   // army 1 unit type 2 = "Archers";

int x;
cout << endl << "Enter X: ";
cin >> x;
int j;
j = x;
int number;
string unitname[1] = { "Random" };
string life[1] = { "Random" };
if(x >=1)
{
 number = NationalUnits[x-1][1], 

 cout << "Number is: " << number,
 unitname[0] = name[number-1],

 cout << "Unit: " << unitname[0] << "  Life: ",
 life[0] = unitstatsattack[number],

// It goes wrong here.
 cout << life[0] << "  Attack: ",
 cout << unitstatsattack[j-1] << endl;

}
 else {
 cout << "Incorrect Input please try again: ",
 cin >> x;
}

Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Strings and char

Posted 04 June 2009 - 02:48 AM

View Postxnewix, on 3 Jun, 2009 - 11:37 PM, said:

// It goes wrong here.
 cout << life[0] << "  Attack: ",
 cout << unitstatsattack[j-1] << endl;



7 isnt even in that array.


Yes it is.
There it is, right between 5 and 6.
int unitstatsattack[5] = { 12, 5, 7, 6, 14, };


The index for "7" is unitstatsattack[2] which is no great shock since you entered "3" and took one away so you are left with 2.

I think you may need to sit for a bit and really read your code and try to understand what you have here.
It seems you may be quite confused.

And you might want to think about what this is meant to be doing.
int NationalUnits[max_units][2] = 
{ 1, 1, 
  1, 2,
  1, 2, 
  }; // army 1 unit type 1 = "Swordmen";
           // army 1 unit type 2 = "Archers";
           // army 1 unit type 2 = "Archers";


I'm not sure it is doing what you wanted it to.
But if you explain it maybe I am wrong.

This post has been edited by janotte: 04 June 2009 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#9 xnewix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 204
  • Joined: 23-May 09

Re: Strings and char

Posted 04 June 2009 - 11:32 AM

Agh I'v done it again. Its supposed to say
cout << unitstatslife[number], 
but for some weird reason ive put attack not life, I think i'll combine theses to arrays later to remove any futher confusion.


Each army contains units
Each army can have a maximum of 5 units
Units move around with their respective armies automatically
Units can gain "Experence" over time so I guessed I'd need to included their stats within the army container.
Units can be destroyed
New Units can be formed which need to be alocated to the army in their provence if their isn't one a new one is made.
I also don't want to have to included the declerations for units in the source code but rather externally in tex files for easy modification later on.


I think I've mised some stuff out I aciently deleted it before

p.s janotte chears for before I can't belive I keep making these silly little mistakes

This post has been edited by xnewix: 04 June 2009 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#10 xnewix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 204
  • Joined: 23-May 09

Re: Strings and char

Posted 04 June 2009 - 12:43 PM

Ignore that I've found the problem

int unitstatslife[5] = { 120, 55, etc, }


then further down
string life[1] = { "Random", }


so as far as i remeber this isnt possable as

int number;


so
life[0] 
would have to be an int right?
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,806
  • Joined: 16-October 07

Re: Strings and char

Posted 04 June 2009 - 01:06 PM

Parallel arrays are, as always, evil. Excise them from you tool box.

I'm not really following what you're aiming for here, by you'll certainly benefit from some structs and a little overloading.

See if this makes sense:
#include <iostream>
#include <string>

using namespace std;

struct UnitType {
	string name;
	int statslife, statsattack;
};

struct NationalUnitType {
	int units;
	const UnitType &unitType;
};


void print(const UnitType &unit) {
	cout <<  unit.name << "  Life: ";
	cout << unit.statslife << "  Attack: ";
	cout << unit.statsattack << endl;
}

void print(const UnitType units[], int size) {
	for(int i=0; i<size; i++) {
		print(units[i]);
	}
}

void print(const NationalUnitType natUnit) {
	cout << natUnit.units << " "; 
	print(natUnit.unitType);
}


int main() {
	// this should reall by a vector...
	const int unitsCount = 5;
	UnitType units[unitsCount] = {
		{"swordmen", 120, 12},
		{"archers", 55, 5},
		{"spearmen", 85, 7},
		{"macemen", 45, 6},
		{"horsemen", 230, 14 }
	};
	print(units, unitsCount);
	
	const int max_units = 10;
	NationalUnitType nationalUnits[max_units] = {
		{ 1, units[1] },
		{ 1, units[2] },
		{ 1, units[3] },
	};
	print(nationalUnits[1]);

	return 0;
};


Was This Post Helpful? 0
  • +
  • -

#12 xnewix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 204
  • Joined: 23-May 09

Re: Strings and char

Posted 05 June 2009 - 12:47 AM

I kind of get it yeah, just a little unsure of how the last few lines work thoug.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1