Question:
C++ Helper/Header file Problem?
kimballclay
2010-05-26 08:42:50 UTC
I'm working on a binary conversion software but my header file is refusing to compile.

Code:

#ifndef LETTERS_H_INCLUDED
#define LETTERS_H_INCLUDED
#include

string letters(string lla)
{
if(lla == "1100001")
{
lla = "a";
}
else if(lla == "1100010")
{
lla = "b";
}
else if(lla == "1100011")
{
lla = "c";
}
else if(lla == "1100100")
{
lla = "d";
}
else if(lla == "1100101")
{
lla = "e";
}
else if(lla == "1100110")
{
lla = "f";
}
else if(lla == "1100111")
{
lla = "g";
}
else if(lla == "1101000")
{
lla = "h";
}
else if(lla == "1101001")
{
lla = "i";
}
else if(lla == "1101010")
{
lla = "j";
}
else if(lla == "1101011")
{
lla = "k";
}
else if(lla == "1101100")
{
lla = "l";
}
else if(lla == "1101101")
{
lla = "m";
}
else if(lla == "1101110")
{
lla = "n";
}
else if(lla == "1101111")
{
lla = "o";
}
else if(lla == "1110000")
{
lla = "p";
}
else if(lla == "1110001")
{
lla = "q";
}
else if(lla == "1110010")
{
lla = "r";
}
else if(lla == "1110011")
{
lla = "s";
}
else if(lla == "1110100")
{
lla = "t";
}
else if(lla == "1110101")
{
lla = "u";
}
else if(lla == "1110110")
{
lla = "v";
}
else if(lla == "1110111")
{
lla = "w";
}
else if(lla == "1111000")
{
lla = "x";
}
else if(lla == "1111001")
{
lla = "y";
}
else if(lla == "1111010")
{
lla = "z";
}
else if(lla == "1000001")
{
lla = "A";
}
else if(lla == "1000010")
{
lla = "B";
}
else if(lla == "1000011")
{
lla = "C";
}
else if(lla == "1000100")
{
lla = "D";
}
else if(lla == "1000101")
{
lla = "E";
}
else if(lla == "1000110")
{
lla = "F";
}
else if(lla == "1000111")
{
lla = "G";
}
else if(lla == "1001000")
{
lla = "H";
}
else if(lla == "1001001")
{
lla = "I";
}
else if(lla == "1001010")
{
lla = "J";
}
else if(lla == "1001011")
{
lla = "K";
}
else if(lla == "1001100")
{
lla = "L";
}
else if(lla == "1001101")
{
lla = "M";
}
else if(lla == "1001110")
{
lla = "N";
}
else if(lla == "1001111")
{
lla = "O";
}
else if(lla == "1010000")
{
lla = "P";
}
else if(lla == "1010001")
{
lla = "Q";
}
else if(lla == "1010010")
{
lla = "R";
}
else if(lla == "1010011")
{
lla = "S";
}
else if(lla == "1010100")
{
lla = "T";
}
else if(lla == "1010101")
{
lla = "U";
}
else if(lla == "1010110")
{
lla = "V";
}
else if(lla == "1010111")
{
lla = "W";
}
else if(lla == "1011000")
{
lla = "X";
}
else if(lla == "1011001")
{
lla = "Y";
}
else if(lla == "1011010")
{
lla = "Z";
}
else if(lla == "1011111")
{
lla = "_";
}
else if(lla == "0101101")
{
lla = "-";
}
else if(lla == "0101110")
{
lla = ".";
}
else if(lla == "0101100")
{
lla = ",";
}
else
{
lla = " ";
}
return lla;
}

#endif // LETTERS_H_INCLUDED

The file refuses to use string as a function or variable type. I have included but it will not recognize it. pointers?
Three answers:
?
2010-05-26 17:25:27 UTC
The class is called std::string.

Either write std::string wherever you wrote string, or write "using std::string;" after #include.

