Public Group

c++ error: warning C4700: uninitialized local variable 'k' used

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

Recommended Posts

can anyone tell me what's the problem here?

warning C4700: uninitialized local variable 'k' used

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int abs(int );
void cube(int );
void printVector(vector<int> );
vector<int> v;
int main()
{
int a,c;
int k;
while(cin>>a)
v.push_back(a);
printVector(v);
cube(k);
for(int i=0; i<v.size(); i++)
{
c=abs(v);
cout<<c<<'\t';
}
cout<<endl;
}
void printVector(vector<int>v)
{
for(int i=0; i< v.size(); i++)
cout<<v<<'\t';
cout<<endl;
}
void cube(int x)
{
for(int i=0; i< v.size(); i++)
{
x=v*v*v;
cout<<x<<'\t';
}
cout<<endl;
}

Share on other sites
Your using the variable k without giving it "default" value.

This warning will go away if you do this:
int k = 0;

Its just a warning and your overwriting whatever value happens to be there for your variable k anyway.

Share on other sites
You are using the variable k without giving it a sane value first.
The compiler you are using consider this to be worth a warning.

Basically, after declaring and defining the variable k:

int k;

its contents is undefined and can be any value.

Share on other sites
it works. thanks u very much = ))

Share on other sites
The warning is there to warn (obviously) you that you may have problems if you do not initialize the variable, as pulpfist said, it can have any value (trash) in it.

Share on other sites
There is no reason to have X as an argument to cube. Cube could be written as:
 void cube() { for(int i = 0 ; i < v.size() ; i++) { int x = v * v * v; cout << x << '\t'; } cout << endl; } 
Once you have done this, it is easy to remove "k" from the calling function.

You should also scope your variables as tight as possible. Note I declared "x" inside the loop in the above example. Likewise, the variable "c" in main() could be declared inside the loop.

There is plenty reason to add "v" as an argument to cube. It means the function could be re-used to show the cubed values of any vector.
 void cube(vector<int> v) { for(int i = 0 ; i < v.size() ; i++) { int x = v * v * v; cout << x << '\t'; } cout << endl; } 
Passing v by const reference would be even better (saves an unnecessary copy of a potentially very long vector).

You could then move "v" to be local to main(), rather than a global. Globals are generally poor design, they create bug prone code which relies on the caller remember to set the state of the global correctly, and cases where two or more callers "fight" over the state of the global, which can cause extremely difficult to reproduce bugs.

Finally, do not declare functions that you intend to use from the standard library. Remove the declaration of "abs".

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633653
• Total Posts
3013162
×