1

my name is Adam, I have just begun to learn C++, I love it, but am only on pg 181 in the seventh edition of sams teach yourself C++ in one hour a day, and pg 102 in the seventh edition of C++ for dummies. I have seven multi page notes on the sams book, and twenty one multi page notes on the for dummies book. Please help me understand why I get 5 errors with my simple program which will be shown shortly. I do not want to use -fpermissive option, I need to learn how to code correctly as I am not very experienced. Thank you everyone, very very much, I absolutely love C++, and even have a very good idea on a simple program I plan to learn how to write, which could allow program development time, or writing time to be reduced by 5-20 times on average. The following program shown is not this program however, but please help me so I may one day write, and use my program idea for a college paper. Thank you again, problem program follows:

            #include <iostream>
            using namespace std;

        int main()
    {
        cout<< "how many integers do you wish to enter? ";
        int InputNums = 0;
        cin>> InputNums;

        int* pNumbers = new int [InputNums];
        int* pCopy = pNumbers;

        cout<< "successfully allocated memory for "<<
         InputNums<< " integers"<<endl;
        for(int Index = 0; Index < InputNums; ++Index)
        {
            cout<< "enter number "<< Index << ": ";
            cin>> *(pNumbers + Index);
        }

            cout<< "displaying all numbers input: " <<endl;
        for(int Index = 0, int* pCopy = pNumbers;
                Index < InputNums; ++Index)
             cout<< *(pCopy++) << " ";

             cout<< endl;

             delete[] pNumbers;
             cout<< "press enter to continue..." << endl;
             cin.ignore(10, '\n');
             cin.get();

             return 0;
    }

The problem is indicated as being in the multiple initializations of the second for loop. Please tell me why my problem program will not compile. Thank you all. Sincerely Adam.

newmanadam
  • 31
  • 4
  • 2
    `for(int Index = 0, *pCopy = pNumbers; ...` . That'll get you past compiler errors. – Igor Tandetnik Dec 20 '15 at 18:38
  • This is newmanadam speaking, I am extremely grateful for all of your input and help. Good work Igor Tandetnik 4, very quick response, and concise solution. Now, it is my belief that my book-s are attempting to illustrate basic principles, and may be starting with a few obsolete techniques in order to demonstrate others. Thank you all very much. – newmanadam Dec 20 '15 at 23:52
  • o, I apologize Igor Tandetnik for adding a 4 after your name, I can see now that this 4 indicated 4 hours ago. I am admittedly a little drunk. Sorry – newmanadam Dec 20 '15 at 23:58

2 Answers2

2

My first advice would be to find a better book.

Once you've done that, forget everything you think you know about using new to allocate an array (e.g., int* pNumbers = new int [InputNums];). It's an obsolete construct that you shouldn't use (ever).

If I had to write a program doing what you've outlined above, the core of it would look something like this:

cout<< "how many integers do you wish to enter? ";
int InputNums;
cin>> InputNums;

std::vector<int> numbers;

int temp;
for (int i=0; i<InputNums; i++) {
    cin >> temp;
    numbers.push_back(temp);
}

cout<< "displaying all numbers input:\n";

for (auto i : numbers)
    cout << i << " ";
Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
-2

Directly answer your question: you cannot initialize variables of different types in the same for loop declaration.

In your example:

for(int Index = 0, int* pCopy = pNumbers;

int and int * are different types. Even if you use auto to let the compiler automatically deduct the types, both variables cannot have different deducted types.

The solution:

int Index = 0;
for(int *pCopy=pNumbers; ...

Having this one single secondary effect: Index is now not only confined to the scope of the for. Should this be a problem, you may do:

{
    int Index = 0;
    for(int *pCopy=pNumbers; ...
    ...
}

And now the scope of Index is limited to the surrounding curly braces.

mementum
  • 3,153
  • 13
  • 20
  • 2
    `you cannot initialize variables of different types in the same for loop declaration` False. `for(int Index = 0, *pCopy = pNumbers;...)` works, and declares `Index` of type `int` and `pCopy` of type `int*`. – Igor Tandetnik Dec 20 '15 at 19:59