CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83

    Problem with code

    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
    Attached Files Attached Files
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

  2. #2
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661
    the main function isn't close with '}'

    common typo
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  3. #3
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661
    but to be more specific:
    if(integerValue < 0 )
    {
    // then exit
    break;
    {
    thre's the typo....
    chnge it into '}'
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83

    Red face

    Thank you Codeguru 's for your help and pointing out about <iostream.h>
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured