-2

I am a new programming student and this is my first time posting here so I apologize if I did it wrong. I have attached my code below. I am receiving two errors when I try to build my solution. They are : error C2065: 'p': undeclared identifier. I'm confused why p is undeclared but not m or q. I am using visual studio 2017. I understand this may be a simple fix, but I'm new to this and still learning the basics.

#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;

}
Rose H.
  • 3
  • 1
  • `unsigned char choice = p`, what's the **p** – Ivien Oct 11 '18 at 03:23
  • apologies if I am wrong but shouldn;t this unsigned char choice = p, m , q; be unsigned char choice , p,m, q; and then again you are trying to compare choice against p , but have not assigned any value to p – Satya Oct 11 '18 at 03:24
  • I tried the format unsigned char choice, p, m, q and it gives me an error uninitialized local variable for p,m,q – Rose H. Oct 11 '18 at 03:28
  • I suspect `if (choice == p)` should be `if (choice == 'p')` and you do not need a variable `p` at all. – user4581301 Oct 11 '18 at 03:32
  • Now it builds fine, but when running there is a debug error that my variables kilometer, liter, kilograms are being used without being initialized? – Rose H. Oct 11 '18 at 04:08

1 Answers1

2

because you seperated with comman, so the compiler will consider the code as

unsigned char choice = p; 
unsigned char m;
unsigned char q;

So the p is undeclared

Ivien
  • 427
  • 2
  • 5