From VB to Cpp. Newbie in need of help!

I have been working on this program for a few days now. I am very new to C++, and as you will probably be able to tell, am very confused about the correct syntax for several occasions (also sorry about my sloppy form :c). I believe the current problem with what I have so far deals with the binary algorithm I'm using. Any help with any portion of the code will greatly appreciated.


Assignment details:


Develop a modularized C++ application program that reads a list of scores from an input data file into an array, displays the list of the scores stored in the arrays, sorts the list in ascending order using a selection sort algorithm, and searches the list for a given score using a binary search algorithm.


Heres what I have so far:


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

ifstream infile(infile.txt); //Just 7 numbers in a text document, space delimited.


int main()
{
void doRead(int scores[]);
void doSort(int scores[]);
int doBin(int scores[], int search); //I'm almost positive the problem is here.. I just can't pin point it.
void doPrint(int scores[]);



int scores[7];
int search, temp;
char yn;

cout<<"Unsorted List"<<endl;
cout<<"-------------"<<endl;
doRead(scores[]);
doSort(scores[]);
cout<<"Sorted List"<<endl;
cout<<"-----------"<<endl;
doPrint(scores[]);

while(yn != 'n')
{
cout<<"Enter a search item, a score: ";
cin>>search;
temp = doBin(scores[], search); //Seems to always get a 3 or a 7 from this function.

if (temp == -1)
cout<<"Item not found!"<<endl;
else{
cout<<"Search is successful. "<<search<<" is the "<<temp;

switch(temp)
{
case 0:
cout<<"st list element."<<endl;
break;
case 1:
cout<<"nd list element."<<endl;
break;
case 2:
cout<<"rd list element."<<endl;
break;
default:
cout<<"th list element."<<endl;
}
}

cout<<"Would you like to search for another item? (y/n)";
cin>>yn;

}

return 0;
}


//*************************************************************************


void doRead(int scores[])
{
int n;

for(n=0;n<7;n++)
{
infile >> scores[n]
cout<<scores[n]<<endl;
}
}


//*************************************************************************


void doSort(int scores[])
{
int i, j, f, t;
int l = 7;

for (i= l - 1; i > 0; i--)
{
f = 0;
for (j=1; j<=i; j++)
{
if (scores[j] < scores[f])
f = j;
}
t = scores[f];
scores[f] = scores[i];
scores[i] = t;
}


//*************************************************************************


int doBin(int scores[], int search)
{
int found = 1;
int m;
int f=0;
int l=7;

if ((search <= scores[6]) && (search >= scores[0])){
while ((found == 1) && (f <= l))
{
m=((f+l)/2)

if (search = scores[m])
found = 0;
else if (search < scores[m])
l=(m-1);
else
f=(m+1);

}}
else
m = -1
return m;
}


//*************************************************************************


void doPrint(int scores)
{
int n;

for(n=0;n<7;n++)
{
cout<<scores[n]<<endl;
}
}
Topic archived. No new replies allowed.