Calling bool function inside if parameters [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 8 years ago.
Improve this question
so I have a C++ bool function that I've written that looks like this:
bool EligibileForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
bool eligible = false;
if (CompanyUsed==CompanySubscribed)
eligible = true;
return (eligible);
}
Now in my main() this function is called as the only parameter for an if statement:
if (EligibleForDiscount(CompanyUsed, CompanySubscribed))
{
ApplyDiscount(Cost, CompanySubscribed);
cout << "\nAfter your discount, your rental is: $"
<< fixed << showpoint << setprecision(2) << Cost << ".\n";
}
The main function was written by my teacher and we wrote the other functions, so this if statement isn't supposed to be changed.
So I understand what the if statement is trying to accomplish, by basically saying "if (true) do this..." since the EligibleForDiscount will return a boolean value.
However, g++ is giving me an error with the if statement, telling me that EligibleForDiscount is not declared in this scope.
But I'm not trying to use it as a value but as a call to a function.

It may be because of two reasons:
You misspelled the function name when called : if (EligibleForDiscount(CompanyUsed, CompanySubscribed)) should be written like your implementation of the function, which is EligibileForDiscount.
This can happen if you forgot to declare the prototype of the function, which is an indicator to the program that you're going to use that function. You simply need to write somewhere before you use the function bool EligibileForDiscount(const char , const char)
One of these should work!

Because : EligibileForDiscount != EligibleForDiscount with an additional "i", just a typo.

p.s. you can write EligibleForDiscount like this:
bool EligibleForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
return CompanyUsed==CompanySubscribed;
}

Related

What does error "redefinition of formal parameter" mean and how can I fix it? [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 1 year ago.
Improve this question
C++ beginner here with a functions error, I get the error "redefinition of formal parameter". What does this mean? and how can I fix it.
int getGuessFromUser(int guess)
{
std::cout << "Guess my lucky number between 0 and 10: ";
int guess;
std::cin >> guess;
return guess;
}
You have a parameter int guess and a variable int guess
You don't seem to be using the parameter, so perhaps remove it?
int getGuessFromUser()
{
std::cout << "Guess my lucky number between 0 and 10: ";
int guess;
std::cin >> guess;
return guess;
}
You are getting this error because you have already declared the parameter guess in you getGuessFromUser function. You do not need to declare it again, so you can remove the line int guess
You have a function parameter named guess and also a local variable with the exact same name.
This would be just like declaring two variables with the same name in the same scope (like a function), which is not allowed.
Change one of the names to remove the error.

Using #define the wrong way round works for calling functions. Why? [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 1 year ago.
Improve this question
C and C++ documentation for the use of #define suggests that this should not work as I am using the define to replace the text MyFunc() with _myfunc(), which is a function that does not exist:
#define MyFunc _myfunc
void MyFunc()
{
cout << "This Prints!" << endl;
}
int Main()
{
_myfunc();
return 0;
}
My guess is that the compiler is being clever. It knows that _myfunc() does not exists and therefore does not replace the text and simple uses MyFunc().
I can't find any documentation to support this theory. Does anyone know whether this is correct?
After the preprocessor has run, your program will look like:
void _myfunc()
{
cout << "This Prints!" << endl;
}
int Main()
{
_myfunc(); // #1
return 0;
}
Ignoring other errors here (lack of includes, ...), the compiler can find _myfunc declared and defined, so naturally it will be found by overload resolution at the call site #1.

C++ Int array type in function call [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
I have an array initialised like the following:
int example[5][5];
example[5][5] = 55;
And a function:
void example_function(auto inArray, int z){
cout << "example text = " << inArray[z][z] << endl;
}
And I am calling it into the function like this:
example_function(example, 5);
As you can see, I have the parameter for the function as auto when it is really using an integer array.
When I use typeid(table).name() to get the type of the array example, it outputs the type as A5_A5_i where the fives are from the initialisation (e.g. int example[3][4][5] would output A3_A4_A5_i)
When using typeid(table).name() on inArray after changing the type of the parameter from int to auto, I get the type name as PA5_i which is different to the one mentioned above.
How can I get a suitable type for a parameter in my function, and is there a better way to do this
If the array passed to the function is known beforehand, in this case as int example[5][5];, you can use the following,
void example_function(int (&inArray)[5][5], int z){
cout << "example text = " << inArray[z][z] << endl;
}
Here we take the array by reference to avoid array decay.
If the size might vary at runtime, use std::vector.
std::vector<std::vector<int>> example;
void example_function(std::vector<std::vector<int>> inArray, int z){
cout << "example text = " << inArray[z][z] << endl;
}

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.

Why can I not use string.length here? [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
OK, I am in a class now that I am taking in C++. It is basic and I am still new. I have a quick question about the string.length() function. Can you compare this for an integer value inside of a if statement? So, if I did
if(string.length() = 20)
{
cout << "IT VWERKS" << endl;
}
would I get an answer? I tried doing this for a program I was working on and it would not work. Could someone explain this to me?
You are using assigment operator = inside if instead of conditional == . so change your code as following .It will work.
if(string.length() == 20)
{
cout << "IT VWERKS" << endl;
}
The correct way to do this it to use the comparison operator (==). In this case, replace the first line with:
if (string.length() == 20)
By doing string.length() = 20 you are trying to assign the value 20 to the result of the function length(), and that is not possible. By replacing the operator = with == you are comparing both values. Once they match, the code inside the if statement is executed.