I'm trying to write a progam that;

Contains a function called sumN() which takes an int n as an argument and returns an int which is the sum of all integers between 1 and n.

In the main() asks the user:
How many values (s)he wants to enter (maximum 50);
Asks for the values and stores them into an array int a[].
Prints to the console sumN(a) for all the elements of a[] that have been entered by the user.

I've gotten this far and when I try to run it, it doesn't work it just says;

in function 'int main()':
error: invalid types 'double[int]' for array subscript. (on line 29)


Here's my code;


#include <iostream>

using namespace std;

int main()
{
    double array, set[50];
    int size, t, value ;

    cout << "How many values do you wish to enter?\n";
    cin >> size;

    while (size>50)
    {
        cout << "Sorry, that was above 50" << endl;
        cout << "How many values do you wish to enter?\n";
        cin >> size;
    }

    cout << "Enter the values:\n";
    for (value=0; value<size; value++)
    cin >> set[value];

    double SumN(double *array, int size);{

        double total = 0;
        int i;
        for(i = 0; i < size; ++i)
            total += *array[i];
        return total;}


    cout << "Here are your values:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

Could someone please tell me where i'm going wrong as soon as possible?
Thanks :)

Don't include 'as soon as possible' in your topic and post... it is rude and even made me consider not replying to you at all.

You are defining the function SumN inside the body of main()... that is wrong. Each function must have its own scope.

Besides that, you say:
double* array;

*array;

that is also wrong... the type of array is double, not double*... Look up exactly how arrays work in C/C++.

General note:
'array' is a pretty bad name for a variable

I believe that the problem lies in line 7. You appear to create a variable called "array" (type double). In line 29, you are trying to call values from an array called "array". However, in line 7, you did not set "array" as an array. There is also something a little funny with the code order. You ask for the size of the array (user input) after you have declared it (or not at the moment). Furthermore, you declare variables "i" and "t" before use in the foor loops. A quicker way of doing this is:

for (int t=0; t<size; t++)   //note the int in the brackets before t
                                    //this initializes t

Nothing will be printed, as you have a return statement at line 30, before your "cout" statement loop at lines 33-35. This will end the function. You should return after your print statements, instead of returning "0". The user will not be able to see the "couts", as the console will close before you get a chance to look.

There may be another couple of things I have skipped informing you about in the above, but here is a working code I have.

#include <iostream>

using namespace std;

 int main()
{

    int size, value ;

    cout << "How many values do you wish to enter?\n";
    cin >> size;

    while (size>50)
    {
        cout << "Sorry, that was above 50" << endl;
        cout << "How many values do you wish to enter?\n";
        cin >> size;
    }
    
    double array[size], set[size];

    cout << "Enter the values:\n";
    int i;
    for (i=0; i<size; i++)
    cin >> set[i];

    double SumN(double *array, int size);{

        double total = 0;
        for(int j = 0; j < size; j++)
            total += array[i];

    system("CLS");
    cout << "Here are your values:\n";
    for (int t=0; t<size; t++)
    cout << set[t] << "\n";
    system("PAUSE");
    
    

     return int(total);}
}

Please upvote this post if it helps you and mark the thread as solved if it solves your problem. Thanks :)

Comments
Nice post, very lnog and informative. If the owner of the topic wont do it, I will...

>>You appear to create a variable called "array" (type double)
From what I can see, "array" shouldn't even exist in main() anyway. It's the name of the first function parameter for SumN() and nothing more. Once the scoping of SumN() is corrected, it will be completely worthless.

In main(), the relevant array is actually called "set" and is declared properly. In the proper version of the program, "set" would be the argument to the call to SumN() and become "array" as the parameter of the function in the corrected definition.

Edited 6 Years Ago by Fbody: n/a

