error:invalid types 'double[int]' for array subscript?

i want to ask a user to input 2 values. after that i would like to assign the two values to an array: "Two Numbers"
but i keep getting this error:invalid types 'double[int]' for array subscript.
1
2
3
4
5
6
7
int main(){
    double x,y;

    cout<<"Enter two numbers "<<endl;
    cin>>x >>y;
    double TwoNumbers[2];
    TwoNumbers={x,y};


may i know what is wrong with my codes?
morning.

assign like this:

1
2
3
TwoNumbers[0] = x;
TwoNumbers[1] = y;



and the other thing is you've declared an array to hold doubles but you're trying to add integers by the looks of it?
Last edited on
you can assign the values in one step, but it has to be done at declare time, like this:

 
double TwoNumbers[2] = {x,y};
im trying to ask the user to input 2 values. then after that my script would call function "arrangeTwoNumbers" to arrange the 2 value in increasing order.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
using namespace std;

void arrangeTwoNumbers(double& x, double& y);

int main(){
    double x,y;
    cout<<"Enter two numbers "<<endl;
    cin>>x>>y;
    double TwoNumbers[2];

    void arrangeTwoNumbers(double x,double y);
    cout<<TwoNumbers;
    return 0;
}

void arrangeTwoNumbers(double& x, double& y){
if (x>y){
        double TwoNumbers[2];
    TwoNumbers[0]=y;
    TwoNumbers[1]=x;}
    else
    double TwoNumbers[2];
        TwoNumbers[0]=x;
        TwoNumbers[1]=y;
}




but im stuck at this:

else
double TwoNumbers[2];
TwoNumbers[0]=x;
TwoNumbers[1]=y;
error:TwoNumbers was not delcared in this scope.

didnt i had already delcared double TwoNumbers[2] soo many times?
1
2
3
4
    else
    double TwoNumbers[2];
        TwoNumbers[0]=x;
        TwoNumbers[1]=y;


is equivalent to:

1
2
3
4
5
6
    else
    {
        double TwoNumbers[2];
    }
    TwoNumbers[0]=x;
    TwoNumbers[1]=y;


If you don't put curly braces around the code after an else, then only the first line is considered to part of the block.

So, as you can see, the TwoNumbers variable is defined in a scope that immediately ends.

EDIT: You don't seem to understand variable scope at all. You've defined several variables called TwoNumbers - at line 11, line 20, and line 24. These are all different entities, with different scopes and different lifetimes. Assigning a value to one of them will not change the value of the others.

I'd recommend going back to your textbook/tutorial material and getting a better understanding of scope.
Last edited on
now, i have tried to rewrite my script and this time, TwoNumbers is delcared just once.
Have i declared TwoNumbers[2] at the right place?

another issue is that function "arrangeTwoNumbers" doesnt seem to cout any values at all? i was expecting it to cout x and y in increasing order..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>
using namespace std;

void sortTwoNumbers(double& x, double& y);

int main(){
    double x,y;
    cout<<"Enter two numbers "<<endl;
    cin>>x>>y;

    void arrangeTwoNumbers(double x,double y);
}

void arrangeTwoNumbers(double& x, double& y){
        double TwoNumbers[2];
if (x>y){
    TwoNumbers[0]=y;
    TwoNumbers[1]=x;
    cout<<TwoNumbers;}
    else
    {
        TwoNumbers[0]=x;
        TwoNumbers[1]=y;
        cout<<TwoNumbers;}
}
Last edited on
Have i declared TwoNumbers[2] at the right place?

Yes - you now have a single variable TwoNumbers, which exists within the scope of the arrangeTwoNumbers() function.

another issue is that function "arrangeTwoNumbers" doesnt seem to cout any values at all?

You can't output a C-style array like that. You need to output each individual element.

If you use a std::vector instead of a C-style array, you could output the entire vector in one statement like that.
You can't output a C-style array like that. You need to output each individual element.





so i rewrite this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
using namespace std;

void sortTwoNumbers(double& x, double& y);

int main(){
    double x,y;
    cout<<"Enter two numbers "<<endl;
    cin>>x>>y;

    void arrangeTwoNumbers(double x,double y);
}
void arrangeTwoNumbers(double& x, double& y){
        double TwoNumbers[2];
if (x>y){
    TwoNumbers[0]=y;
    TwoNumbers[1]=x;
    cout<<TwoNumbers[0]<<TwoNumbers[1];}
    else
    {
        TwoNumbers[0]=x;
        TwoNumbers[1]=y;
        cout<<TwoNumbers[1]<<TwoNumbers[0];}
}




but it still doesnt print out the values for x and y..
Last edited on
because you also reverse you're printout ordering.
1
2
3
4
5
6
7
8
9
10
11
if (x>y)
{
    TwoNumbers[0]=y;
    TwoNumbers[1]=x;
}
else
{
        TwoNumbers[0]=x;
        TwoNumbers[1]=y;
}
cout<<TwoNumbers[0]<<TwoNumbers[1];
Topic archived. No new replies allowed.