Web Hosting Talk







View Full Version : Problem reading from input file into array


Liguidsoul
11-28-2004, 06:49 PM
I am trying to read from an input file the complete line, into an array. There are 20 lines. The lines contain words... (is float best type for array?) I know I am starting with array index 1 and not 0. I did that on purpose.

I am getting an error:

f40242.cpp(18) : error C2065: 'getline' : undeclared identifier

#include <iostream>
#include <fstream>

using namespace std;

int index;
float names[21];
ifstream inFile;
ofstream outFile;

int main ()

{
inFile.open("names.dat");

for (index = 1; index <= 20; index++)
{
getline(inFile, names[index]);
}

for (index = 1; index <= 20; index++)
{
cout << names[index];
}

return 0;
}

hiryuu
11-28-2004, 07:41 PM
getline is a C function, while the rest of your IO is C++. Float is an outright asinine choice for the type of input you're describing. This task seems trivial for any C/C++ web project -- you would be better off working in PHP or Perl, where variable types simply aren't an issue.

Please don't bring your homework questions to this forum.

Liguidsoul
11-28-2004, 09:35 PM
There is nothing in the rules stating that homework related questions are not allowed. This is a programming discussion, and therefore, I have brought you a programming question. If you feel like answering, go right ahead. If not, nobody is going to force you. Who cares what it's for? Regardless of whether it's homework or not, I'm interested in learning. I have made an attempt to solve the problem to the best of my ability. I'm not asking for free answers or solutions. I'm asking for help with a very small portion of the program that I ran into trouble with. For your information, this program is much longer, and much more complex, and I plan to complete it myself. However, I do and will ask for help when I feel that it's needed. And from my experience, WebHostingTalk is a lot more convenient than driving 45 minutes to my university to meet with a tutor regarding one simple question.

Sorry for the rant. Now, I did look up some information regarding getline and it does appear in my C++ book like this:

getline(cin, inputStr);

However, due to the context of my program (using input files and arrays), I think I'm using it incorrectly.

Please advise the correct way to use Getline to read into this array. And which type of array should be used instead of float.

Liguidsoul
11-29-2004, 12:31 AM
Nevermind this anyway. I believe I am thinking of an incorrect approach to the problem.

I started over and I'm off to a better start.

hiryuu
11-29-2004, 12:39 AM
Sometimes frustration and difficulty are a necessary part of the learning process. If it's homework, you should say so, so people don't give you the answer without the knowledge.

Anyway, I'm not sure where they came up with that getline. The Object Oriented C++ version is here:
http://www.cplusplus.com/ref/iostream/istream/getline.html

Each line is a string (an array of characters), so floating point numbers should not come up in this at all. You need an array of strings.

Liguidsoul
11-29-2004, 01:41 AM
Well here's the deal.

I have just completed the whole program. However, I am still having a minor problem somewhat related to the original issue.

The problem exists in the section of the code commented by "// Output number for each candidate". In this portion of the code, you will notice I use the getline function to collect data from the input file and into a string variable. This variable is then supposed to be outputed along with a number.

However, it seems that the getline function is not picking up the data as the output shows the number only, and not the string variable.

Here is what my input files look like:

(voteFile.dat):
9 18
1 4
1 11
4 1
etc...

(names.dat):
1 Villalta, Alana
2 Kingsland, Guy
3 Fonner, Althea
etc...

This is indeed homework, so I'd rather not you post a complete solution for me. I would like hints or information as to what I'm doing wrong so I can attempt to correct the problem myself. Perhaps I am using getline incorrectly?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int index, tempindex, maxvotes, precincts[500], candidates[500], candcount[21] = {0}, preccount[11] = {0};
string name;
ifstream inFile;
ofstream outFile;

int main ()

{
inFile.open("voteFile.dat");

// Read in precinct and candidate numbers
for (index = 1; index <= 500; index++)
{
inFile >> precincts[index] >> candidates[index];
}

inFile.close();

// Incrememnt number for each candidate
for (index = 1; index <= 500; index++)
{
tempindex = candidates[index];
candcount[tempindex]++;
}

// Incrememnt number for each precinct
for (index = 1; index <= 500; index++)
{
tempindex = precincts[index];
preccount[tempindex]++;
}

inFile.open("names.dat");

// Output number for each candidate
cout << "CANDIDATE TOTALS:" << endl << endl;
cout << "NAME" << " " << "VOTES" << endl;
for (index = 1; index <= 20; index++)
{
inFile >> tempindex;
getline(inFile, name);
cout << name << " " << candcount[index] << endl;
}

inFile.close();

// Output number for each precinct
cout << endl << "PRECINCT TOTALS:" << endl << endl;
cout << "PRECINCT NUMBER" << " " << "VOTES" << endl;
for (index = 1; index <= 10; index++)
{
cout << index << " " << preccount[index] << endl;
}

// Output total number of votes cast
tempindex = 0;
for (index = 1; index <= 21; index++)
{
tempindex += candcount[index];
}
cout << endl << "TOTAL NUMBER OF VOTES: " << tempindex << endl << endl;

// Output winning candidate
tempindex = candcount[1];
for (index = 1; index <= 21; index++)
{
if (candcount[index] > tempindex)
{
maxvotes = candcount[index];
tempindex = index;
}
}
cout << "Winning candidate with " << maxvotes << " votes:" << endl;
cout << tempindex << endl << endl;

return 0;
}

hiryuu
11-29-2004, 03:28 AM
Try working from the getline example I posted. I seriously think your book is wrong on that syntax. Everything else about istream is object-oriented. I don't see why that would be an exception.

Liguidsoul
11-29-2004, 04:22 PM
I tried changing the getline section to:

for (index = 1; index <= 20; index++)
{
inFile >> tempindex;
inFile.getline(name, 100);
cout << name << " " << candcount[index] << endl;
}

Compiling...
new.cpp
C:\Documents and Settings\Pentium\Desktop\Project 3\new.cpp(47) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int)' : cannot conve
rt parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

new.obj - 1 error(s), 0 warning(s)

hiryuu
11-29-2004, 07:14 PM
It's looking for a char array (or pointer), but it doesn't have a way to convert a string object to a char array.

Liguidsoul
11-29-2004, 07:20 PM
Hmm, I'm a little confused then.

I was told I could use getline to get the names from the input file. However, you're saying its looking for a char array. Why is that? Can't getline get a string? Isn't that what it's supposed to do? Get the rest of the line.... which is usually of type string.

hiryuu
11-29-2004, 08:35 PM
Back when I was learning all of this, we didn't have strings. Strings were arrays of characters thus
char name[30];

The string object probably has a way to convert to/from a character array, but I don't know what it is. I forgot how much I hate low-level programming. As I was leaving, the programming courses were converting from C++ to Java.

Liguidsoul
11-29-2004, 09:37 PM
K, thanks for the info.

For the most part it seems like I'm done, I'll just need to get additional help to figure out how to have getline working in this context.

Thanks again!