In my class, I am required to use the error checking libraries my instructor has provided for me (named isNumeric and toFloat; isNumeric checks numericy and toFloat converts a character array to a float)
Anyhow, his library can only be included in one area of the project, or else it creates
Code:
groceryinfo.obj : error LNK2005: "bool __cdecl isNumeric(char * const)" (?isNumeric@@YA_NQAD@Z) already defined in HW25.obj
groceryinfo.obj : error LNK2005: "float __cdecl toFloat(char * const)" (?toFloat@@YAMQAD@Z) already defined in HW25.obj
That's not usually too much of a problem - I simply organize my code so all my errorchecking is done in one file of the project. However, it's giving me the same error now and it is only being included in my .h class file, and it is not used/accessed/included ANYWHERE else inside the project. Here is where it is required (if it helps any - I doubt it will, but better safe than sorry):
Note: The errorchecking function coding is below this code in a different subset, so don't think it's done! Sorry for long coding, but I'm trying to provide as much info as possible.
Code:
public: //accessible outside the class
//structure
struct item_type{ //info for each item
char chName[20]; //name of the item
char chUPC[12]; //UPC label of the item
double dPrice; //price of the item
int iQuan; //in-stock quantity of the item
};
private: //accessible only within the class
friend istream &operator>>(istream &stream, item_type &grocery){ //allows easier cin for inventory input
bool blnCheck; //boolean to handle error checking
char chPrice[10]; //character array for price to send to isNumeric
char chQuan[10]; //character array for stock to send to isNumeric
system("cls"); //clears the screen
cout << "Input an item to the database\n=============================\n\n";
//inputs item name
cout << "Item name: ";
cin >> grocery.chName;
//inputs item UPC
do{
cout << "UPC # for " << grocery.chName << ": ";
cin >> grocery.chUPC;
if(isNumeric(grocery.chUPC)==false){ //if the number isn't numeric
cout << "Not a numeric number. Enter a valid UPC.\n";
blnCheck=false; //sets errorchecking flag to false
}
else{
blnCheck=true; //sets errorchecking flag to true
}
}
while(blnCheck==false); //do loop while the flag is set to false
//inputs item price
do{
cout << "Price for " << grocery.chName << ": ";
cin >> chPrice;
if(isNumeric(chPrice)==false){ //if the number isn't numeric
cout << "Not a numeric number. Enter a valid price.\n";
blnCheck=false; //sets errorchecking flag to false
}
else{
blnCheck=true; //sets errorchecking flag to true
grocery.dPrice = toFloat(chPrice); //converts to float and stores in structure
}
}
while(blnCheck==false); //do loop while the flag is set to false
//inputs item stock
do{
cout << "In-stock quantity of " << grocery.chName << ": ";
cin >> chQuan;
if(isNumeric(chQuan)==false){ //if the number isn't numeric
cout << "Not a numeric number. Enter a valid price.\n";
blnCheck=false; //sets errorchecking flag to false
}
else{
blnCheck=true; //sets errorchecking flag to true
grocery.iQuan = toFloat(chQuan); //converts to float and stores in structure
}
}
while(blnCheck==false); //do loop while the flag is set to false
return stream;
}
My question is this: is there any way to get rid of the error (I'm assuming by somehow making these functions below "multithreaded" or whatnot?) Now here is the code for the functions isNumeric and toFloat:
Code:
#include <math.h>
struct EvCCStrings {
char text[20];
};
bool isNumeric (char theString[]){ //returns a boolean statement and receives a character array
int iNumofChar = 0;
int iFind = 0;
int iDot = 0;
for (int iA = 0;iFind == 0;iA ++ ){ //do until the end of the array is found
if (theString[iA] == '\0') { //if the letter in the array is the terminating value...
iFind = 1; //the end was found
}
else { //if it equals something else
iNumofChar ++; //the array is at least 1 character bigger
}
}
for (iA = 0; iA < iNumofChar; iA ++){ //checks each character in the array
if (theString[iA] == '.'){ //if the character is a decimal point
iDot ++; //increase iDot by 1
if (iDot > 1) { //if more than one decimal point was found
return (false); //return as a false
}
}
else if (theString[iA] <= 47 || theString[iA] > 57){
return (false);
}
}
return (true); //returns as true if other statements passed
}
float toFloat (char theString[]){
int iDecPlace = -1;
int iNumofChar = 0;
int iFind = 0;
int iPlaces = 0;
float fResult = 0;
if (isNumeric (theString)){
for (int iA = 0;iFind == 0;iA ++ ){
if (theString[iA] == '\0') {
iFind = 1;
}
else {
iNumofChar ++;
}
}
for (iA = iNumofChar - 1; iA > -1; iA --) {
if (theString[iA] == '.') {
iDecPlace = iA;
}
else {
fResult += (theString[iA] - 47 - 1) * (float) pow (10, iPlaces);
iPlaces ++;
}
}
if (iDecPlace != -1){
fResult = fResult / (float) pow (10.0, iNumofChar - iDecPlace - 1);
}
return (fResult);
}
else {
return (0);
}
}