Hi guys i'm new in C++..:icon_wink:

I have a homework that i need to pass soon and i need help because i only have 1 error and i'm staring at it for 2 hours:icon_cry: going back and forth to my book and came up with no solution:icon_sad:.

ok so heres the direction:).. (i need to follow those)

Q: did my codes follow the direction:?:?
if not then how can i fix it..

Thank you in advance for your help guys:icon_biggrin:

1) Use the following numbers as your input file.

5658845
4520125
7895122
8777541
8451277
1302850
8080152
4562555
5552012
5050552
7825877
1250255
1005231
6545231
3852085
7576651
7881200
4581002
-1

these numbers should be read by your program and stored in a single dimention array of type long. assume there will be no more than 20 numbers, but your code should check that the user doesnt attempt to enter more. If an attemp is made to enter 21, print an error message on the screen and process only 20.

2) Print an appropriate program purpose and labels on the both screen output and file out put. Print the unsorted file, four number per file line.

3) Print sorted array on the output file, four numbers per line.

4) The user should enter a number from keyboard. Your program shoudk print a message on the screen saying whether or not the number is in the list. The binary search should be use to determine if the number is valid.

5) your design should include at least four functions in addition to the main func. The selection sort code and the code to perform a binary search should be two of the function.

6) Your name as a programmer should appear on the output file and on the screen

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


int getinput(); //Function prototype
int binarySearch(long aray[], int, int); //function prototype
void selectionSort(ofstream & , char[] , long aray[], int); //Fuction prototype
void showarray(long aray[], int); //Function prototype

int main()
{

ifstream infile;
ofstream outfile;
char fileName[20]; //contains the filename on the storage device
char fileName1[20]; //contains filename of the output
int loop = 0;
const int SIZE = 20;
//int Arraysize[SIZE];
long singleary;
int count = 0, size2 = 0;
int number;
long aray[SIZE];


//Print program purpose
cout << "Process the Charged Account Numbers"<<endl<<endl;

//Print the name of the input from the file
cout << "Enter the name of the input file: ";
cin >> fileName;
infile.open(fileName);

//get files
infile >> singleary;

while(singleary != -1 && loop < SIZE)
{

count++;

aray[count -1] = singleary;
infile >> singleary;

}


//display the sorted numbers to the outfile
selectionSort(outfile, fileName, aray,loop);

//display unsorted array

showarray(aray, loop);



//enter a number from keyboard
number = getinput();

//display the result

if (number == -1)
{
cout << "That number doesnt exist" << endl;
}
else
cout << "That number is on the list" << endl;



//account number search
selectionSort( outfile, fileName, aray , loop);

for ( int i = 0; i < SIZE; i++)
outfile << aray[i];



//print programmer's name
cout << "Programmer:trowa\n";

//Close the files
infile.close();
outfile.close();

return 0;
}


//function 1

int getinput()
{
int numBer;
int count = 0;
int SIZE = 20;

cout << "Please enter a number to search: " <<endl;
cin >> numBer;


return numBer;

}

//function 2

void selectionSort(ofstream & outfile, char fileName1[20], long aray[], int loop)
{

cout << "Enter the name of the output file: ";
cin >> fileName1;
outfile.open(fileName1);


outfile << "The sorted Numbers" <<endl << endl;

{
int startScan, minIndex, minValue;


for(startScan = 0; startScan < loop; startScan++)
{
minIndex = startScan;
minValue = aray[startScan];
for (int index = startScan + 1; index < loop; index++)
{
if (aray[index] < minValue)
{
minValue = aray[index];
minIndex = index;
}
}
aray[minIndex] = aray[startScan];
aray[startScan]= minValue;
}
}

//print programmer's name
outfile << "\n\nProgrammer: trowa\n";
outfile.close();

}

Ok heres the error i'm getting for 2 hours :confused:

error C2109: subscript requires array or pointer type

it says its this one

