Thread: Question about arrays.

  1. #1
    Registered User
    Join Date
    Sep 2007

    Question about arrays.

    Hello, I am extremely new to C++.. following the guidelines of a book I bought, I have knowledge of PHP etc so I know a few things.

    Anyway I went ahead of myself and tried to create something using simple arrays. Here's what I got.

    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main()
        string average;
        int total=0;
        int i=0;
        int input;
        for(i =0; i<3; ++i){
              cout<<"Enter A Number:"<<endl;
              cout<<"  adding: "<<average[i]<<" to "<<total<<endl;
              total += average[i];
              cout<<"    new total: "<<total<<endl;
        cout<<"\nThe average of "<<i<<" numbers is: "<<total/i<<".";
    My problem is, average[] array is obviously not adding the integer value of what the user enters. How can I convert the input to average[i] from a string to integer so the total is correct? Or better yet, how do I create a Integer specific array?

    Thanks in advance for your help. I hope I was clear enough.


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    In your code, average is not an array. average is a std::string (but you did not #include <string>, so there is a risk that it may not be defined at that point).

    how do I create a Integer specific array?
    In your case, it would be:
    int average[3];
    Though average is not a particularly appropriate name for the array (and you can do without the array in the first place).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2007
    Thanks for your quick reply. Changing it worked however I was certain I had tried that before :S

    Once again thanks and good luck !

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Important difference between PHP and C++: C++ arrays are of fixed size. Even the dynamically sized vector does not automatically extend when you supply a large index.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM