Counting Lines and Fixing Strings

Hey Everyone!

I'm stuck on an issue with my coding and have been having trouble trying to figure it out. I'm hoping that someone can help me or point me into the right direction.

So this is my problem with my program:
I'm writing a program for class where we have to list the names of our "customers" and state how many items they have checked out. One of the things that needs to be done is that there needs to be a function to fix the string for the customer names and another to count how many customers (lines of input). The input data will come from a file containing the data, and each line will represent a customer and the number of items they have checked (ex- lastName,firstName #).

I've written my code so that it capitalizes the first letter in the string (which is the first letter of the last name), but I'm having trouble figuring out how to also make it format the first letter of the first name (first letter after the comma, and no space inbetween names). In addition to that, I'm also having trouble with getting the count for the lines of input correct (I get some ridiculous number like 4201296 or something like that for only 8-9 lines of input when tested).

Any help is greatly appreciated! Thank you in advance!


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

void getData (string name[], int& count);
void fixString (string& word);
void bubblesort (string name[], int count);
void printResults (int count, string name[], int n);

int main ()
{
int count;
int n;
const int MAX = 50;
string name[MAX];

cout << fixed << showpoint;
cout << setprecision (2);

getData (name, count);
bubblesort (name, n);
printResults (count, name, n);

return 0;

}

void getData (string name[], int& count)
{
//Reads customer names and counts them.
ifstream fin;
string fname;
cout << "Enter name of input file: " << endl;
cin >> fname;
fin.open(fname.c_str());

count = 0;

fin >> name[count];
while (fin)
{
fixString (name[count]);
count++;
fin >> name[count];
}
fin.close();
}

void fixString (string& word)
{
int n = word.length();
word[0] = toupper(word[0]);
for (int i = 1; i < n; i++)
word [i] = tolower(word[i]);
}

void bubblesort (string name[], int count)
{
string temp;
for (int i = 0; i < count-1; i++)
for (int j = 0; j < count-(i+1); j++)
if (name[j] > name[j+1])
{
temp = name[j];
name[j] = name[j+1];
name[j+1] = temp;
}
}

void printResults (int count, string name[], int n)
{
cout << "# of data sets read: " << count << "\n" << endl;

cout << right << setw (11) << "DVDs Rented" << setfill (' ')
<< " " << left << setw (25) << "Name" << endl;
for (int i = 0; i < n; i++)
cout << name[i] << endl;
}
Topic archived. No new replies allowed.