【问题标题】:C++ - User Input and Output using Switch? [closed]C++ - 使用开关的用户输入和输出? [关闭]
【发布时间】:2012-09-23 19:19:05
【问题描述】:

所以我正在尝试创建一个程序,该程序本质上会根据用户的选择要求用户提供各种输入和输出。我正在使用 switch 方法以及 cin 来执行此操作。出于某种原因,它不允许我编译。谁能提供有关我在这里做错了什么的见解?谢谢!

   #include <iostream>
          using namespace std;
     int main()
     {
     cout << "Welcome to my countertop store.\n" << "Before running this program you will need        the depth and 
     length of the block, which edges are to be finished and 
     the style, as well as the grade to be used 
     and the finish.\n";

     cout << "Please enter the depth of the counter: ";
     cin << counterDEPTH;
     cout << "Please enter the length of the counter: ";
     cin << counterLENGTH;
     cout << "Number of long edges to be finished: ";
     cin << longEDGES;
     cout << "Number of short edges to be finished: ";
     cin << shortEDGES;
     cout << "Please choose a grade: \n";
     switch (grade)
       {
    case Marble:
        double cost = 92.99;
    case Granite:
        double cost = 78.99;
    case Quartz:
        double cost = 56.99;
    case Quartite:
        double cost = 39.99;
}
     cout << "Selected grade: " & grade;
     cout << "Would you like it polished (y/n)? ";
     switch (polished)
       {
              case Yes:
    double newcost = cost;
    double newcost = (.15 * newcost);
              case No:
    double newcost = 0.00;
    }
     cout << "Cost Information\n";
     cout << "Stone Cost: " & cost;
     cout << "Polishing Cost: " & newcost;
     cout << "Edge Cost: " & (longEdges * shortEdges * 4.99);
     cout << "Total: " & cost+newcost+(longEdges * shortEdges * 4.99); 
     return 0;
     }

【问题讨论】:

  • 你可以谈论“它不让你编译的原因”吗?它会关闭您附近的电源吗?是不是把你老公当人质了?它会用电子击中你的脸吗?
  • 您不能使用&amp; 链接输出。你需要更多&lt;&lt;
  • double newcost = cost; double newcost = (.15 * newcost); 还有一个。
  • -1,抱歉,这段代码有太多问题。

标签: c++ input switch-statement


【解决方案1】:

首先,您需要声明几乎所有变量。 C 和 C++ 都要求在使用变量之前声明变量,所以到目前为止几乎所有的语句都是非法的。

仅仅看一眼你的节目,似乎还有许多其他问题也可能会破坏它。在这一点上,如果您尝试自己使用编译器的输出进行调试,它将更加有用,因为我确信到目前为止我发现的所有错误都会被任何编译器发现。

如果您仍然遇到问题,那么您需要提出更具体的问题,最好连同编译器的输出\错误消息一起提出。

【讨论】:

  • 谢谢。我会检查一下并回复你。
【解决方案2】:
cin << counterDEPTH;

改成

cin >> counterDEPTH;

并以类似方式改变其他人

【讨论】:

    【解决方案3】:

    这是您的全部代码吗?您需要声明要存储用户输入的变量。此外,Yes/No/Marble/Granite/Quartz/Quartite 也没有在任何地方定义。

    #include <iostream>
    using namespace std;
    int main()
    {
    cout << "Welcome to my countertop store.\n" << "Before running this program you will need        the depth and 
    length of the block, which edges are to be finished and 
    the style, as well as the grade to be used 
    and the finish.\n";
    
    double counterDEPTH, counterLENGTH, longEDGES, shortEDGES;
    
    cout << "Please enter the depth of the counter: ";
    cin >> counterDEPTH;
    

    你的 switch 语句也需要中断。

    case Marble:
        double cost = 92.99;
        break;
    case Granite:
        double cost = 78.99;
        break;
    

    【讨论】:

      【解决方案4】:
      1. cin 语句更改为&gt;&gt;,例如

        cin >> 计数器深度;

      2. switch 语句的每个case 块的末尾添加break;

      【讨论】:

        【解决方案5】:

        你已经用&lt;&lt; 写了cincoutcin 必须使用 &gt;&gt;

        【讨论】:

          猜你喜欢
          • 2011-12-10
          • 1970-01-01
          • 1970-01-01
          • 2016-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-03
          相关资源
          最近更新 更多