selectionSort(outfile, fileName,aray[20],loop[20]); (in my codes) please take a look at my codes

You have two variables with the same name, fileName, which may well be problematic, but I see only two calls to selection sort, neither of which have the 4th parameter as an array and the types passed seem to match the declaration and definition argument lists, which each match, so I don't see the problem either.

Does it give you a line number?

Have you tried a rebuild to see if that clears it up?

You could try changing the loop to a different name and see if that does any better.

You have two variables with the same name, fileName, which may well be problematic, but I see only two calls to selection sort, neither of which have the 4th parameter as an array and the types passed seem to match the declaration and definition argument lists, which each match, so I don't see the problem either.

Does it give you a line number?

Have you tried a rebuild to see if that clears it up?

You could try changing the loop to a different name and see if that does any better.

Yes i tried that one.. but the other filename has 1 on it (fileName1) so i guess it doesnt matter right? and the fileName in my main is just fileName (no 1).. i tried to change it to "hello" just now and still got the same error.. T_T
anymore suggestion please?
thank you for replying .. I appreciate your time

my bad, I didn't see the 1 at the end of fileName1 which is why I don't even like to use long file names that differ by just a single value because I get them mixed up when scanning code.

I did a copy/paste/compile and had to add a dummy definition for showarray() but then it compiled fine with just a warning that the variable fileName1 declared on line 18 was unrefereced (not used in the code). I used VS 2008.

my bad, I didn't see the 1 at the end of fileName1 which is why I don't even like to use long file names that differ by just a single value because I get them mixed up when scanning code.

I did a copy/paste/compile and had to add a dummy definition for showarray() but then it compiled fine with just a warning that the variable fileName1 declared on line 18 was unrefereced (not used in the code). I used VS 2008.

sorry if i confused you

lol heres the last two codes for func3 and 4
unreferenced? ei.. how? but i have the header and stuff .. so what do you suggest?

i dont know why it didnt copy(sorry)

I'm using 2008 too.. i have alot of problems with 2010

// function 3
int binarySearch(long aray[],int numElements, int value)
{
int first = 0;
int last = numElements -1;
int middle;
int position = -1;
bool found = false;

while (!found && first <= last)
{
middle = (first + last) / 2;
if (aray[middle] == value)
{
found = true;
position = middle;
}
else if (aray[middle] > value)
last = middle -1;
else
first = middle + 1;
}
return position;
}

// function 4

void showarray(long aray[],int loop)
{
	
for (int count = 0; count < loop; count++)
{cout << aray[count] << " ";
//if ((count+1)%4 == 0);
cout<< endl;
}
}

You didn't call binarySearch() in post #1 so it wasn't needed. I just assumed you didn't post showArray() because it wasn't pertinent. I just added a definition for showArray() with empty function body and it compiled fine. Therefore I can't see where your error report is coming form. From your post it looks like the compiler thinks loop is an array of int instead of a single int or that selectionArray is expecting an array of 20 as the 4th argument, when in fact both the prototype and definition indicate the 4th argument is clearly supposed to be an int. However, I didn't get that error when compiling.

The warning just meant that the variable named fileName1 as declared on line 18 in post #1 wasn't used (wasn't used, wasn't refered to and unreferenced all mean the same thing) in the program. That's because variable named fileName1 used in the parameter list and body of selectionSort is a local variable of selectionSort but it can be any valid char[] of capacity 20 that is in scope and sent to selectionSort. You actually send fileName to both calls to selectionSort so it is actually the value of fileName that's been sent to selectionSort that is being used fileName1 in selectionSort until you overwrite it on line 114. Since you are using an ofstream in selectionSort and fileName1 is the string declared to hold the file to write to, I suspect you really want to send fileName1 to selectionSort, not fileName, anyway, which would be another good example of confusion arising from using variable names that are long and similar.

commented: Your patience deserves rep.. I would have been somewhat upset w/ troubleshooting partial code. +5
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.