getline loop

I am having a problem with reading more then the first line in this loop. The function has a loop that should read each line in a csv file and output the information from the file that the user is searching for. It will then send data to an array to store the important information read from the file. It keeps reading the first line over and over again. This is the entire program and i'll point out where it goes wrong.

This is not a finished program, i can't continue writing it until i know how to fix this loop. Thanks.

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

string getMenu(ifstream&);
int getChoice(ifstream&, int, int);
int arrayLength(ifstream&);

int main()
{
int i = 1;
int choice;
int choicePlace;
string mainMenu;
string picks;
string lookUpChoices;
string file = "music_list.csv";
stringstream menuString;
ifstream fileIn;
int length;

cout<<"\n\n MUSIC LIBRARY\n";
cout<<" ------------------------------------\n";
cout<<" MAIN MENU\n\n";


/*cout<<"What is your file name? ";
getline(cin, file);
fileIn.open(file.c_str()); */

fileIn.open(file.c_str());
length = arrayLength(fileIn);
fileIn.close();
fileIn.clear();
fileIn.open(file.c_str());

if(fileIn.is_open())
{
mainMenu = getMenu(fileIn);
menuString<<mainMenu;
while(getline(menuString, picks, ','))
{
cout<<" "<<i<<". "<<picks<<endl;
i++;
}
cout<<" "<<i<<". Exit\n";
cout<<"\n What are you searching for? (Enter the number): ";
cin>>choice;
cout<<"\n Searching....\n";
if(choice == i)
{
cout<<"C YA!\n";
system("pause");
return 0;
}
choicePlace = getChoice(fileIn, choice, length);
return choicePlace;
}
else
cout<<" !Error opening file!\n";
system("pause");
return 0;
}
string getMenu(ifstream& fileIn)
{
string menu;
getline(fileIn, menu);
return menu;
}
int getChoice(ifstream& fileIn, int numChoice, int length)
{
string line;
string lineReturn;
stringstream stringLine;
string pick;
//int i = 1;
string itemArray[length];

//makes sure the file is open
if(fileIn.is_open())
{

***** This is the problem loop ***********************************************

//reads to the end of the file
while(!fileIn.eof())
{
//reads the file line by line
getline(fileIn,line);
{
int i = 1;
stringLine<<line;
while(getline(stringLine, pick, ','))
{
if(i == numChoice)
{
for(int k = 0, j = 1, l = 0; k < length; k++)
{
if(pick == itemArray[k])
{
i++;
}
else
{
cout<<"\n "<<j<<". "<<pick;
itemArray[l] = pick;
l++;
i++;
j++;
}
}
}
*******************************************************************************
else
i++;
}
}
}
}
system("pause");
// return lineCount;
}
int arrayLength(ifstream& fileIn)
{
int lineCount = 0;
char n;

while(fileIn.good())
{
n = fileIn.get();
if(n == '\n')
lineCount++;
}
return lineCount-1;
}
closed account (3CXz8vqX)
Wrap in code tags please >.>

Also don't use system("pause") use getchar() or getline() or anything but system("pause").

PS. Check your brackets. You should always indent your code with the closing bracket directly below the opening bracket. You're missing one.

Correction: You're missing two. edit (Possibly three).

Edit.

Of course, you might not be but I was just checking between the asterisks. But yeah...code tags and indent please.
Last edited on
What does the input data file look like please. Sample data would be useful.

I'd tend to structure the loop like this:
1
2
3
4
5
6
7
8
        while (getline(fileIn,line)) //reads the file line by line
        {
            istringstream stringLine(line);
            
            // rest of processing here
            
            
        }
Last edited on
Topic archived. No new replies allowed.