c++ linux program for class project

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

Any help would greatly be appreciated. THANKS!!
code tags, please.
the `getline()' member function is for `char *'
You need to use the global function for `std::string'

1
2
while( std::getline( fin, unorgList[i]) )
  ++i;
thanks but now I am getting this error

error: no matching function for call to 'getline(std::fstream&, char&)'

What do you mean by "code tags, please."?

Thanks!
code tags http://www.cplusplus.com/forum/articles/16853/

> error: no matching function for call to 'getline(std::fstream&, char&)'
Post your real code.
Sorry, here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <fstream>
using namespace 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();
}
Last edited on
http://cplusplus.com/doc/tutorial/arrays/
1
2
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)
Last edited on
now I am getting this error lol wow I suck at programming.

ssign05.cpp:44: error: conversion from 'std::string*' to non-scalar type 'std::string' requested
Post your updated code.
Topic archived. No new replies allowed.