Click to See Complete Forum and Search --> : Problem with code


waheedrafiq
February 23rd, 2003, 04:48 AM
Guy's i am having problems with this code for some reason its from a book called C++ for Dummies 4th Edition a chapter Arrays

here is the code O i am using MS-Visual C++ // Array Demo - demonstrate the use of arrays
// by reading a swquence of integers and then
// displaying them in order

#include <iostream.h>

int sumArrary( int intergerArray[], int sizeOfloatArray);
void displayArray(int integerArrary[], int sizeOfloatArray);


int main(int nArg, char* pszArgs[])
{
int nAccumulator =0;
cout << "This program sums values entered"
<< "by the user \n";
cout <<"Terminate the loop by entering "
<< "a negative number \n";

// store numbers into an array

int inputValues[128];
int numberOfValues =0;
for (; numberOfValues < 128; numberOfValues++)
{
// fetch another number
int integerValue;
cout << "Enter next number: ";
cin >> integerValue;

//if its negative.....
if(integerValue < 0 )
{
// then exit
break;
{


inputValues[numberOfValues] = integerValue;

}

displayArray( inputValues , numberOfValues);

cout<<"The sum is "
<< sumArrary(inputValues, numberOfValues)
<< "\n";
return 0;

}



void displayArray(int integerArray[], int sizeOfArray)
{
cout <<"The value of the array is :\n";
for ( int i =0; i < sizeOfArray[i]; i++)
{
cout.width(3);
cout << i <<": " << integerArray[i] << "\n";
}
cout <<"\n";

}

int sumArray(int integerArray[], int sizeOfArray)
{

int accumulator = 0;
for ( int i = 0; i < sizeOfArray; i++)
{
accumulator += integerArray[i];
}
return accumulator;
}



and here are the errors that i am getting

:\My Documents\cprograms\arraydemo.cpp(54) : error C2601: 'displayArray' : local function definitions are illegal

C:\My Documents\cprograms\arraydemo.cpp(66) : error C2601: 'sumArray' : local function definitions are illegal

C:\My Documents\cprograms\arraydemo.cpp(76) : fatal error
C1004: unexpected end of file found

Error executing cl.exe.

arraydemo.obj - 3 error(s), 0 warning(s)

the file is attach to this post if any one of you can help me out here i would be gratefull and will own you a pint :)

SeventhStar
February 23rd, 2003, 05:12 AM
the main function isn't close with '}'

common typo ;)

SeventhStar
February 23rd, 2003, 05:15 AM
but to be more specific:

if(integerValue < 0 )
{
// then exit
break;
{


thre's the typo....
chnge it into '}'

Graham
February 23rd, 2003, 06:46 AM
Does the book actually specify the line:

#include <iostream.h>

?

If it does, ditch it now - it's worse than useless. <iostream.h> does not exist in the ISO standard. It has been replaced by <iostream>, and a book that doesn't recognise that fact is wrong and is misleading you. What else has it got wrong if it can't get a simple thing like that right?

waheedrafiq
February 24th, 2003, 11:35 AM
Thank you Codeguru 's for your help and pointing out about <iostream.h>