简体   繁体   中英

Unused variable error, altought it shouldent

I`m having some errors in my c++ program. I need to create out of 2 sorted list, a 3-rd which is sorted out of the first 2.

void arrayInp()
/* Create 2 vectors by the length defined by the user*/
{
int a,b,c,d,i,j;  
Array A,B;    /* HERE i get the error used varaibls, why?*/
int rez[20];
cout<<"enter length of the first array: ";
cin>>a;
cout<<"enter length of the second array: ";
cin>>b;

cout<<"insert first array:";
for (i=0;i<=a;i++)
    cin>>c;
    A.els[i]=c;
    cout<<", ";

cout<<"insert second array:";
for (j=0;j<=a;j++)
    cin>>d;
    B.els[j]=d;
    cout<<", ";
}

The header i imported is containing:

const int dim = 10; 
struct Array
{
int n;
int els[dim];
};

Thank you for your help

The warning possibly comes from rez , which you don't use.

First time I looked at the code, I could tell you're coming from python. The code results in undefined behavior (possibly, depending what indexes get to be):

int a,b,c,d,i,j;  
Array A,B;    /* HERE i get the error used varaibls, why?*/

//...

for (i=0;i<=a;i++)
    cin>>c;
    A.els[i]=c;
    cout<<", ";

See the error?

for (i=0;i<=a;i++)
{
    cin>>c;
}
A.els[i]=c;
cout<<", ";

How about now?

If you are a beginner, try and get Clang to compile your code. It puts a particular emphasis on digestible error messages.

If you cannot use it, you still have the online version , though it's limited in terms of dependencies obviously.

/tmp/webcompile/_1981_1.cc:18:5: warning: unused variable 'rez' [-Wunused-variable]
int rez[20];
    ^
1 warning generated.

You can see, in general, for diagnosis:

  • the file, line number and column number where the warning/error is anchored
  • the flag which generated the warning, should you choose to disable it
  • the source line that produced the output, with a cursor pointing at the position
  • finally, it is colored if your terminal supports it, to better identify the various parts (file, error/warning, text, source code, ...)

And for this particular warning: the name of the unused variable is actually shown.

Do yourself a favor, get a friendly compiler ;)

rez is the unused variable, not A or B .

And you have several other errors. The braces for one thing. And you ask for more input parameters than the user is prepared to give (in (i=0;i<=a;i++) ). And you use a for an upper boundary instead of b in the second block. Copy and paste error?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM