Newb question--Why can't I ask for a position in an array using a variable?

This is a discussion on Newb question--Why can't I ask for a position in an array using a variable? within the C++ Programming forums, part of the General Programming Boards category; I keep on getting an error whenever I try to assign character arrays to a postition in another array using ...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    Newb question--Why can't I ask for a position in an array using a variable?

    I keep on getting an error whenever I try to assign character arrays to a postition in another array using a variable to say what position in the second array I want the first array to be stored in. It is hard to say what I mean in a simple way in English. Below is the code for my program. I only included it so you have some idea what's going on in it; you shouldn't need to understand how all of the embedded for loops work. I would GREATLY apprectiate your help.

    Code:
    /*Program to take sentence user writes and divide into seperate words and 
    store each word in an array
    
    Skipped commenting first section because it is pretty obvious what's
    going on*/
    
    #include <iostream>
    
    using namespace std;
    
    int words();
    
    char sentence[150];
    
    int main()
    {
    	cout << "Enter any words:" << endl;
    	cin.getline(sentence, 150);
    	cout << sentence;
    	return 0;
    }
    int words()
    {
    int wordCount = 0;
    char wordGather = 15;
    char words[10];
    	int sentenceLength = strlen(sentence);
    	for(int c = 0; c < sentenceLength; c++) //Start checking for spaces.
    	{
    		if(sentence[c] == ' ') 			//If a space occurs, 
    		{								//
    			for(int x = c; x <= 0; x--) //Count down from point in array 
    			{							//sentence where a space occured.
    				int y = 0;				//
    				y = y + 1;				//
    				if(sentence[x] == ' ')  //If a space occurs, stop checking.
    				{						//
    					break;				//
    				}						//
    				else					//If no spaces occur, assign 
    				{						//identified word's chars to array 
    										//'wordGather'.
    					wordGather[y] = sentence[x];
    					char words[wordsNumber] = wordGather; //Assign word
    				}							//collected in 'wordGather' to
    											//space <wordCount> in array
    											//'words'.
    /* In the else statement closest to the end I get error 
    'illegal constant expression' dealing with the line
    'char words[wordsNumber] = wordGather;' and the error 
    'pointer/array required' for the line 'wordGather[y] = sentence[x];'
    */
    											
    			} 
    			wordCount = wordCount + 1;
    		}
    	
    	}
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    char words[wordsNumber] = wordGather;
    Do you want:
    Code:
    words[wordsNumber] = wordGather;
    Otherwise you would be *redeclaring* the variable, although you couldnt really declare it that way
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    still dosn't work

    The compiler doesn't seem to care at all that I fixed that error, I still get the same two errors.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    char wordGather = 15;
    -wordGather is not an array, but here you are treating it as one:

    Code:
    wordGather[y] = sentence[x];

    Code:
    char words[wordsNumber] = wordGather;
    -wordsNumber is not declared, unless I'm overlooking it
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    one less error

    JaWiB-thanks a lot. It's really weird that the compiler didn't catch that I didn't declare that one. I changed the name of the variable that holds the number of words, but the find function in the compiler skipped that one for some reason. I still get that pointer/array required error on the line "wordGather[y] = sentence[x];" It highlights the right bracket in "wordGather[y]". I have no clue what it means.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Jawib answered that. wordgather is not an array. You can't DO wordgather[y]. Just:

    wordGather = sentence[x];
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    thanks

    Thanks a ton for your help guys

    Would I have to use a 2 dimentional array to write the entire array wordGather to words[wordsNumber]?

  8. #8
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    Risregard my last post (except for the thanks)
    I was looking at the old code I posted. Here is the new stuff:
    Code:
    /*Program to take sentence user writes and divide into seperate words and 
    store each word in an array
    
    Skipped commenting first section because it is pretty obvious what's
    going on*/
    
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    int words();
    
    char sentence[150];
    
    int main()
    {
    	cout << "Enter any words:" << endl;
    	cin.getline(sentence, 150);
    	cout << sentence;
    	return 0;
    }
    int words()
    {
    int wordCount = 0;
    char wordGather[15];
    char words[10];
    int y = 0;
    	int sentenceLength = strlen(sentence);
    	for(int c = 0; c < sentenceLength; c++) //Start checking for spaces.
    	{
    		if(sentence[c] == ' ') 			//If a space occurs, 
    		{								//
    			for(int x = c; x <= 0; x--) //Count down from point in array 
    			{							//sentence where a space occured.
    										//
    				y = y + 1;				//
    				if(sentence[x] == ' ')  //If a space occurs, stop checking.
    				{						//
    					break;				//
    				}						//
    				else					//If no spaces occur, assign 
    				{						//identified word's chars to array 
    										//'wordGather'.
    					wordGather[y] = sentence[x];
    					words[wordCount] = wordGather; //Assign word
    				}							//collected in 'wordGather' to
    											//space <wordCount> in array
    											//'words'.
    			} 
    			wordCount = wordCount + 1;
    		}
    	
    	}
    }
    The compiler won't allow me to assign the entire array wordGather to words[wordCount]. Do I need to use a 2 dimentional array to do this correctly?

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,199
    I "think" (might be wrong) you have to do something like this:
    Code:
    for(int i=0;sentence[i]!=' ';i++)
        words[i] = sentence[i];
    words[i]='\0';
    I think this is the easiest way to assign a single word to another array, this is not the full code though, but experiment some with it.

  10. #10
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    You also have a function called "words" and a char called "words", which is naughty as far as I know.

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    9
    Originally posted by Mr_Jack
    The compiler won't allow me to assign the entire array wordGather to words[wordCount]. Do I need to use a 2 dimentional array to do this correctly?
    I believe the reason for that is this: you defined words as an array of 15 char elements. But, you're trying to assign to it an array of char elements, not just a single char.

    You would have to redefine words as an array of arrays of characters instead. The first way is (as you suggested) to use a two-dimensional array. But this is limited in that you may define an array that is too small to hold a word. Then you'd get overflow, etc etc etc. Or you might define an array so big you waste memory.

    The other way is to define your words array as an array of pointers to char's:

    Code:
    char *words[10];
    In this example, you can hold 10 words (read: strings) of (almost) any size in the words array.

    You might want to consider using a std::vector object instead, and using a bunch of std::string objects. This removes the hassle of dealing with the arrays, as it is all done transparently.

    You also have a function called "words" and a char called "words", which is naughty as far as I know.
    It depends on the scope of the variable. If the variable is defined in the same scope as the function, you're in trouble. Otherwise, it should be OK.
    Last edited by Jeb.; 10-12-2003 at 03:36 AM.

  12. #12
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    thanks tons

    Without your guys' help I'd be stuck. Thanks.

    Now there are issues with how my code is written. It's really weird. I decided to use a 2 dimensional array becuase I have never used them before and I don't understand how pointers work.
    Here is the current code with notes where stuff doesn't work:
    Code:
    /*Not working*/
    
    
    /*Program to take sentence user writes and divide into seperate words and 
    store each word in an array
    
    Skipped commenting first section because it is pretty obvious what's
    going on*/
    
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    int words();
    
    char sentence[150] = {' '};
    
    int main()
    {
    	cout << "Enter any words:" << endl;
    	cin.getline(sentence, 150);
    	words();
    }
    int words()
    {
    int wordCount = 0;
    char wordGather[15] = {' '};
    char word[10][15] = {'k'};
    int y = 0;
    	int sentenceLength = strlen(sentence);
    	for(int c = 0; c < sentenceLength; c++) //Start checking for spaces.
    	{
    		if(sentence[c] == ' ') 			//If a space occurs, 
    		{								//
    			for(int x = c; x > 0; x--)  //Count down from point in array 
    			{							//sentence where a space occured.
    										//
    		
    				y = y + 1;				//
    				if(sentence[x] == ' ' || '/0')  //If a space occurs or 
    				{						//the end, stop checking.
    					cout << "Working here" << endl;
    					break;				//the '|| "/0"' part doesn't work
    				}						//will output cout stuff x-1 times
    										//when x=number of words typed
    				/*stops working here (because of the break?)*/	//
    				else					//If no spaces occur, assign 
    				{						//identified word's chars to array 
    										//'wordGather'.
    					wordGather[y] = sentence[x];
    					word[wordCount][y] = wordGather[y]; //Assign word
    				}							//collected in 'wordGather' to
    											//space <wordCount> in array
    											//'words'.
    			} 
    			wordCount = wordCount + 1;
    		}
    	
    	}
    	//below is to stop the program from closing; my compiler doesn't add
    	//the press enter to close feature to programs
    	cout << word[1][2];
    	//If I don't initialize the array 'word' to all spaces, then I get
    	//some wacky characters that I have never seen before.
    	int stop;
    	cin >> stop;
    	return 0;
    }

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    9
    The reason your or statement doesn't work as you expect is because it will always be "true".

    You need to change it so that you say if sentence[x] is a space, OR sentence[x] is a null character. You must specifically state sentence[x] in both cases.

    Code:
    if(sentence[x]==' ' || sentence[x]=='\0')...
    Note: A null character is \0, not /0

    From that, you can then see why it never works below your comment - your null character part of the OR always evaluates to TRUE (because a null character is still a character, and a valid character is the equivalent of "true"), therefore that break is going to be called for every character in the word.

  14. #14
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    jeb-I changed to the if statement how you suggested, yet "working here" still only prints n - 1 times. I am really clueless now.

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    9
    Remember that the indexes of an array of characters start at 0. So where your loop says x > 0, it should really be x >= 0.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 06:33 AM
  2. Yet another array question...
    By Raigne in forum C++ Programming
    Replies: 11
    Last Post: 11-13-2008, 12:55 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Array Index: A simple question?
    By logicwonder in forum C Programming
    Replies: 18
    Last Post: 01-06-2006, 02:26 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21