Hi, this is my first time posting here so I'm sorry if I post something wrong.

I have an assignment for my class and I think I have done everything right, well except for this one error that's been killing me.

I get this error for each function call when I try to compile (so basically two errors)

(109): error C2664: 'sortArray' : cannot convert parameter 1 from 'std::string *' to 'char *[]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
(113): error C2664: 'binarySearch' : cannot convert parameter 1 from 'std::string *' to 'char *[]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I had this error before with doubles and the way I fixed it was to remove the pointers in the array, now whenever I do that it gives me about 10 more errors, basically about std:operators or something, and I need the stuff to be in a string for my homework assignment.

Use C-Strings to do Part 1 of the assignment and the string class to do Part 2 of the assignment.

Part 2 of this Assignment requires your program to read in a list of strings from a file called strings3.dat, then display, sort, display, and search the strings (just like Assignment #1 but with strings, not integers). Use the string class to do Part 2 of the assignment (do not use C-Strings to store the strings read from strings3.dat).

Here's my code so far

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;

//function prototypes
bool verifyPass(char *);
int verifyUpper(char *);
int verifyLower(char *);
int verifyNum(char *);
int binarySearch(char *[], int, string);
void sortArray(char *[], int);
int menu();

int main()
{  
	cout << "Welcome to this program!" << endl;
	menu(); 
	return 0;
}

//function definitions
int menu()
{
	int num=0;
	int ans=1;
	cout << "Here is the menu." << endl;
	cout << "Choose one of the following, or choose to quit this program." << endl << endl;
	cout << "1.  Enter a strong password" << endl;
	cout << "2.  Search for a string in the list of strings read from a data file" << endl;
	cout << "3. Quit" << endl << endl;
	cout << "Enter your choice:";
	cin >> num;
	if (num <1 || num > 3)
		cout << "Enter a number between 1 and 3!" << endl;
	else 
	{

		switch (num)
	{
	case 1:
		{
	char password[100]; 
	while (ans==1)
			{
	cout << "Enter a password with the following criteria:\n";
    cout << "8 to 10 characters\n";
    cout << "1 uppercase\n1 lowercase\n1 number:\n";
	cin >> password; 
    
    int length;
    length = strlen(password);
    
	if(length >= 8 && length <= 10)
    {
        while (!verifyPass(password))    
	   {        
		  cout << "Invalid Password. try again\n";        
		  cin >> password;    
	   }    
	   cout << "Your password: " << password << ", is valid\n";    
    }
    else
	{
        cout << "The password entered must be between 8-10 characters long\n";            
	}
	cout << endl << "Would you like to try again? Press 1 to continue" << endl;
	cin >> ans;
		}
		}
	break;
	case 2: 
		{
	string search;
	int results;
	int SIZE=0;
	string *name;
	char file;
	string temp;


	ifstream myfile;
	myfile.open ("3strings.dat");
	while (myfile >> file)
  {
	  SIZE++;
  }
  // dynamic array
  name = new string[SIZE];
   // clear the eof flag and begin to read the file from the beginning
   myfile.clear();
  myfile.seekg (0, ios::beg);

// transfer the file contents to the array
   for (int count = 0; count < SIZE; count++)
   {
	   myfile >> temp;
      name[count] = temp;
   }

   cout << "Here are the unsorted strings" << endl;

      for (int count = 0; count < SIZE; count++)
   {
     cout << name[count] << endl;
   }
	  	//Sort the names
	sortArray(name, SIZE);

	   cout << "Here are the sorted strings" << endl;
	     for (int count = 0; count < SIZE; count++)
   {
	  cout << name[count] << endl;
   }
     		while (ans==1)
			{
	//Get a name to seiarch for
	cout << "Enter the string you wish to search for: ";
	//cin >> search;
	getline(cin, search);

	//Search for the name
	results = binarySearch(name, SIZE, search);

	//If binarySearch returned -1, the name was not found
	if (results == -1)
		cout << "That string does not exist in the array.\n";
	else
	{
		cout << "String " << search << " was found in element "
			<< results << " of the array.\n";
	}
	cout << endl << "Press 1 if you would like to search again" << endl;
	cin >> ans;
				}
		}
	break;
	case 3:
		{
		cout << "Thank you for using this program!" << endl;
		}
		break;
	}
	}
		return 0;
}

bool verifyPass(char *str)
{    
	int len = strlen(str); 
    return verifyUpper(str) && verifyLower(str) && verifyNum(str);
}
//count
int verifyUpper(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (!isupper(str[count]))
		num++;
    }
		return num;    
}
int verifyLower(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (!islower(str[count]))
		num++;
	}
	return num;
}
int verifyNum(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (!isalpha(str[count]))
        num++;
	}
		return num;
}

void sortArray(char *string[], const int size)
{
   int pass, i;
   char *temp;

   for (pass=0; pass < size - 1; pass++) 
   {
      for (i=0; i<size - 1; i++) 
	  {
         if (strcmp(string[i], string[i+1]) > 0) 
		 {
            temp = string[i];
            string[i] = string[i+1];
            string[i+1] = temp;
         }
      }
   }
}

int binarySearch(char *word[], int size, string value)
{
	int first = 0,
		last = size - 1,
		middle,
		position = -1;
	bool found = false;

	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if (word[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (word[middle] > value)
			last = middle - 1;
		else
			first = middle + 1;
	}
	return position;
}

The errors are self explanitory. Your sortArray() and binarySearch() functions are prototyped to take an array of cstrings, and you are trying to pass in a string object.

couple of things to try:

1) see if you can pass in your string whilst calling the c_str() member

