ISO C++ forbids comparison between pointer and integer [-fpermissive]_ [closed] - c++

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 3 years ago.
Improve this question
I am not sure why I am getting this error in my code. It seems that my variable num is not being passed to my function printIt.
#include <stdio.h>
void printIt(int *num);
int main(){
int num;
printf("What is your annual income? ");
scanf("%d", &num);
printIt(&num);
return 0;
}
void printIt(int *num){
if (num > 90000){
printf("Congratulations! You are doing great.");
return;
}else{
printf("You will make $90,000, if you keep going.");
return;
}
}

You've defined printIt as receiving a pointer to an int, so inside of it, num is the address of that int, and *num is the value of the int.
IMO, it would be better to change it to void printIt(int num); and when you call it, just pass num directly: printIt(num);. There's no real point in passing the address in this case.

In your function printIt num is a pointer to an integer and *num is the income you have been given from the user.
There are two ways of solving this either change the signature of printIt or change the code.
Alternative one
void printIt(int num){
if (num > 90000){
printf("Congratulations! You are doing great.");
return;
}else{
printf("You will make $90,000, if you keep going.");
return;
}
}
and then call it with printIt(num); instead.
Alternative two: change the code and keep the function call as it is
void printIt(int *num){
if (*num > 90000){
printf("Congratulations! You are doing great.");
return;
}else{
printf("You will make $90,000, if you keep going.");
return;
}
}

Related

incomplete type not allowed, function dec/def and no classes [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 2 years ago.
Improve this question
So i keep getting an "incomplete type not allowed error" on the emphasized call of function "out" on this and ive switched the function decs around, and i decided to not move on until i have this figured out because i can't find any non-class examples that aren't completely analogically opaque. Noob question but any help would be appreciated.
its supposed to be a simple fibonacci function but obviously i haven't written all the code.
Cpp 14 ( on g++, im getting syntax highlights suggesting its 11, however).
#include <string>
#include <iostream>
int fib(int x);
int input();
void out(int p);
int fib(int x) {
// int* prim;
// int* sec;
// *prim = 1;
// *sec = 1;
// int count =
}
int main(){
void out( fib( input() ) ); // error here // out on this line
return 0;
}
int input (){
int inp = 0;
}
void out(int p){
std::cout << "the number is: " << p ;
}
This:
int main(){
void out( fib( input() ) ); // error here
}
is interpreted to something like an instantiation of a variable of type void calling the constructor that accepts a int as first parameter,
instead you probably want this:
int main(){
out( fib( input() ) );
}
that is just a function call
You don't need return type name to call function. Remove void on the error line.

Trying to create a cos(x) calculating function [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
I am coding a cos(x) function in C++ but the result I am getting is infinity, except that it should be -1/3.
Here is my code:
#include <iostream>
#include <math.h>
using namespace std;
The factorial function:
int factorial(unsigned int n)
{
unsigned long factorial = 1;
for(int o=1;o<=n;o++)
{
factorial *= o;
}
}
int main()
{
double x;
double answre;
double input;
cin>>input;
for(int i=0;i<2;i++)
{
double y=2*(i)+2;
I declared y here instead of implementing it's value directly, since I thought it is dividing by factorial instantaneously and that is the reason for all the parentheses as well.
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x;
}
answere=1+x;
cout<<answere<<endl;
return 0;
}
You are not returning anything in factorial
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x; // ???
If you want to add to x, just write x += .... What you did was assign one (!) summand to x and then set x to itself. You are never adding anything.

Why is this recursive function creating an infinite loop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
The following code results in an infinite loop. The value of a does not seem to be going lower than 1. Please help.
int main()
{
init(4);
return 0;
}
void init(int a)
{
int j;
cout<<"After declaring: "<<j;
j = a;
cout<<"After initializing: "<<j;
a--;
while(a>0)
{
init(a);
}
}
First, you are accessing an uninitialized variable. This introduces undefined behaviour into your program; I'd remove this section.
Second, your while-loop runs as long as a > 0, but in the body of the loop, the value of a is never changed. Note that when calling init, parameter a is passed by value, i.e. the value of a is copied; Each instance of init will get it's own a, and it will never change the value of the caller's a.
Recursive functions usually are used instead of a loop. Nevertheless, you need an "anchor" to tell the recursion where to stop:
void init(int a)
{
int j = a;
cout<<"After initializing: " << j;
if (a>0) {
init (a-1);
}
}

Try-Catch inside a "for" loop in c++ [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 6 years ago.
Improve this question
I've 10 numbers to be read. My task is to generate exceptions with the numbers are either a negative number or a even number. Below is the code I wrote, but it isn't working.
#include <iostream>
using namespace std;
int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cin>>a[i];
}
for(int i=0;i<10;i++)
{
try{
if(a[i]<0 && a%2==0)
throw a[i];
}
catch(int a)
{
cout<<"You ve entered a -ve number or a even number";
}
}
return 0;
}
This was the error shown:
In function 'int main()': 16:24: error: invalid operands of types 'int [10]' and 'int' to binary 'operator%'
Thanks for the help!
Two things:
You are modulo-ing an array a with an integer 2. You should dereference it to get the value: a[i] % 2 == 0.
You do a boolean AND, where you want a boolean OR (according to your question): || instead of &&.
Typo here. You can not do modulo operation on an array. a%2==0 should be a[i]%2==0. Also change && to || as you are trying to find "either a negative number or a even number".

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

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.