-1

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;
 }
}
jxh
  • 69,070
  • 8
  • 110
  • 193
panos
  • 17
  • 3
  • 5
    Unlike python, the conditional statements after keywords like `while` and `if` must be fully enclosed in parentheses. – JohnFilleau Feb 21 '20 at 23:58
  • 1
    Note: The `>>` operator typically (and definitely in the case of a `std::string`) stops as and returns what it has gathered soon as it finds whitespace. This means `cin >> txt;` will read a single word, not a sentence. [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) is likely the best tool to use here. – user4581301 Feb 22 '20 at 00:15

3 Answers3

1

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.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • thank you so much for your fast reply. cant believe how much time i wasted on such a simple mistake – panos Feb 22 '20 at 00:01
  • @panos See my edit. If you don't have one already, use a good book that teaches you the syntax correctly. I don't know how you got the idea that there should be a semicolon after the condition or that there don't need to be parentheses around it. – walnut Feb 22 '20 at 00:03
1

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;
}
Philip Nelson
  • 1,027
  • 12
  • 28
0

Try surrounding the entire while condition with parentheses:

while ((txt == "") || (txt == " ")) {
    ...
}
Personage
  • 464
  • 4
  • 13