I am having problems with my code and I was hoping someone could help me. I created an array of strings and I am going to put student names into the string array from a file but I keep getting this error. Invalid conversion from char to char*. I think I understand this as the array is a pointer and the information from the file is a value and you can't put a value into an address. I want to be careful putting too much code here because it is an assignment for a class. Here is what I think is appropriate. Keep in mind that there has been some code removed like the .close() statement.
assign05.cpp: In function \u2018void writeStudentList(std::string, int&)\u2019:
assign05.cpp:94: error: invalid conversion from \u2018char\u2019 to \u2018char*\u2019
assign05.cpp:94: error: initializing argument 1 of \u2018std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]\u2019
int main()
string unorgList[140];
void "function"()
int i = 0;
fstream fin("studentList.txt");
while (!fin.eof())
{
fin.getline(unorgList[i], 40);
i++;
}
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
void openingMessage();
void writeStudentList(string unorgList, int &numStudents);
/**********************************************************************
* MAIN
*
* Description:
*
* Input:
* Output:
*
* Return:
***********************************************************************/
int main()
{
// The unorganized student name list that will be used by the write
// student list function to input the initial list.
string unorgList[140];
int numStudents;
// Friendly opening message
openingMessage();
// Function that writes the names of the students to a string array
writeStudentList(unorgList[140], numStudents);
return 0;
}
/***********************************************************************
* OPENING MESSAGE
*
* Description: Displays a friendly opening message
*
* Input: none
* Output: none
*
* Return: none
**********************************************************************/
void openingMessage()
{
cout << endl;
cout << "-----------------------------------------------------------"
<< "-----" << endl;
cout << "Binary Search Program - Search using last name (case sensit"
<< "ive)" << endl;
cout << "-----------------------------------------------------------"
<< "-----" << endl << endl;
}
/***********************************************************************
* WRITE STUDENT LIST
*
* Description: Writes from a file the names of students into a string
* array.
*
* Input: unorgList, numStudents
* Output: unorgList, numStudents
*
* Return: none
**********************************************************************/
void writeStudentList(string unorgList, int &numStudents)
{
int i = 0;
fstream fin("studentList.txt");
if (fin.fail())
{
cout << "ERROR: Unable to find studentList.txt. Please ensure the"
<< " file is in the same directory as this program." << endl;
}
else
{
while ( std::getline( fin, unorgList[i]) )
i++;
}
fin.close();
}
void writeStudentList(string unorgList, int &numStudents); //Ask for just one string
writeStudentList(unorgList[140], numStudents); //pass one string (out of bounds, btw)
I suppose that you intended
1 2
void writeStudentList(string unorgList[], int &numStudents); //Ask for an array of strings
writeStudentList(unorgList, numStudents); //pass an array of string
PS: you may have trouble with your name convention (your function `reads' from a file)