Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Why if statement with condition cout << c << endl is evaluated to true.
The below code runs without showing the error. I can not understand why does it happen.
#include <iostream>
using namespace std;
int main()
{
int c = 0;
if (cout << c << endl) {
c++;
}
cout << c <<endl;
return 0;
}
Also, why don't I need semicolon there?
std::cout doesn't return true. It's the overloaded operator bool of the returned iostream which is cout here.
the bool operator returns true unless the last taken is eof or the operation has caused a fail or bad state.
Now, why don't you need the semicolon? because it's an expression not a statement.
Like when you use
while(true)// you don't write while(true;)
cout << c << endl
is an expression. If you add a ; at the end, the expression becomes a statement.
The grammar for if statements is that the code inside the () is an expression. This expression still gets executed, and there is no error here. The result of evaluating this expression is just cout, which gets contextually converted to boolean true inside the if condition (since the cout expression is successful), and so c is incremented.
Outside of an if statement, the expression is not valid (or rather, it is not a statement yet), so it must be made into a statement for it to be executed.
Related
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
newbie here messing around and i encountered this error: expected primary-expression before '||' token. checked ever post I could find with similar issues but with no luck.
any help greatly appreciated!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string txt;
while(txt == "") || (txt == " ");
{
cout << "Please enter the sentence you want to translate.";
cin >> txt;
}
}
The syntax for while is (roughly):
while(condition)
block
where block is either a single statement or multiple statements enclosed in { and }.
There is no semicolon after the condition and the condition needs to be wrapped in parentheses.
I recommend you to learn C++ from a good introductory book. Your instructional material should explain to you the syntax of grammar constructs and you should not try to guess it.
The reason you are getting this error message from the compiler is because it is seeing the || operator and expecting to find two "primary expressions", one on each side of the ||. In your case, while(txt == "") is not a primary expression.
from https://learn.microsoft.com/en-us/cpp/cpp/primary-expressions?view=vs-2019, a primary expression is:
100 // literal
'c' // literal
this // in a member function, a pointer to the class instance
::func // a global function
::operator + // a global operator function
::A::B // a global qualified name
( i + 1 ) // a parenthesized expression
It can be confusing because the compiler looks at your code differently than you do and can have a hard time understanding what you are attempting to write even when it seems obvious to you.
What you were trying to do, write a while loop, is spelled like this in C++
while(condition)
statement
//or
while(condition)
{
statements...
}
The condition can be a compound expression like the one you used
while((txt == "") || (txt == " "))
{
cout << "Please enter the sentence you want to translate.";
cin >> txt;
}
Try surrounding the entire while condition with parentheses:
while ((txt == "") || (txt == " ")) {
...
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm working on a print function and my original codes is like
void print1(const char *cp)
{
while(cp)
{
if(*cp)
{
cout << *cp++ << " ";
}
}
}
it would never stop until I changed it to
void print1(const char *cp)
{
if(cp)
{
while(*cp)
{
cout << *cp++ << " ";
}
}
}
I got a little confused about this code, actually it's a code in C++Primer. Did while and if consider the same thing? But why the first one cannot stop? Is that because there is a pointer points to the last location but it has nothing in it, the while would be true forever but will never get into the if?
In the first example, the loop runs until the pointer itself becomes null, which it never does (instead, it's eventually incremented past the end of the buffer, whereupon the program exhibits undefined behavior).
In the second example, the loop runs until the character pointed to becomes zero - which it does once the advancing pointer reaches the end of a nul-terminated string.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hello I'm wondering how I can call functions within parameters in my class in main?
class processChoice {
public:
void processInput(string, int, string, int);
};
void processChoice::processInput(string processInput_UN,
int processInput_PC,
string initial_UN,
int initial_PC) {
for (; (processInput_UN != initial_UN) || (processInput_PC != initial_PC);
cout << endl) {
cout << "Enter your username: " << flush;
cin >> initial_UN;
cout << "Enter your 4 digit pincode: " << flush;
cin >> initial_PC;
cout << endl;
if ((processInput_UN == initial_UN) && (processInput_PC == initial_PC)) {
cout << "Access granted!" << endl;
} else {
cout << "Username and/or pincode doesn't match, try again..."
<< endl;
}
}
int main() {
userPinchoice Choice;
Choice.chooseUsername();
Choice.choosePincode();
cout << endl;
initial Values;
Values.initialUsername();
Values.initialPincode();
processChoice Input;
Input.processInput();
return 0;
What am I suppose to put in the round brackets at Input.processInput()?
I have been trying to get it to work but I just can't seem to access the function. I'm new to this so any help would be welcome.
Thanks in advance!
The bulk of your problem lies in the class processChoice. Here are some of such errors:
Firstly, the parameters specified in the function declaration in your class is faulty:
void processInput(string, int, string, int);
Here, you have only specified 4 data types, not variables. Kee in mind, these are variables that store the data that is passed from another function. In order to do this, you need to have these variables declared in the above line, variables with specific names that can be identified inside a function. You should have this line in your function declaration:
void processChoice::processInput(string processInput_UN, int processInput_PC, string initial_UN, int initial_PC)
This brings up another problem. Your function header is diffent upon declaration, and its header is different upon defining. The compiler sees it as 2 different functions. Therefore, you should keep your function headeer the same when declaring it and defining it.
Secondly, the for-loop inside your function has syntax errors:
for (; (processInput_UN != initial_UN) || (processInput_PC != initial_PC);
cout << endl) {
Firstly, judging from the syntax of the condition statement of the loop, it should be a do-while loop, not a for loop. Secondly, the cout << endl; should not be inside the condition statement of the for-loop; it should be implemented with the rest of the function. Thirdly, you do not put a semicolon at the end of your condition statement; it tells the compiler that this an empty loop without a body. Your loop should be something like:
do
{
cout << endl; // this is where you put the cout statement
// add rest of function code here
}
while ((processInput_UN != initial_UN) || (processInput_PC != initial_PC))
Another side note, why do you need 4 input parameters in your function? You take the pin number and username as input from the user, eliminating the need for 2 parameters. Something like:
string initial_UN;
int initial_PC;
If you declare this inside your function, just before the do-while loop I suggested above, then that eliminates these 2 variables from being parameters.
Now, to get to your question, if we have the following declaration:
Input.processInput();
Then we need to pass 4 parameters (2 if you follow my notes above) to it, and that is what the brackets next to the function name in this line of code a for. To pass a value to the function, simply, do the following (I'm only passing 2 parameters here, you can pass how many ever parameters you have defined in yout class only, not less or not more):
Input.processInput("Username", "password");
There are 2 input parameters that specify what the username and password should be. To differentiate the parameters being passed, syntax requires a comma to split them. You can also pass variables as arguments; however, make sure that you have initialized these variables.
This is a long post, so I may have made some errors I didn't notice. If there is any other mistake I noticed, then please inform me in the comments.
Good luck!
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 6 years ago.
Improve this question
int f_point(int kek,int *lol) {
*lol *= *lol;
return kek;
}
int main {
int x;
std::cin >> x;
int *adress_of_x = &x;
int a,b = f_point(x,&x); //how does it work?
std::cout << a << LINE_JUMP;
std::cout << b << LINE_JUMP;
}
For example, if I give 2 to program then I will get 0 and 2. Why?
b = f_point(x,&x) in this statement value of first parameter is 2.
Your function is not changing the value of first parameter and returns the same value.
Your are passing first parameter by value so it has no relation with the updated value of x. Variable a is uninitialized, so it is taking a garbage value.
int a,b = f_point(x,&x); //how does it work?
The variable declaration leaves a uninitialized and initializes b from the result of f_point(x,&x);.
Since it's an uninitialized variable, accessing the value of a in the
std::cout << a << LINE_JUMP;
statement leads to undefined behavior of your program. Having an output of 0 is just one of any possibilities (including your fridge explodes unexpectedly or little demons flying out of your nostrils).
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;
}