Okay.. em im not great at this computer programming stuff, so i did my best to try and correct it and if its still wrong could someone just kinda tell me what i have wrong in simpler terms please and thanks :) (it works but it doesnt do what i need it to do :( )

here it is;

#include <iostream>

using namespace std;

int main()
{
    double array, set[50];
    int size, t, value,i, total ;


    cout << "How many values do you wish to enter?\n";
    cin >> size;

    while (size>50)
    {
        cout << "Sorry, that was above 50" << endl;
        cout << "How many values do you wish to enter?\n";
        cin >> size;
    }

    cout << "Enter the values:\n";
    for (value=0; value<size; value++)
    cin >> set[value];

    {
    int SumN(int size);
        int sum = 0;
        for(int i = 0; i <= size; i++)
        return (total);}


    cout << "Here are your values:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

oh and SgtMe i tried running your code and it didnt work :S thanks anyway though! :)

Edited 6 Years Ago by Nick Evan: Added CODE-tags. After &gt; 30 posts, you should know how they work

okay.. em im kinda crap at all this computer programming stuff, so i did my best to try and correct it.. it works but it doesnt do what i need it to do! :(
and im sorry if i didnt correct what ya's are saying i should, em i dont really understand what it is i have to correct, so could ya's maybe say it in a simpler way? please and thanks :)

(and i dont mean any of that to sound rude either.. so im sorry if it does :S )

here it is;

#include <iostream>

using namespace std;

int main()
{
    double array, set[50];
    int size, t, value,i, total ;


    cout << "How many values do you wish to enter?\n";
    cin >> size;

    while (size>50)
    {
        cout << "Sorry, that was above 50" << endl;
        cout << "How many values do you wish to enter?\n";
        cin >> size;
    }

    cout << "Enter the values:\n";
    for (value=0; value<size; value++)
    cin >> set[value];

    {
    int SumN(int size);
        int sum = 0;
        for(int i = 0; i <= size; i++)
        return (total);}


    cout << "Here are your values:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

oh an SgtMe.. em i tried running your code and it didnt work.. but thanks anyway!

Your function does not belong in the middle of main

#include <iostream>
using namespace std;

int SumN(your parameters here); //function prototype

int main()
{


}

int SumN(your parameters here)  //function definition
{


}

Note you can put your function definition where you function prototype is and forgo the prototype. Skim through the section of your book pertaining to functions.

I omitted the parameters, but the ones you had originally were basically correct.

Edited 6 Years Ago by jonsca: n/a

The array notation that SgtMe uses is correct for the body of the function. However, read your assignment carefully in that it wants you to sum from 1 to N for each of the N, rather than sum the array together.

Okay so does that mean that i'd be better trying to get my own one working then?

i tried putting in sumN where ya said but it didnt work :( thanks though!

okay

#include <iostream>


using namespace std;


int SumN(int size);
int sum = 0;
for(int i = 0; i <= size; i++)
return (total);


int main()
{
    double array, set[50];
    int size, t, value ;

    cout << "How many values do you wish to enter?\n";
    cin >> size;

int SumN(int size);
int sum = 0;
for(int i = 0; i <= size; i++)
return (total);

    while (size>50)
    {
        cout << "Sorry, that was above 50" << endl;
        cout << "How many values do you wish to enter?\n";
        cin >> size;
    }

    cout << "Enter the values:\n";
    for (value=0; value<size; value++)
    cin >> set[value];

    cout << "Here are your values:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

Errors:

expected unqualified-id before 'for'(line 7)
expected constructor, destructor, or type conversion before '<=' token (line 7)
expected constructor, destructor, or type conversion before '++' token (line 7)
In function 'int main()':
error: 'total' was not declared in this scope (line 22)
warning: unused variable 'array' (line 13)
warning unused variable 'sum' (line 20)


sorry.. there's a lot :(

now there's no errors but it doesn't add them :(

#include <iostream>


using namespace std;

int main()

{
    double set[50];
    int size, t, value, total ;

    cout << "How many values do you wish to enter?\n";
    cin >> size;

int SumN(int size);
int sum = 0;
for(int i ; i <= size; i++)
return total;

    while (size>50)
    {
        cout << "Sorry, that was above 50" << endl;
        cout << "How many values do you wish to enter?\n";
        cin >> size;
    }

    cout << "Enter the values:\n";
    for (value=0; value<size; value++)
    cin >> set[value];

    cout << "Here are your values:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

Your function does not belong in the middle of main:
Remove lines 15-18

Move line 15 to line 5.

After the close of main (i.e., after the }) start the definition of your function int SumN(int size) (no semicolon in the the function definition)
Enclose the body of the function in braces. Notice that main itself is a function that (in this case) takes no arguments and returns a value.

You're going to need to make some changes in the function also, as you never declared total, you didn't initialize i, and you would end up returning the first value only, as once the function hits that return, its execution is over.

As far as the second issue goes, you never use your function in your code. Call it instead of just spitting back the original set like you do in line 33.

I say this again with your best interest in mind. Run through your textbook to look at the examples at least, and try to emulate their syntax.

Edited 6 Years Ago by jonsca: n/a

When I try to run it, it just says that I need to delare system or something like that. Okay thanks for all your help though! I'll try what you said now and let ya know how I get on! :)

Hey everyone! Thanks for all you're help.. I know it was probably frustrating for you all to try and explain things to me but I'm very grateful that ya's did!
I have to sumbit it in 25 minutes so I'll sumbit the bit that I have done and hope I pass!

Thanks again :)

Edited 6 Years Ago by KellieD: n/a

Why didn't my code run? It runs fine for me. I'm confused.

double SumN(double *array, int size);{

Your function is right in the middle of main(). Unless I'm missing something, but at a minimum your braces are off.

You could remove the system commands from my code. They are not essential. System means that it uses shell commands. I'm guessing your not on windows? Sorry I should have asked really :(

Edited 6 Years Ago by SgtMe: n/a

This article has been dead for over six months. Start a new discussion instead.