未声明的标识符 C2065 "P"
Undeclared Identifier C2065 "P"
我是一名新的编程学生,这是我第一次在这里发帖,如果我做错了,我深表歉意。我在下面附上了我的代码。我在尝试构建解决方案时收到两个错误。它们是:错误 C2065:'p':未声明的标识符。我很困惑为什么 p 未声明但 m 或 q 未声明。我正在使用 visual studio 2017。我知道这可能是一个简单的修复,但我是新手,仍在学习基础知识。
#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;
}
因为你用逗号分隔,所以编译器会认为代码为
unsigned char choice = p;
unsigned char m;
unsigned char q;
因此 p
未声明
我是一名新的编程学生,这是我第一次在这里发帖,如果我做错了,我深表歉意。我在下面附上了我的代码。我在尝试构建解决方案时收到两个错误。它们是:错误 C2065:'p':未声明的标识符。我很困惑为什么 p 未声明但 m 或 q 未声明。我正在使用 visual studio 2017。我知道这可能是一个简单的修复,但我是新手,仍在学习基础知识。
#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;
}
因为你用逗号分隔,所以编译器会认为代码为
unsigned char choice = p;
unsigned char m;
unsigned char q;
因此 p
未声明