Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » VERY annoyed at C++ right now!
Closed Thread
Old 07-13-2006, 07:45 PM   #1 (permalink)
Chase's Avatar
 
CECS Major

Join Date: Jul 2005

Location: Louisville, KY

Posts: 387

Chase is on a distinguished road

Send a message via MSN to Chase Send a message via Yahoo to Chase Send a message via Skype™ to Chase
Default VERY annoyed at C++ right now!

I have recently been studying arrays and tried to write a console application that would intake, store, display, and display the sum of numbers input by the user but I keep getting an error! Why? What have I done? It appears to be correct but maybe I am just frustrated! Please help if you can.


Code:
//This prorgam inputs, stores, displays, and sums an array
//entered by the user.

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

//declare protos

void displayArray(int indexOfArray, int arrayA[]);
int sumArray(int indexOfArray, int arrayB[]);


int main(int argc, char *argv[])
{
    cout << "This program allows you to enter an array which is\n"
         << "then stored, summed, and ouput at the end of the program.\n"
         << "To terminate the program simply enter any negative number.\n"
         << endl;
    
    //Declare the array for 100 numbers
    int mainArray[100];
    
    //start loop    
    for (int arrayCount = 0; arrayCount < 100; arrayCount++)
    
    {
        cout << endl
             << "Input your next number in the array: ";
             
        
        //delcare input variable     
        int arrayInput;
        
        cin >> arrayInput;
        
        //check to make sure user wants to continue
        if (arrayInput < 0)
        {
                       break;
                       }
                       
        //else continue on and store the value in the array
        mainArray[arrayCount] = arrayInput;
        
        //jump back to the top of the loop and start over
        
        }
    
    //here the user has exited the loop with a negative and is going
    //to now see the containments and sum of the array
    
    cout << "\n\nYou have entered a negative number.\n\n"
         << "The sum of your array is: "
         << sumArray(arrayCount, mainArray)
         << "\n\nThe containments of your are are: \n\n";
         displayArray(arrayCount, mainArray);

         
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

//Sum Array - this function sums the numbers in the array
int sumArray(int indexOfArray, int arrayB[])
{
    int accumulator = 0;
    for (int i = 0; i < indexOfArray; i++)
    {
        accumulator += arrayB[i];
    }
    return accumulator;
}

                 
//Display Array - this function shows the containments of an array
void displayArray(int indexOfArray, int arrayA[])
 {
    cout << "The value of the array is:" << endl;
    for (int i = 0; i < indexOfArray; i++)
    {
        cout.width(3);
        cout << i << ": " << arrayA[i] << endl;
    }
    cout << endl;
}

The bold code is where I get the error Dev-C++ says "In function 'int main(int, char**)':
name lookup of 'arrayCount' changed for new ISO 'for' scoping
using obsolete binding at 'arrayCount'



Thanks for any help,

::-Chase-::
Chase is offline  
Old 07-14-2006, 09:41 AM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

You're getting an error because 'arrayCount' is out of scope. You declared it in your 'for' loop, which means it only exists within the 'for' loop. If you want to use the variable outside the loop, declare it outside the loop like this:

Code:
int arrayCount;
for (arrayCount=0; arrayCount<100; arrayCount++)
{
     ....
}

jaeusm is offline  
Old 07-14-2006, 09:49 AM   #3 (permalink)
Chase's Avatar
 
CECS Major

Join Date: Jul 2005

Location: Louisville, KY

Posts: 387

Chase is on a distinguished road

Send a message via MSN to Chase Send a message via Yahoo to Chase Send a message via Skype™ to Chase
Default

Thanks for the help! Greatly appreciated. I can't belive I made such a 'noob' (if you will) mistake...argg...oh well, at least now I will remember...thanks again!

::-Chase-::
Chase is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On