我是一名编程专业的新学生,这是我第一次在这里发布文章,因此,如果我做错了,我表示歉意。我在下面附加了我的代码。尝试构建解决方案时出现两个错误。它们是:错误C2065:'p':未声明的标识符。我很困惑为什么未声明p而不是m或q。我正在使用Visual Studio2017。我知道这可能是一个简单的修复程序,但是我对此并不陌生,仍然在学习基础知识。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//declare variables//
int kilogram, kilometer, liter;
unsigned char choice = p, m , q;
double pounds, miles, quarts;
//create menu for user//
cout << "Please enter your choice from the menu below\n"
<< "(p)ounds to kilograms\n"
<< "(m)iles to kilometers\n"
<< "(q)uarts to liters\n"
<< "Please enter your conversion choice\n";
cin >> choice;
//validate choice//
if (choice != 'p' && choice != 'm' && choice != 'q')
{
cout << "Invalid Choice\n"
<< "Please enter choice" << endl;
cin >> choice;
}
//Make conversion//
if (choice == p)
{
//Get metric value//
cout << "Please enter the kilogram value\n";
cin >> kilogram;
}
//calculate conversion//
else
{
pounds = kilogram * 2.2046;
cout << kilogram << "kilograms is" << pounds << "Pounds" << endl;
}
//Validate input//
if (kilogram < 1)
{
cout << "Invalid Input" << endl;
cout << "Enter the kilogram value" << endl;
cin >> kilogram;
}
if (choice == m)
{
//Get metric value//
cout << "Please enter the kilometer value\n";
cin >> kilometer;
}
//calculate conversion//
else
{
miles = kilometer * 0.621388;
cout << kilometer << "kilometer is " << miles << "miles" << endl;
}
//Validate Input//
if (kilometer < 1)
{
cout << "Invalid Input" << endl
<< "Enter the kilometer value" << endl;
cin >> kilometer;
}
//Make conversion
if (choice == q)
{
//Get metric value//
cout << "Please enter the liter value\n";
cin >> liter;
}
//Calculate conversion
else
{
quarts = liter * 0.877193;
cout << liter << "liter is" << quarts << "quarts" << endl;
}
//Validate input//
if (liter < 1)
{
cout << "Invalid Input" << endl
<< "Enter the liter value" << endl;
cin >> liter;
}
return 0;
}
答案 0 :(得分:2)
因为您用逗号分隔,所以编译器会将代码视为
unsigned char choice = p;
unsigned char m;
unsigned char q;
因此p
是未声明的