UndeclaredIdentifier
Feb 6, 2013 at 2:06am UTC
Hello, im trying to compile this code but it keeps failing due to the Y being an undeclared identifier even though i declare answer as a local variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
// Local Variables
char answer;
float numberOfPrices;
float sumInEuros;
//Prototypes
void processAPrice( float );
void produceFinalData();
numberOfPrices = 0;
sumInEuros = 0;
answer = Y;
while ( answer = Y)
{
processAPrice( numberOfPrices);
numberOfPrices = ++numberOfPrices;
cout << "\nContinue (Y/N)?" ;
cin >> ( answer);
}
if (numberOfPrices > 0)
{
produceFinalData();
}
system("PAUSE" );
return ( 0);
}
What am i doing wrong?
Last edited on Feb 6, 2013 at 2:06am UTC
Feb 6, 2013 at 2:14am UTC
im trying to compile this code but it keeps failing due to the Y being an undeclared identifier even though i declare answer as a local variable:
What does the declaration of
answer have to do with
Y being an undeclared identifier?
I believe you wanted to use
'Y' and probably
== rather than
=
Feb 6, 2013 at 2:35am UTC
Im trying to assign the variable 'answer' to Y, why would i want to use the equality operator?
Feb 6, 2013 at 2:40am UTC
1 2 3 4 5 6 7 8 9 10 11
answer = Y; //Use 'Y' here, not Y. Y would refer to a variable called Y. 'Y' is a char.
while ( answer = Y) //This is always true. (If you change it to 'Y'). The = operator
//returns the value assigned, and anything non-zero is considered true.
//So cire is right, you need ==.
{
processAPrice( numberOfPrices);
numberOfPrices = ++numberOfPrices;
cout << "\nContinue (Y/N)?" ;
cin >> ( answer);
}
Feb 6, 2013 at 2:52am UTC
Ahhhhhhh right, a bit of a "duh" moment...
Thanks for that.
With that small blip out of the way, could either of you tell me why its not compiling still. Heres the full program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
// Local Variables
char answer;
float numberOfPrices;
float sumInEuros;
//Prototypes
void processAPrice( float );
void produceFinalData();
numberOfPrices = 0;
sumInEuros = 0;
answer = 'Y' ;
while ( answer == 'Y' )
{
processAPrice( numberOfPrices);
numberOfPrices = ++numberOfPrices;
cout << "\nContinue (Y/N)?" ;
cin >> ( answer);
}
if (numberOfPrices > 0)
{
produceFinalData();
}
system("PAUSE" );
return ( 0);
}
void processAPrice()
{
//Local Variables
float priceInPounds;
float priceInEuros;
float sumInEuros;
float numberOfPrices;
//Prototypes
void getPriceInPounds( float &);
void convertPriceIntoEuros( float , float &);
void showPriceInEuros( float , float );
void calculateSum( float , float &);
void produceFinalData( float , float );
//Calls
getPriceInPounds( priceInPounds);
convertPriceIntoEuros( priceInPounds, priceInEuros);
showPriceInEuros( priceInPounds, priceInEuros);
calculateSum( priceInEuros, sumInEuros );
produceFinalData( numberOfPrices, sumInEuros );
return ;
}
void getPriceInPounds( float & priceInPounds)
{
assert( priceInPounds >= 0);
cout << ("\nEnter a price (in pounds): " );
cin >> priceInPounds;
}
void convertPriceIntoEuros( float priceInPounds, float & priceInEuros )
{
assert((priceInPounds >= 0) && ( priceInEuros >= 0));
float conversionRate = 0.82f;
priceInEuros = ( priceInPounds / conversionRate);
}
void showPriceInEuros( float priceInPounds, float priceInEuros )
{
assert((priceInPounds >= 0) && ( priceInEuros >= 0));
cout << "\nThe euro value of £" << priceInPounds << "is \u20AC" << priceInEuros;
}
void calculateSum( float priceInEuros, float & sumInEuros)
{
assert( sumInEuros >= 0);
sumInEuros = (sumInEuros + priceInEuros);
}
void produceFinalData( float numberOfPrices, float sumInEuros )
{
assert((numberOfPrices >= 0) && ( sumInEuros >= 0));
cout << "The total sum is: " << sumInEuros << "Euros" ;
cout << "The average is: " << sumInEuros / numberOfPrices;
}
and heres the error i get:
1 2 3
1>Task4Wed.obj : error LNK2019: unresolved external symbol "void __cdecl produceFinalData(void)" (?produceFinalData@@YAXXZ) referenced in function _main
1>Task4Wed.obj : error LNK2019: unresolved external symbol "void __cdecl processAPrice(float)" (?processAPrice@@YAXM@Z) referenced in function _main
1>E:\Uni Work\Fundamentals of Programming Work\TestProject\Debug\TestProject.exe : fatal error LNK1120: 2 unresolved externals
Feb 6, 2013 at 4:57am UTC
When the compiler encounters the two function calls in
main() it hasn't yet seen their definitions. You need to add forward declarations for the functions before
main() .
Also, you need to move the function prototypes in
processAPrice() out of the function and before
main() .
1 2 3 4 5 6 7 8 9 10 11 12 13
void processAPrice();
void produceFinalData( float numberOfPrices, float sumInEuros );
void getPriceInPounds( float &);
void convertPriceIntoEuros( float , float &);
void showPriceInEuros( float , float );
void calculateSum( float , float &);
void produceFinalData( float , float );
int main()
{
...
}
I haven't looked for other errors.
Topic archived. No new replies allowed.