warning C4700: uninitialized local variable 'p' used [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
This code compiles and runs though gives a Microsoft compiler error that I cant fix
warning C4700: uninitialized local variable '' used.
This is in the starting line of the code, I think
void employee::loginemployee()
{
char uname[15];
char pass[15];
char p;
int i=0;
cout<<"\n\t\tEnter User Name :-";
cin>>uname;
puts("\n\t\tEnter Password :-");
while(p!=13)
{
p=_getch();
_putch('*');
pass[i]=p;
i++;
}
pass[i]='\0';
ifstream objdata;
objdata.open("HRStaff",ios::in|ios::out|ios::binary|ios::app);
if(!objdata)
{
cout<<"\n-----Cannot Open the File-----\n";
//return 1;
}
int nflag=0;
while(!objdata.eof())
{
objdata.read((char *)& info, sizeof(info));
if(strcmp(uname,info.uname)==0 )
{
system("cls");
cout<<"\n\n\n\t\t****************************************";
cout<<"\t\t\t\t\t\t\t Welcome TO EMS"<<info.uname<<endl;
cout<<"\t\t****************************************\n"<<endl;
info.putdata("SPS");
cout<<"\n\tPress any key to log out...";
nflag=1;
}
}
if(nflag==0)
{
cout<<"\n\nSorry !! Your Username & Password do not match.";
_getch();
logoutAll();
}
objdata.close();
}

The warning is quite clear. You declare a variable without initialising it:
char p;
then use its uninitialised value:
while(p!=13)
{
// ...
}
Either initialise it before use:
char p = 0; // or any value other than 13
or restructure the logic so its value isn't used until you've assigned to it:
do
{
// ...
} while (p != 13);
Then learn about buffer overflow and stop reading user input into fixed-sized buffers without checking the length. This is C++, not C, so you should usually use std::string to store string values.

Related

I am not allowed to change the declaration of my variable, what other ways can I edit my code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I know this warning have been asked many times. But I can't think of how to edit my code.
I only include the code on the error/warning part.
const unsigned char *ad[100];
unsigned long long ad[100];
int main
{
adlen = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
if(adlen > 0)
{
ad[i] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
i++;
adlen--;
}
}
After compiling, I will get a warning.
warning: assignment makes pointer from integer without a cast
ad[i] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
^
I also tried searching online for another way to declare.
const unsigned char *ad; //or const unsigned char *ad = malloc(100);
unsigned long long ad[100];
int main
{
adlen = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
if(adlen > 0)
{
ad[i] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
i++;
adlen--;
}
}
But I will end up getting an error
error: assignment of read-only location '(ad + (sizetype)((unsigned int)i * 1u))'
ad[i] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
^
I cannot change and have to declare const unsigned char *ad as a pointer so I have to probably add/change something to my code in int main but I cannot think of how/what to do.
I will be glad if anyone can help/guide me on what to do with this warning and error. Thank you!
The function CDC_Device_ReceiveByte returns a value of type int16_t so you need
int16_t ad[100];

printf working fine when kept outside of for loop but causes some error while running when kept in the for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b;
printf("hello");
for(b=1;b<=100;++b)
{
if(b%10==1){
cout << "\n";
for(int l=0;l<=100;++l)
cout << "-" ;
cout << endl;
}
printf("|%s|",b);
}
return 0;
}
enter image description here
printf which is placed outside of loop body works fine but the one placed in the loop body of for causes some kind of error while running!! take a look at the picture !
Your b is an int.
You give b where printf() expects a pointer to char and will attempt to dereference the value you give as such.
Since the value you give via b is not a valid pointer to anything, your program has some access problem.

I cannot understand why this code gives a segmentation fault. Please, can anyone tell me where have I allocated a memory which cannot be used [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
#include<cmath>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
//There were problems in this code. the include wasnt showing up. So i just put them as comments.
using namespace std;
int main()
{
int n, a[n],count[100],temp;
cin>>n;
if(n<100||n>pow(10,6))
return 0;
for(int i=0;i<100;i++)
{
count[i]=0;
}
for(int j=0;j<n;j++)
{
cin>>a[j];
if(a[j]<0||a[j]>=100)
return 0;
}
for(int m=0;m<n;m++)
{
temp=a[m];
count[temp]++;
}
for(int s=0;s<100;s++)
{
cout<<count[s]<<" ";
}
return 0;
}
Whoops!
Looks like you're trying to create a variable sized array (which doesn't exist... kinda) with an undefined variable (which can be anything the system wants it to be).
Use pointers instead, and let the user fill the variable before creating the array:
int n;
std::cin >> n;
int* a = new int[n];
Or use the magic C++ vectors, since you're including them. They offer many more features than plain pointers like dynamic allocation and some newer methods of type-safety, along with multiple in-out methods like LIFO and FIFO:
int n;
std::cin >> n;
std::vector<int> a(n);
Your below statement is wrong:-
int n, a[n],count[100],temp;
cin>>n;
a fix size array declaration must need the size during compilation. In the above declaration n does not have a size during compilation. Hence compiler can not allocate memory from stack.
a

Run-Time Check Failure #3 - The variable 'result' is being used without being initialized [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Having issues with my code... the program compiles, but then it gives the following statement "Run-Time Check Failure #3 - The variable 'result' is being used without being initialized." It then ends the program at that point. Everything I have after trying to run the function is ignored. What should I do?
double result;
for (int i=0; i<nRows; i++)
{
absum.push_back(vector<double>());
for (int j=0; j<nColumns; j++)
{
double temp;
temp = matrixa[i][j]+matrixb[i][j];
absum[i].push_back(temp);
cout << temp << '\t';
}
cout << endl;
}
return result;
At the top of your code you have:
double result;
At the moment it's not initialised to anything at all, so the compiler won't use it. So you need to need to initialise it thus:
double result = 0;
It's also generally good practice to initialise every variable you use in C++, that way you don't get nasty compiler messages, and don't run the risk of returning some random chunk of memory. You always want to start your program from a known state, so if you know that result is 0, then all is good.
C++ is picky about this sometimes, have you tried double result = 0?

Segmentation fault while using printing output of strlen in C++ 4.8.1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include<cstdio>
#include<cstring>
#include<cstdlib>
int main(){
int t;
scanf("%d",&t);
while(t--){
char *str;
scanf("%s",str);
int l,n,nop=1;
l=strlen(str);
printf("Length %d",l); //causing segmentation fault
if(l%2==0)n=l/2;
else n=(l-1)/2;
for(int i=0;i<n;i++)nop=nop+abs(str[i]-str[l-1-i]);
printf("%d\n",nop);
}
return 0;
}
I am getting error when I print the length of string. If I remove that line the code works but with incorrect output.
The code works fine with DevC++
You need to initialize str by allocating space for it:
char *str = (char *)malloc(SIZE);
Otherwise it will invoke undefined behavior. Also, declare l of size_t type because strlen returns type is size_t and change %d to %zu in printf