我有我的switch语句的问题// number of crowns
和// frame color
如何解决这个问题:C++ switch语句
这是我的输出
我怎样才能让我的cout
输出frame_color
和number_crowns
(上// number of crowns
和// frame color
)?
BTW即时通讯使用CodeBlocks
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int length, width, number_crowns;
double cost;
int frameType;
int yes_no;
int colorSelection;
string frame_color;
string type;
cout << "Input length and width of the picture: ";
cin >> length >> width;
cout << "1(spacebar)regular" << endl
<< "2(spacebar)fancy" << endl
<< "(regular/fancy)Enter type of frame: ";
cin >> frameType >> type;
cout << "1 = with" << endl
<< "2 = without" << endl
<< "(with/without)With color/Without?";
cin >> colorSelection;
cout << "1 = yes" << endl
<< "2 = no" << endl
<< "(yes/no)Do you want to put crowns? ";
cin >> yes_no;
cost = (2 * 0.1) * (length + width); // cost in length and width of the frame
cost += (0.02 * (length * width)); // cost per square inch
switch(frameType)
{ // type of frame
case '1':
cost += 0.15;
break;
case '2':
cost += 0.25;
break;
default:
cout << "wrong input!";
}
switch(colorSelection)
{ // frame color
case '1':
cost += 0.10;
cout << "Enter desired frame color: ";
cin >> frame_color;
break;
case '2':
cost += 0;
break;
default:
cout << "wrong input!";
}
switch(yes_no)
{ // number of crowns
case '1':
cout << "Input number of crowns: ";
cin >> number_crowns;
cost += (number_crowns * 0.35);
break;
case '2':
cost =+ 0;
break;
default:
cout << "wrong input!";
}
cout << endl;
cout << "\n\nFrame type: " << type << "\n" << endl;
cout << "Frame colour: " << frame_color << "\n" << endl;
cout << "Total frame cost: " << cost << "\n" << endl;
}
非常感谢!
最新升级程序w^*** wahahaha
我怎样才能让我的cout
输出frame_color
和number_crowns
(上// number of crowns
和// frame color
)?
#include <iostream>
using namespace std;
int main()
{
int length, width, number_crowns;
double cost;
char frameType;
char yes_no;
char colorSelection;
string frame_color;
string type;
cout << "Input length and width of the picture: ";
cin >> length >> width;
cout << "(regular/fancy) Enter type of frame: ";
cin >> frameType >> type;
cout << "(with/without) With color/Without?";
cin >> colorSelection;
cout << "(yes/no) Do you want to put crowns? ";
cin >> yes_no;
cost = (2 * 0.1) * (length + width); // cost in length and width of the frame
cost += (0.02 * (length * width)); // cost per square inch
cout << "\nFrame type: " << frameType << type << "\n" << endl;
switch(frameType)
{ // type of frame
case 'regular':
cost += 0.15;
break;
case 'fancy':
cost += 0.25;
break;
default:
cout << "wrong input!";
}
cout << "\nFrame colour: " << frame_color << "\n" << endl;
switch(colorSelection)
{ // frame color
case 'with':
cost += 0.10;
cout << "Enter desired frame color: ";
cin >> frame_color;
break;
case 'without':
cost += 0;
break;
default:
cout << "wrong input!";
}
switch(yes_no)
{ // number of crowns
case 'yes':
cout << "Input number of crowns: ";
cin >> number_crowns;
cost += (number_crowns * 0.35);
break;
case 'no':
cost =+ 0;
break;
default:
cout << "wrong input!";
}
cout << endl;
cout << "\nTotal frame cost: " << cost << "\n" << endl;
}
你的'case'标签是错误的 - 你正在比较'int'值和'char'文字 - 去除单引号,即case'1':' - >'case 1:'。另外,为了将来的参考,您可能想了解如何使用调试器来遍历代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。进一步阅读:[如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –
请停止发布文字图片,将文字改为文字。 –
oooh非常感谢! – Vadamadafaka