Whats wrong with this program?!?!?

This is a discussion on Whats wrong with this program?!?!? within the C++ Programming forums, part of the General Programming Boards category; I'm relatively new to C++, and was trying to write a simple linear search function to search through an array ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    7

    Whats wrong with this program?!?!?

    I'm relatively new to C++, and was trying to write a simple linear search function to search through an array of strings to determine at which point a new string could be added.

    Here is the code so far:
    Code:
    #include <iostream.h>
    
    int searchList(char[], int, char);
    
    const int arrSize = 5;
    
    void main(void)
    {
    	char tests[arrSize][15] = {"Jeremiah", "David", "Timothy", "Samuel"};
    	int results;
    
    	results = searchList(tests[], arrSize, '\0');
    	if (results == -1)
    		cout << "There is no room left in the array!";
    	else
    	{
    		cout << "Space ";
    		cout << (results + 1) << " is available." << endl;
    	}
    }
    
    
    
    int searchList(char list[], int numElems, char value)
    {
    	int index = 0;
    	int position = -1;
    	bool found = false;
    
    	while (index < numElems && !found)
    	{
    		if (list[index] == value)
    		{
    			found = true;
    			position = index;
    		}
    		index++;
    	}
    	return position;
    }
    And I keep getting the following error:

    error C2059: syntax error : ']'

    The point at which the error is encountered is:
    Code:
    results = searchList(tests[], arrSize, '\0');
    Now, if I delete the brackets, I get:

    error C2664: 'searchList' : cannot convert parameter 1 from 'char [5][15]' to 'char []'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    And if I has a second pair of brackets, I get:

    error C2059: syntax error : ']'

    again.



    So where's my problem? If anyone could help, that would be great. Thanks!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you need to put a number instead of the [] to indicate which array out of the array of arrays to look at. Or you need to change searchList to accept and handle a two dimension array.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Also:
    Code:
    void main(void)
    Bad! Use:
    Code:
    int main(void) 
    //or
    int main() //since this is C++ you can omit the void
    And if you have an up-to-date compiler you should use:
    Code:
    #include <iostream>
    using namespace std;
    Which is the new header
    "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

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    7
    Thantos, how do I do what you said?

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    which part?

    From your code it looks like you want to search for the end of a string.
    so you would do:
    Code:
    results = searchList(tests[0], arrSize, '\0');
    to search in the string "Jeremiah",
    Code:
    results = searchList(tests[1], arrSize, '\0');
    to search in the string "David", etc

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    From the original question posed however, ...determine at which point a new string could be added?, it may be best to change searchList() to accept a two dimensional array. The code posted suggests that the place you want to insert the next string is at the end of the current group of strings, if the current name doesn't exist in the group of strings already. To do that,

    Code:
    1) place the declaration of arraySize before the prototype for searchList()
     
    2) change the prototype to:
     
    int searchList(char[arraySize][MAXLENGTH]), int nextIndex, char [MAXLENGTH]);
     
    where MAXLENGTH is declared a constant int with value of 15 before the prototype is declared. 
     
    3) add an int variable called nextAvailableIndex to main and initialize to 0. Use to keep track of place to place next string.
     
    4) add a variable called currentString declared as a char array of MAXLENGTH.
     
    5) use strcpy() or >> to place a string in currentString to search for in tests.
     
    6) change the function call to:
     
    results = searchList(tests, nextAvailableIndex, currentString);
     
    7) change body of searchList() to
     
    a) search the array of strings
     
    for(int i = 0; i < nextIndex; ++i)
    	 use strcmp() to compare currentString to tests[i]
     
    if currentString found in tests
    	 return i
    else return nextIndex;
     
    8) back in main, look at value of results.
     
    a) if results equals nextAvailableIndex
    	 {
    	 if nextAvailableIndex < arraySize
    		 add currentString to tests at tests[nextAvailableIndex] using strcpy 
    	 else
    		 notify user they can't add any more names to tests
    	}
    	else
    	 notify user that the name already exists in tests and won't be added to it.
    Of course, if the plan is to insert the new string someplace other than at the end of the current list, say insert such that the strings are in alphabetical order, then you will need a different algorithm, although the tasks will be similar.
    Last edited by elad; 06-30-2004 at 12:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 02:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 10:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM

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