这是我第一次尝试 C++,下面是一个通过控制台应用程序计算小费的示例。完整的(工作代码)如下所示:
// Week1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// Declare variables
double totalBill = 0.0;
double liquour = 0.0;
double tipPercentage = 0.0;
double totalNoLiquour = 0.0;
double tip = 0.0;
string hadLiquour;
// Capture inputs
cout << "Did you drink any booze? (Yes or No)\t";
getline(cin, hadLiquour, '\n');
if(hadLiquour == "Yes") {
cout << "Please enter you booze bill\t";
cin >> liquour;
}
cout << "Please enter your total bill\t";
cin >> totalBill;
cout << "Enter the tip percentage (in decimal form)\t";
cin >> tipPercentage;
// Process inputs
totalNoLiquour = totalBill - liquour;
tip = totalNoLiquour * tipPercentage;
// Output
cout << "Tip: " << (char)156 << tip << endl;
system("pause");
return 0;
}
这工作正常。但是,我想搬家:
cout << "Please enter your total bill\t";
cin >> totalBill;
成为第一行:
// Capture inputs
但是当我执行应用程序中断时(它会编译,但只是忽略 if 语句,然后同时打印两个 cout。
我挠头,因为我不明白发生了什么——但我假设我是个白痴!
谢谢