So, I'm working on an assignment. It involves a read-in text file and the program itself. When I run it I get what my teacher would call "garbage" in the output.
Here is the info from the .txt:

Mike 1200
John 350
Jen 1500
Tara 700
Michelle 2000
Kevin 500
Matt 450
Kim 200

..and here is my source code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void OrderByName(string[], float[], int);

void OrderByScore(string[], float[], int);

int SearchForWinner(string[], float[], int);


int main()
{
string name[15];
string search;
float score[15];
int count =0;
ifstream infile("winners.txt");
if(!infile)
{
cout << "unable to open file , so exiting from program ";

return 0;
}
while(!infile.eof())
{
infile >> name[count] >> score[count];
count++;
}
//OrderByName(name, score, count);
OrderByScore(name, score, count);
SearchForWinner(name,score,count);
infile.close();

return 0;
}

void OrderByName(string array[], float score[], int n)
{
    cout<< " Ordered alphebetically by name:\n";
    for(int i=0; i<n; i++)
    {
    int min = i;
        for(int j=i; j<n; j++)
        {
            if(array[min]>array[j])
            {
                min = j;
            }
        }
        string temp = array[i];
        array[i] = array[min];
        array[min] = temp;
        float temp1 = score[i];
        score[i] = score[min];
        score[min] = temp1;

        cout<<array[i]<<" "<<score[i]<<endl;

    }
}
    //cout<< " Ordered alphebetically: "<<temp1 " "<<score<<endl;
void OrderByScore(string array[], float score[], int n)
{
    cout<<" Ordered by Score: "<<endl;
    for(int i=0; i<n; i++)
    {
    int min = i;
        for(int j=i; j<n; j++)
        {
            if(score[min]>score[j])
            {
                min = j;
            }
        }
        string temp = array[i];
        array[i] = array[min];
        array[min] = temp;
        float temp1 = score[i];
        score[i] = score[min];
        score[min] = temp1;
        cout<<score[i]<<" "<<array[i]<<endl;
    }
}
int SearchForWinner(string array[], float score[], int n)
{
    OrderByName(array,score,n);
string search;
cout << "Enter a name: ";
cin >> search;
int start = 0;
int end = n - 1;
int index = 0;
int middle = (start+end)/2;
   while( start <= end )
   {
      if ( array[middle] < search )
         start = middle + 1;   
      else if ( array[middle] == search )
      {
        index = middle;
        break;
      }
      else
        end = middle - 1;
       middle = (start + end)/2;
   }
   if(start > end)
   index = -1;

   if(index>0)
   cout << search << " is in the list and resides in position " <<(index/*+1*/) << " alphabetically. "<<  endl;
    else
    cout << search << " was not found in the list. " << endl;
return index;

}

When I run it, I get this:

 Ordered by Score: 
3.98864e-34           //here is the garbage I'm talking about!
200 Kim
350 John
450 Matt
500 Kevin
700 Tara
1200 Mike
1500 Jen
2000 Michelle
 Ordered alphebetically by name:
 3.98864e-34          //here is the garbage I'm talking about!
Jen 1500
John 350
Kevin 500
Kim 200
Matt 450
Michelle 2000
Mike 1200
Tara 700
Enter a name: 

I commented where the unwanted output is. I've run into this before, but since I can't remember how to fix it, I guess I didn't learn it.

Recommended Answers

All 11 Replies

There are eight lines to read in the text file but your input loop goes around nine times.

commented: Master! +15

Okay, I'm trying to see where I can remedy this...If I limit the scope of the loop to n-1, I lose a name, and if I start the loop at 1 I lose a name...where am I missing this? Also, after looking through this a little deeper...where in the heck is main calling OrderByName? I see where I have it in main but it's commented out? How is that possible?

Also, don't bother writing your own sorting and searching functions - chances are you will get them wrong! Use qsort() and bsearch() instead. These are standard C functions (usable in C++ as well), or if you have to use C++ classes/methods, then things like maps that automatically sort elements are appropriate. IE, these techniques will reduce your code to a few lines...

