追踪奇怪的错误

发布于 2024-08-23 12:44:10 字数 1109 浏览 4 评论 0原文

我正在尝试做一些 C++ 练习,但在构建时遇到了错误,这不仅仅是我突然想到的。我缺少什么?在几年前完成之后,我刚刚从 C# 等回到 C++。

[错误] 语法错误:“返回” [/错误]

#include <iostream>
using namespace std;

/* Pre-compiler directives / macros */
#define isValidDrinkChoice(Choice,MaxNumDrinks) ((Choice < MaxNumDrinks) && (Choice > 0))

/* Primary Entry Point for Executable */
int main(const int & argc, char * argv[]){

    const int MaxNumDrinks = 4;
    char ** Drinks;
    Drinks = new char* [MaxNumDrinks];
    Drinks[0] = "Soda";
    Drinks[1] = "Water";
    Drinks[2] = "Coffee";
    Drinks[3] = "Tea";
    Drinks[4] = "Perrier Sparkling Water";

    int Choice = -1;
    do while(!isValidDrinkChoice(Choice, MaxNumDrinks)) {
        cout << "Please select your favorite drink\r\n\r\n" << endl;
        for (int x = 0; x < MaxNumDrinks; x++) cout << "\t" << Drinks[x] << endl;
        cin >> Choice;
        if (isValidDrinkChoice(Choice, MaxNumDrinks)) cout << "\r\n\r\n" << "You chose " << *Drinks[Choice] << endl;
    }
    return 0;
}

I'm trying to do some C++ exercises, but I'm running into a error on build, which doesn't just jump out at me. What am I missing? I'm just getting back to C++ from C# et al after having done it years ago.

[ERROR]
syntax error : 'return'
[/ERROR]

#include <iostream>
using namespace std;

/* Pre-compiler directives / macros */
#define isValidDrinkChoice(Choice,MaxNumDrinks) ((Choice < MaxNumDrinks) && (Choice > 0))

/* Primary Entry Point for Executable */
int main(const int & argc, char * argv[]){

    const int MaxNumDrinks = 4;
    char ** Drinks;
    Drinks = new char* [MaxNumDrinks];
    Drinks[0] = "Soda";
    Drinks[1] = "Water";
    Drinks[2] = "Coffee";
    Drinks[3] = "Tea";
    Drinks[4] = "Perrier Sparkling Water";

    int Choice = -1;
    do while(!isValidDrinkChoice(Choice, MaxNumDrinks)) {
        cout << "Please select your favorite drink\r\n\r\n" << endl;
        for (int x = 0; x < MaxNumDrinks; x++) cout << "\t" << Drinks[x] << endl;
        cin >> Choice;
        if (isValidDrinkChoice(Choice, MaxNumDrinks)) cout << "\r\n\r\n" << "You chose " << *Drinks[Choice] << endl;
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2024-08-30 12:44:10

我不认为 C++ 中有类似的 do while 。它是do { ... } while (表达式);。或者while (表达式) { ... }

I don't think there's a do while like that in C++. It's do { ... } while (expression);. Or while (expression) { ... }.

梦巷 2024-08-30 12:44:10

更正后的代码示例是需要替换的 while 循环

#include <iostream>

namespace {
  bool isValidDrinkChoice(int Choice, int MaxNumDrinks) {
    return ((Choice < MaxNumDrinks) && (Choice >= 0));
  }
}

/* Primary Entry Point for Executable */
int main() {
    using namespace std;   

    const char *Drinks[] = {
      "Soda", "Water", "Coffee", "Tea", "Perrier Sparkling Water" };
    const int MaxNumDrinks = sizeof(Drinks) / sizeof(*Drinks);

    int Choice = -1;
    do  {
        cout << "Please select your favorite drink\r\n\r\n" << endl;
        for (int i = 0; i < MaxNumDrinks; i++) 
          cout << Drinks[i] << endl;

        cin >> Choice;

        if (isValidDrinkChoice(Choice, MaxNumDrinks)) 
          cout << "\r\n\r\n" << "You chose " << Drinks[Choice] << endl;
    } while(!isValidDrinkChoice(Choice, MaxNumDrinks) && cin) ;

    return cin.good() ? 0 : 1;
}

The Corrected code sample its the while loop that needs replacement

#include <iostream>

namespace {
  bool isValidDrinkChoice(int Choice, int MaxNumDrinks) {
    return ((Choice < MaxNumDrinks) && (Choice >= 0));
  }
}

/* Primary Entry Point for Executable */
int main() {
    using namespace std;   

    const char *Drinks[] = {
      "Soda", "Water", "Coffee", "Tea", "Perrier Sparkling Water" };
    const int MaxNumDrinks = sizeof(Drinks) / sizeof(*Drinks);

    int Choice = -1;
    do  {
        cout << "Please select your favorite drink\r\n\r\n" << endl;
        for (int i = 0; i < MaxNumDrinks; i++) 
          cout << Drinks[i] << endl;

        cin >> Choice;

        if (isValidDrinkChoice(Choice, MaxNumDrinks)) 
          cout << "\r\n\r\n" << "You chose " << Drinks[Choice] << endl;
    } while(!isValidDrinkChoice(Choice, MaxNumDrinks) && cin) ;

    return cin.good() ? 0 : 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文