我正在尝试做一些C ++练习,但是我在构建时遇到了错误,这不仅仅是跳出来。我错过了什么?在几年前完成它之后,我只是从C#等回到C ++。
[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;
}
答案 0 :(得分:6)
我不认为C ++中有do while
这样的东西。它是do { ... } while (expression);
。或while (expression) { ... }
。
答案 1 :(得分:3)
更正后的代码采样需要替换的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;
}