@rubberman- Thanks, I will definitely note that, but she specifically wanted us to write our own. Actually I'm going to ask her if I can use those...Understanding is one thing but simplification and modularity are king!
Can anyone help me figure out how to NOT get the extra index entry?

You need to initalize the all loop indices to zero, not one; the index of the first element of an array in C++ (and most other languages that don't support defined array boundaries - a rare feature found mainly in Ada, the Wirth languages, and some Lisps because it has added overhead and is not often needed), is always the zeroth element. Arrays in C++ are what are called zero-indexed arrays, because they follow mathematical notation and start at element zero.

In other words, the first element of an array a[5] would be a[0], the second element would be a[1], and so on to the fifth element, a[4].

I understand that the first element in an array is 0. I may be confused as to what you are saying, but doesn't

for(int i = 0; i < n; i++);

initialize the loop index to 0?

Erk, you are correct; I saw the initializers for the inner loops and didn't notice why you had them starting with 1. My mistake.

Oh, has your course covered structs yet? If so, you might consider using a defining one that holds the names and scores, and create an array of that struct, rather than having to synchronize a pair of arrays:

struct Player 
{
    string name;
    float score;
}; 

Player players[15];

Ah, now I see the issue: the problem is not with the sorting functions, but with the count variable. You are incrementing it once too many times. If you pass count - 1 to the functions instead it ought to work.

Also, the min variable is redundant, as it will always equal i. I expect that you're aware of this, but that you had added it while trying to debug the functions.

Here is what I have tweaked...let me know what you think. I still cannot see how the OrderByName function is being called in main...Will someone point out what I am missing with how it's being called? Also about structs...yeah we have done them, it just didn't occur to me to try it that way. Thanks.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void OrderByName(string[], float[], int);

void OrderByScore(string[], float[], int);

int SearchForWinner(string[], float[], int);


int main()
{
string name[15];
string search;
float score[15];
int count =0;
ifstream infile("winners.txt");
if(!infile)
{
cout << "unable to open file to read data, exiting. ";

return 0;
}
while(!infile.eof())
{
infile >> name[count] >> score[count];
count++;
}

OrderByScore(name, score, count-1); //-1 added to remove garbage in output
SearchForWinner(name,score,count-1);  //-1 added to remove garbage in output
infile.close();

return 0;
}

void OrderByName(string array[], float score[], int n)
{
    cout<< "Ordered alphebetically by name:\n";
    for(int i=0; i<n; i++)
    {
    int min = i;
        for(int j=i; j<n; j++)
        {
            if(array[min]>array[j])
            {
                min = j;
            }
        }
        string temp = array[i];
        array[i] = array[min];
        array[min] = temp;
        float temp1 = score[i];
        score[i] = score[min];
        score[min] = temp1;

        cout<<"   "<<array[i]<<" "<<score[i]<<endl;

    }
}

void OrderByScore(string array[], float score[], int n)
{
    cout<<"Ordered by Score: "<<endl;
    for(int i=0; i<n; i++)
    {
    int min = i;
        for(int j=i; j<n; j++)
        {
            if(score[min]>score[j])
            {
                min = j;
            }
        }
        string temp = array[i];
        array[i] = array[min];
        array[min] = temp;
        float temp1 = score[i];
        score[i] = score[min];
        score[min] = temp1;
        cout<<"   "<<score[i]<<" "<<array[i]<<endl;
    }
}
int SearchForWinner(string array[], float score[], int n)
{
    OrderByName(array,score,n);
string search;
cout << "Enter a name: ";
cin >> search;
int start = 0;
int end = n - 1;
int index = 0;
int middle = (start+end)/2;
   while( start <= end )
   {
      if ( array[middle] < search )
         start = middle + 1;   
      else if ( array[middle] == search )
      {
        index = middle;
        break;
      }
      else
        end = middle - 1;
       middle = (start + end)/2;
   }
   if(start > end)
   index = -1;

   if(index>0)
   cout << search << " is in the list and resides in position " <<(index+1) << " alphabetically. "<<  endl;
    else
    cout << search << " was not found in the list. " << endl;
return index;

}

I still cannot see how the OrderByName function is being called in main...Will someone point out what I am missing with how it's being called?

It's being called by SearchForWinner.

Ah! There it is! Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.