Sign in to follow this  

C++ compile-time error

This topic is 4781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I've just started learning C++ (with the help of "C++ for Dummies") and I've run into a problem. Instead of copying the code right from the book (or worse, not doing it at all), I've been trying to write the various programs before I read his. So far, so good. But alas, nothing good lasts forever. I just wrote a program to try out arrays. It's supposed to ask the user repeatedly to input numbers, until he/she inputs a negative number, at which point it will display all of the numbers entered and their sum. I'm using Dev-C++ 4.9.9.0. Here it is:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

// we're gonna use some function prototypes and such here
int nArray[128];
int nGatherer();
int nAdder(int nArray[128]);

int main(int nNumberofArgs, char* pszArgs[])
{

    //first we give the user instructions and tell them whats goin on
    cout << "Okay, once again, I'm gonna ask you for a bunch of numbers \n"
         << "and I'm not gonna stop until you give a negative number. \n \n";

    int nArray[128];
    nArray[128] = nGatherer();
    
    // now we print out the numbers that they entered with a loop
    for (int i = 1;i <= 128; i++)
    {
        if (nArray[i] < 0)
        {
                cout << nArray[i] << "  ";
        }
        else
        {
                break;
        };
    };
    
    int nSum = nAdder(nArray[128]);
    
    // now we tell the user the sum of thier numbers
    cout << "\n" << "The sum of your numbers is " 
         << nSum;
         
};

int nGatherer()
{
    // we initialze the array
    int nArray[128];
    
    // now we start the loop to fill in the array    
    for (int i = 1;i <= 128; i++)
    {
        cout << "Please enter a number:";
        cin  >> nArray[i];
        
        // if the user enters a negative number we break the loop
        if (nArray[i] < 0)
        {
                break;
        };
    };    
    return nArray[128];
};

int nAdder(int nArray[128])
{
    // initialize the variable to hold the sum
    int nSum = 0;
    
    // we start the loop that adds up all the numbers
    for (int i = 1;i <= 128; i++)
    {
          // check to see if the number is greater than zero.
          // if so, we add it. if not, we break the loop.
          if (nArray[i] >= 0)
          {
                    nSum = nSum + nArray[i];
          }
          else
          {
                    break;
          };
    };
    return nSum;
};

in reference to this line: int nSum = nAdder(nArray[128]); the compiler keeps throwing me this error: invalid conversion from 'int' to 'int*' It may be a simple question (hey, even the best were new to this once), but does anyone know what this means? [edit: added source tags -SiCrane]

Share this post


Link to post
Share on other sites
Quote:
Original post by Action Hank
in reference to this line:

int nSum = nAdder(nArray[128]);

the compiler keeps throwing me this error:

invalid conversion from 'int' to 'int*'


What's happening here is that nAdder expects a "pointer to"/"an array of" integers ("int *") (behind the scenes they're the same thing). nArray is such an array of ints - but nArray[128] refers to the 129th element of nArray (just a single int, and one that dosn't really exist since you've only declared 128 elements in nArray, accessed via 0..127). You only need to specify the size of an array at it's decleration (the line that reads: int nArray[128];).

Change that line to int nSum = nAdder(nArray); and it should work. Instead of passing just a single integer, you'll be giving it the entire array.

Share this post


Link to post
Share on other sites
There seems to be some confusion between declaring an array and accessing an array. It's understandable.

In a declarations such as these:

int nArray[128];
int nAdder(int nArray[128]);
The number says how big the array is. When accessing an array, like these for example:
    nArray[128] = nGatherer();
int nSum = nAdder(nArray[128]);
the number says which element to access. If you want to reference an entire array, you either leave off the "[number]" or you get the address of the first element like this: "&array[0]".

In your case, you declared the function parameter of nAdder to be an array with 128 elements. When you call the function, you must give it an array, but you gave it a single element (because you included "[128]"). The line should probably be either of these:
int nSum = nAdder(nArray);
int nSum = nAdder(&nArray[0]);


Now, there are plenty of other problems with your code, but if you keep trying, you will figure it all out.

Share this post


Link to post
Share on other sites
The [128] is not part of the variable's name; it is part of its type. When nArray[128] is seen anywhere that it couldn't be a declaration, it must be a reference to that "element of the array" - just like in the loop you have to assign to each "nArray[i] - even if it's not that big.

Share this post


Link to post
Share on other sites
The main function can accept a few different configurations of arguments; the one in use is most common, although for "historical reasons" the usual spelling is "int main(int argc, char *argv[])" (or with char** for argv). These parameters will actually pick up command-line arguments; the int parameter is the number of them, and the other is a pointer to some char-stars, each of which has one token from the command line arguments.

Basically, it's just like how Java code requires an entry point of "public static void main(String[] args)" (of course you can call the String[] parameter something else!), although you often don't bother looking at the args.

The amazing thing about it all is that C++ won't convert the 'argv' to a std::vector<std::string>, or even just a std::string[], for you :(

Share this post


Link to post
Share on other sites

This topic is 4781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this