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;
}