Question about arrays.

This is a discussion on Question about arrays. within the C++ Programming forums, part of the General Programming Boards category; Hello, I am extremely new to C++.. following the guidelines of a book I bought, I have knowledge of PHP ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    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.

    Code:
    #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;
              cin>>average[i];
              cout<<"  adding: "<<average[i]<<" to "<<total<<endl;
              total += average[i];
              cout<<"    new total: "<<total<<endl;
        }
        cout<<total<<endl;
        cout<<"\nThe average of "<<i<<" numbers is: "<<total/i<<".";
        system("pause");
    }
    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.


    -Aaron

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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:
    Code:
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    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
    Posts
    8,893
    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!
    CornedBee

    "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, 12:27 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21