(you could also write "using namespace std;" except it shouldn't be done in headers)

And consider refactoring, that sequence of ifs is bad style.
Vladimir F
2010-05-26 10:26:44 UTC
Depending on the environment you are using your header path might not be set up properly. Try including string.h instead of string. It should work, but in my experience with different compilers and and environments that seems to clear up that issue.



Also, you might want to take the #include for out of the ifdef block, but it doesn't look like it should make a difference, unless you have other code that uses that header that is not displayed here.
?
2016-06-03 03:12:47 UTC
I cannot tell exactly how the program should work because you did not provide enough information. Regardless, I can help explain how to do simple file operations using C. Remember to include the stdio.h library.. The following is used to open a file: FILE* myfile = fopen("Expenses.txt", mode) ...where mode is a string to determine how the file should be opened: r read text mode w write text mode (truncates file to zero length or creates new file) a append text mode for writing (opens or creates file and sets file pointer to the end-of-file) rb read binary mode wb write binary mode (truncates file to zero length or creates new file) ab append binary mode for writing (opens or creates file and sets file pointer to the end-of-file) r+ read and write text mode w+ read and write text mode (truncates file to zero length or creates new file) a+ read and write text mode (opens or creates file and sets file pointer to the end-of-file) r+b or rb+ read and write binary mode w+b or wb+ read and write binary mode (truncates file to zero length or creates new file) a+b or ab+ read and write binary mode (opens or creates file and sets file pointer to the end-of-file) Note that this function will return NULL if there is a failure opening the file for the mode specified. Use int fgetc(FILE*) to get the current character at the file pointer and move the pointer forward. Use int fputc(char, FILE*) to write the specified char at the file pointer and move the pointer forward. Check out the source supplied for a good reference on C-language programming. Here is an example of something similar to what you are doing: //include #include int main(int argc, char *argv[]) { //declare variables to be used FILE* expfil1; FILE* expfil2; char* filname1 = "Expenses.txt"; char* filname2 = "Expenses2.txt"; int gamecount = 4; double tmpexpense = 0.0; double maxexpense = 50000.0; double avgexpense = 0.0; char tmpdat = 0; int i = 0; //attempt to open the 1st file to read existing data expfil1 = fopen(filname1, "r"); if (expfil1 != NULL){ //open was successful //attempt to open the 2nd file for writing expfil2 = fopen(filname2, "a"); if (expfil2 == NULL){ printf("Unexpected error while opening the file %s for writing!\nClosing program...\n", filname2); fflush(stdout); return -1; } //copy the data 1 char t a time until End Of File reached or Error tmpdat = fgetc(expfil1); while (tmpdat != EOF){ if (fputc(tmpdat, expfil2) == EOF){ printf("Unexpected error while writing to the file %s!\nClosing program...\n", filname2); fflush(stdout); return -1; } tmpdat = fgetc(expfil1); } //attempt to close the 2nd file if (fclose(expfil2) == EOF){ printf("Error closing file the file %s!\n", filname2); fflush(stdout); } //attempt to close the 1st file if (fclose(expfil1) == EOF){ printf("Error closing file the file %s!\n", filname1); fflush(stdout); } }else{ //open failed } //attempt to open the 1st file for writing expfil1 = fopen(filname1, "w"); if (expfil2 == NULL){ printf("Unexpected error while opening the file %s for writing!\nClosing program...\n", filname1); fflush(stdout); return -1; } //ask the user to input the 4 expenses for (i=0; i= 0.8){ printf("Very Expensive"); }else if(tmpexpense >= 0.6 && tmpexpense < 0.8){ printf("Expensive"); }else if(tmpexpense >= 0.5 && tmpexpense < 0.6){ printf("Less Expensive"); }else if(tmpexpense >= 0.4 && tmpexpense < 0.5){ printf("Not Costly"); }else{ printf("Good"); } fflush(stdout); //attempt to write the average expense to the 1st file if (fprintf(expfil1, "%.2Lf\n", avgexpense) == -1) { printf("Error writing to the file %s!\n", filname1); fflush(stdout); }; //attempt to close the file if (fclose(expfil1) == EOF){ printf("Error closing file the file %s!\n", filname1); fflush(stdout); return -1; } return 0; }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...