1

编译时没有错误......但我的输出一无所获。我知道这是一个非常愚蠢的编码尝试......我主要是在处理数字......字符串的东西让我感到困惑。

#include <iostream>
#include <string>

using namespace std;
char chr;

int main()
{
    string name;
    string artist;
    string Taylor;
    string Alan;
    string general;
    string box;
    string seating;

    cout << "Enter your name followed by the return key" << endl;
    cin >> name;
    cout << endl;
    cout << "Enter Taylor for Taylor Swift tickets or Alan for Alan Jacskon seats, followed" << endl << "by the enter key" << endl << endl;
    cout << "Enter box for box seating or general for general seating, followed by the enter key" << endl << endl;
    cin >> artist;
    cin >> seating;

    if ( artist + seating == Taylor + general )
        cout <<  name << ", Your ticket will cost $60" << endl;
    else if ( artist + seating == Taylor + box )
        cout <<  name << ", Your ticket will cost $100" << endl;
    else if ( artist + seating == Alan + box )
        cout <<  name << ", Your ticket will cost $75" << endl;
    else if ( artist + seating == Alan + general )
        cout <<  name << ", Your ticket will cost $50" << endl;

    cin >> chr;
    return 0;
}
4

4 回答 4

2

Taylor, Alan,general并且box在您的逻辑文字值中,而不是变量...您的工作代码应该变成

string Taylor = "Taylor";
string Alan = "Alan";
string general = "general";
string box = "box";

不幸的是,在 C++ 中你不能只写

if ( artist + seating == "Taylor" + "general" )

由于此处无法解释的原因过于复杂,您可以改为摆脱这些值的变量并使用

if ( artist + seating == "Taylorgeneral" )

顺便说一句:我不确定 C++ 是否是学习编程的好选择。在我看来,更适合这项工作的将是Python(或为像 6502 这样的简单 CPU 编写汇编程序,但这是另一种完全不同的方法)。

于 2013-10-19T06:09:53.763 回答
1

字符串Taylor, Alan, general, box永远不会被初始化。比较它们是没有意义的。你可能想要的是:

string Taylor = "Taylor";
string Alan = "Alan";
string general = "general";
string box = "box";
于 2013-10-19T06:08:32.413 回答
0

当我将您的 if 语句更改为这种格式时,它们起作用了。

if (( 艺术家 == "泰勒") && (座位 == "一般"))

  cout <<  name << ", Your ticket will cost $60" << endl;
于 2013-10-19T06:21:26.850 回答
0

你还没有初始化字符串 Taylor;字符串艾伦;字符串一般;字符串框;所以它们都是 ='\0' ,它不匹配任何大小写,所以你没有输出。什么,当你使用cin作为输入时,它会忽略'\n','',你也可以使用cin.getline(string a)作为输入

于 2013-10-19T06:36:03.460 回答