c++期望的主表达式

c++ expected primary expression before

本文关键字:表达式 期望 c++      更新时间:2023-10-16

我试着创建这个

#include <iostream>
using namespace std;
int main()
{
    int right=0 , left=0;
    cout << "do you go left or right?" << endl;
    if (<right>) {
        <you meet a troll>
    }
    else {
        <you meet a witch>
    }
}

但是我写

时会出现很多错误

primary expression before);之前& lt;

你能帮帮我吗?

就像其他人说的,你真的需要做一些基础的工作。那里有很多资源,但是如果您想看到您的示例工作,下面的代码将做到这一点。我想你这里缺少的键是std::cin,用于从键盘获得响应:

#include <iostream>
int main()
{
  std::cout << "do you go left or right?" << std::endl;
  std::string direction;
  std::cin >> direction;
  if(direction.compare("right") == 0)
  {
     std::cout << "you encountered a troll" << std::endl;
  }
  else
  {
     std::cout << "you encountered a witch" << std::endl;
  }
  return 0;
}

我重新创建了这个

#include <iostream>
#include <string>
using namespace std;
int main() {
    std::string right;
    cout << "do you go left or right?" << endl;
    cin >> right;
    if (right == "right") {
        cout << "<you meet a troll>";
    }
    else if (right == "left") {
        cout << "<you meet a witch>";
    }
}