8 Replies - 6661 Views - Last Post: 26 April 2008 - 09:42 AM Rate Topic: -----

#1 xtheendx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 08

Multidimensional array Probelm using strings

Post icon  Posted 25 April 2008 - 06:36 PM

Hi,

I need to have my program store names and department title stored in a two dimensonal array. I have it set up as a char type array using strings but it isn;t compling. I am just simply getting an initialzier string error. using bloodshed dev-c++ comlier. Any ideas?


#include <iostream>
#include <conio.h>
#include  <iomanip>

using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::fixed;

int main()
{

	char nameTitle [10] [9] = { {"Ollie Regan" , "William Sherman", "Maureen Mooney", "Marty Shark", "Marcella Riley"} , { "manager", "assistant manager", "produce manager", "bakery manager", "cashier manager"} };
	double string2 [5] = { 18.00, 16.00, 15.00, 15.25, 13.00};

	for ( int i = 0; i< 5; i++)
	{
		for (int j = 0; j <5; j++)
		cout << nameTitle [ i] [j];

return 0;

} 


Is This A Good Question/Topic? 0
  • +

Replies To: Multidimensional array Probelm using strings

#2 KYA  Icon User is offline

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

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

Re: Multidimensional array Probelm using strings

Posted 25 April 2008 - 06:42 PM

It isn't compiling because your array dimensions are not big enough for the strings you defined.

and you're missing the closing brace on the for loop

This post has been edited by KYA: 25 April 2008 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 xtheendx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 08

Re: Multidimensional array Probelm using strings

Posted 25 April 2008 - 06:59 PM

View PostKYA, on 25 Apr, 2008 - 06:42 PM, said:

It isn't compiling because your array dimensions are not big enough for the strings you defined.

and you're missing the closing brace on the for loop



I fixed those two things. Trying to make the array dimensions as big a possiable, but still geting the same error.

/*

#include <iostream>
#include <conio.h>
#include  <iomanip>

using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::fixed;

int main()
{

	char nameTitle [100] [90] = { {"Ollie Regan" , "William Sherman", "Maureen Mooney", "Marty Shark"} ,{"Marcella Riley", "manager", "assistant manager", "produce manager", "bakery manager", "cashier manager"} };
	double string2 [5] = { 18.00, 16.00, 15.00, 15.25, 13.00};

	for ( int i = 0; i< 5; i++)
	{
		for (int j = 0; j <5; j++)
		cout << nameTitle [ i] [j];
	}
return 0;

} 

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: Multidimensional array Probelm using strings

Posted 25 April 2008 - 07:15 PM

You are declaring a char array that tells the computer that will be 100 characters in the first part and 90 in the second, but you are initializing strings. You'll have to choose.

Another possible solution is declaring it to be a pointer to a char [5][5] array
Was This Post Helpful? 0
  • +
  • -

#5 xtheendx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 08

Re: Multidimensional array Probelm using strings

Posted 25 April 2008 - 07:26 PM

View PostKYA, on 25 Apr, 2008 - 07:15 PM, said:

You are declaring a char array that tells the computer that will be 100 characters in the first part and 90 in the second, but you are initializing strings. You'll have to choose.

Another possible solution is declaring it to be a pointer to a char [5][5] array



i am not sure what you mean that i will have to chose between declaring the array and intializing the strings. What am i doing there? and i can't use pointers for this project..it is a good idea thought. thanks for the hlp so far.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

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

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

Re: Multidimensional array Probelm using strings

Posted 25 April 2008 - 08:27 PM

I mean you have to choose between using an array of chars or using an array of strings. Depends on the needs of the assignment, rules, etc...
Was This Post Helpful? 0
  • +
  • -

#7 xtheendx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 08

Re: Multidimensional array Probelm using strings

Posted 25 April 2008 - 10:06 PM

Alright got that pasrt working ..thanks for the help. I still have a problem though. I have to have the name and department stored in a 2-d array and the payrate stored in a 1-d array. But the progem has to print the name and department of everyone that makes more than 15 dollars an hour. I am not sure how to do this since htey are two different arrays. here is my code thus far


#include <iostream>
#include <conio.h>
#include  <iomanip>

using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::fixed;

int main()
{

	static const char *nameTitle [5] [5] = {{"Ollie Regan ", 
							   "William Sherman ", 
							   "Maureen Mooney ", 
							   "Marty Shark ", 
							   "Marcella Riley "}, 
							   {"manager", 
							   " assistant manager", 
							   " produce manager", 
							   " bakery manager", 
							   " cashier manager"}};

	float payRate[5] = { 18.00, 16.00, 15.00, 15.25, 13.00};

	nameTitle[0] [0] = payRate [0];
	nameTitle[1] [1] = payRate [1];
	nameTitle[2] [2] = payRate [2];
	nameTitle[3] [3] = payRate [3];
	nameTitle[4] [4] = payRate [4];
	


	for (int nRow = 0; nRow < 5; nRow++)  
		{ 
			  for (int nCol = 0; nCol < 5; nCol++)
			  {
			 for (int i = 0; i < 5; i++)
		{
			if ( payrate[i] > 15.00)

			
		cout << setw (15) << nameTitle[nRow] [nCol];
	 		}
	 	}
	
	
	char pause = getchar();  // program pauses before ending

	getchar();

return 0;

} 

This post has been edited by xtheendx: 25 April 2008 - 10:36 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

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

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

Re: Multidimensional array Probelm using strings

Posted 25 April 2008 - 11:23 PM

  nameTitle[0] [0] = payRate [0];
    nameTitle[1] [1] = payRate [1];
    nameTitle[2] [2] = payRate [2];
    nameTitle[3] [3] = payRate [3];
    nameTitle[4] [4] = payRate [4];
    



This is illegal. You would have to overload the operator for it to understand this. Why not make a 3 dimensional array with the trailing part the pay rate?

Question: I'm getting all kinds of errors when running the code you posted. Out of curiosity isn't it crashing for you due to a write error using the array? I thought you said earlier no pointers?
Was This Post Helpful? 0
  • +
  • -

#9 xtheendx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 08

Re: Multidimensional array Probelm using strings

Posted 26 April 2008 - 09:42 AM

yeah turns out i can use pointers..sorry for the mistake. But yeah i know it doesn't run, it says illegal operation. It will run fine just displaying the info, it is the code you are talking about that is giving me problems. I can't figure out how to use the payrate to display the name a department of all those who make more tat 15 dollars an hour. And it it can use a 3d array, that am i sure of.. any ideas.. thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1