expected primary-expression before '||' [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
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 == " ")) {
...
}

Related

Assign a function to a new variable [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
Why can't I assign my new_age(age) to another variable (in this case it will be new)?
I get the following errors:
main.cpp:21:9: error: expected type-specifier before ‘=’ token
21 | new = new_age(age)
| ^
This is my code:
#include <iostream>
using namespace std;
int new_age (int & age)
{
return (age + 100);
}
int main()
{
int age {};
new = new_age(age)
cout << "How old are you "
cin >> age
cout << "In 100 years you will be " << new:
return 0;
}
The word new is a keyword in C++. It has a special meaning and cannot be used as name of a variable.
For a full list of all keywords see here.
As an aside, even if new was allowed as an identifier, you have not declared it. To declare and initialize it directly with the value you need to add the variable's type in front of the identifier:
int not_new = new_age(age);
(Also note the semicolon at the end of the declaration which you forgot or mistyped in a lot of statements as well.)
It also seems that you'd want to call new_age(age) after taking input from the user.

Why is condition with `cout` evaluated to true? [closed]

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.

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.

expected a; but I already have a ; [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
hello I am trying to do my college homework but I keep getting an error saying expected a ; but I already have a ; on that line. my error is during the cin answer
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Welcome to the Prison of Elders, Guardian, what is your name ?.";
cin >> name;
cout << "are you ready to face a challenge" << name << "!" << endl;
cin answer;
while (1);
return 0;
cin answer;
should be replaced by
cin >> answer;
More generally, the "expected thing" as compiler message should always be treated with caution. It's just some guess that matches the syntax from the compiler. However, it is always the sign of a syntax error somewhere. (And not necessarily at the line where you see the "expected thing")
To elaborate on why ";" was expected :
cin is an identifier, as well as answer
You wrote identifier identifier;, which is never a correct syntax in C++.
the compilator was confused by seeing two identifiers next to each other. So it suggested this :
cin; answer;
which is syntactically correct and solve the confusion, but complete nonsense semantically in your code.

How to write an if-else statement 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 am very new to C++. My objective is to write the following logic:
if a = yes then print "ok", else return 0
Here is my code so far:
int a;
cin>>a;
if (a = "Yes") { // Error right here
cout<< "ok"; << endl;
}else{
return 0;
}
First of all, what do you want your program to do?
You need to distinguish assignment and equal to operator.
Please note that you need to understand the basics before proceeding to perform conditional statements.
A reasonable program should go like this:
int a;
cin>>a;
if (a == 5) { // 5 is an integer
cout<< "You entered 5!" << endl; // no semicolon after "
}
return 0; // must be out of the else statement
= assigns things.
Use == to compare, however you are comparing an int with a string.
If you are not careful, you compare the address of a char * with a number when dealing with strings.
Use a std::string instead.
#include <string>
//.... some context I presume
std::string a;
cin >> a;
if (a == "Yes") { // Error right here
cout<< "ok"; << endl;
}else{
return 0;
}
There are multiple errors in this code.
You need to use the comparison operator in your condition. This is denoted by the double equal sign "==". Your code is using assignment "=" which tries to assign the value "Yes" to the variable a. This is a common error in C/C++ so you need to be careful whenever you compare things.
The other error is that you have declared the variable a to be an integer, so you will get a type mismatch error when you try to compile because "Yes" is a string.
Your code is incorrect in terms of data types. You have a variable 'a' of type int which you are comparing to string "yes". Try to see it from a logical point of view; you can compare:
2 numbers (for example, 2 is greater than 1)
2 strings (for example, "food" is not the same word as "cat")
Etc...
In your case, you are comparing a number inputted(let's assume 5) to a word "yes". When you try to input a letter for var a, you will get a compilation error. Therefore, simply change the following:
string a;
Another problem with your code is when the if-then loop checks the condition; a comparison operator is 2 equal signs next to each other instead of a single equal sign. A single equal sign assigns the item on the right to the item on the left. For example, in:
int num = 5;
The variable num is assigned 5. But you want to make a comparison, not assign the variable its own condition!
Your loop is always true because you set the variable to the condition it is supposed to meet. You also need to do the following:
if (a == "yes")
This compares the value stored in var a to the value on the right side of the == .
Just some advice, I would recommend you to get some good books on c++. Search them online. You can also take online programming courses on edx, course record, etc... . There are a lot of other free learning resources online too which you can make use of. You may also want to dive into a simpler programming language; I would recommend scratch. It gives you a very basic idea about programming and can be done in less than a week.
** Note that I feel this is the simplest way; however, you can also set type of a to a char, accept input and then convert it back to a string. Good luck!