sortArray(name.c_str(), name.size());

this might not work because i don't think you can modify a string object via it's cstring pointer. the c_str() member is mainly used for backwards compatibility with the older style cstring functions, as you will see below:

2) what will work though, is make a copy of the string to a cstring:

#include<cstring>

char* word = new char[name.size()+1];

strcpy(word, name.c_str());

sortArray(word, strlen(word));

Now you can pass cstrings arguments into your functions, which are prototyped to accept cstrings (not strings class objects).

Edited 5 Years Ago by Clinton Portis: n/a

The thing is that I can't use cstrings for that part of the assignment, so I'm guessing I will have to re do my entire function to get it to work with strings?

yes.. by all means, please use string class objects. they are so much easier to use than strings.

Edited 5 Years Ago by Clinton Portis: n/a

Well, I decided to not use functions lol as they were confusing me, I decided to loop them instead, I got the code to work but whenever I try to run it I get an error while its debugging now

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;

//function prototypes
bool verifyPass(char *);
int verifyUpper(char *);
int verifyLower(char *);
int verifyNum(char *);
int menu();

int main()
{  
	cout << "Welcome to this program!" << endl;
	menu(); 
	return 0;
}

//function definitions
int menu()
{
	int num=0;
	int ans=1;
	cout << "Here is the menu." << endl;
	cout << "Choose one of the following, or choose to quit this program." << endl << endl;
	cout << "1.  Enter a strong password" << endl;
	cout << "2.  Search for a string in the list of strings read from a data file" << endl;
	cout << "3. Quit" << endl << endl;
	cout << "Enter your choice:";
	cin >> num;
	if (num <1 || num > 3)
		cout << "Enter a number between 1 and 3!" << endl;
	else 
	{

		switch (num)
	{
	case 1:
		{
	char password[100]; 
	while (ans==1)
			{
	cout << "Enter a password with the following criteria:\n";
    cout << "8 to 10 characters\n";
    cout << "1 uppercase\n1 lowercase\n1 number:\n";
	cin >> password; 
    
    int length;
    length = strlen(password);
    
	if(length >= 8 && length <= 10)
    {
        while (!verifyPass(password))    
	   {        
		  cout << "Invalid Password. try again\n";        
		  cin >> password;    
	   }    
	   cout << "Your password: " << password << ", is valid\n";    
    }
    else
	{
        cout << "The password entered must be between 8-10 characters long\n";            
	}
	cout << endl << "Would you like to try again? Press 1 to continue" << endl;
	cin >> ans;
		}
		}
	break;
	case 2: 
		{
	string search;
	int results;
	int SIZE=1;
	string *name;
	string file;
	string pw;
	int i, j, a;
	string temp;


	ifstream myfile;
	myfile.open ("3strings.dat");
	while (myfile >> file)
  {
	  SIZE++;
  }
  // dynamic array
  name = new string[SIZE];
   // clear the eof flag and begin to read the file from the beginning
   myfile.clear();
  myfile.seekg (0, ios::beg);

   cout << "Here are the unsorted strings" << endl;

      for (int count = 0; count < SIZE; count++)
   {
     cout << name[count] << endl;
   }
	  	//Sort the names
	  	// transfer the file contents to the array
	while ( !myfile.eof() ) //while not at end of file
	{
	for ( i = 0; (i < SIZE); i++ )
	{
	getline ( myfile,pw );
	name[i] = pw;
	}//end for
	for ( j = 0; j < SIZE-2; j++ )
	{
	for ( a = 1; a < (SIZE - ( j + 1 ) ); a++)
	{
	if ( name[ SIZE - a ] < name[ SIZE - ( a + 1 ) ] )
	{
	temp = name[ SIZE - a ];
	name[ SIZE - a ] = name[ SIZE - ( a + 1 ) ];
	name[ SIZE - ( a + 1 ) ] = temp;
	}//end if
	}//end for
	}//end for
	for ( i = 0; i < 500; i++)
	{
	cout << i+1 << ". " <<name[i] << endl;
	}//end for
	}//end while

	   cout << "Here are the sorted strings" << endl;
	     for (int count = 0; count < SIZE; count++)
   {
	  cout << name[count] << endl;
   }
     		while (ans==1)
			{
	//Get a name to seiarch for
	cout << "Enter the string you wish to search for: ";
	//cin >> search;
	getline(cin, search);

	//Search for the name

	//If binarySearch returned -1, the name was not found
	if (results == -1)
		cout << "That string does not exist in the array.\n";
	else
	{
		cout << "String " << search << " was found in element "
			<< results << " of the array.\n";
	}
	cout << endl << "Press 1 if you would like to search again" << endl;
	cin >> ans;
				}
		}
	break;
	case 3:
		{
		cout << "Thank you for using this program!" << endl;
		}
		break;
	}
	}
		return 0;
}

bool verifyPass(char *str)
{    
	int len = strlen(str); 
    return verifyUpper(str) && verifyLower(str) && verifyNum(str);
}
//count
int verifyUpper(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (!isupper(str[count]))
		num++;
    }
		return num;    
}
int verifyLower(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (!islower(str[count]))
		num++;
	}
	return num;
}
int verifyNum(char *str)
{
	int num = 0;
	int len = strlen(str);
 
	for (int count = 0; count < len; count++)
	{
		if (isalpha(str[count]))
        num++;
	}
		return num;
}
This article has been dead for over six months. Start a new discussion instead.