// Issues and resolution
Main() should not be in the same source file as finpro.cpp, it should be in another file (like main.cpp)
///////// finpro.cpp /////////
error :
while((token = str.next()) != " ")
It can be:
for( int i = 0; (token = str[i]) != ' '; ++i )
It is suggested that you should not be assigning variables within a condition expression.
//----
error :
if(0==strcmp("Dow",c_word))
fix:
if(0==strcmp("Dow",c_word.c_str())
error :
else if((0==strcmp("Nasdaq",c_word))||(0==strcmp("nasdaq",c_word)))
fix:
Same as previous error. strcmp only takes char * as a second parameter not string types.
///////// main.cpp /////////
error : ifstream inFile;
fix: std::ifstream inFile;
Here is your code fixed up:
Nothing wrong with finpro.h so it isn't included below.
main.cpp
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
|
#include "finpro.h"
int main()
{
std::ifstream inFile;
inFile.open("A1_finance.txt");
FinanceApp temp;
if(!inFile)
{
cout << "The file does not exist. " << endl;
return 1;
}
else
{
string alpha;
getline(inFile, alpha);
temp.indexCheck(alpha);
}
inFile.close();
system("pause");
return 0;
}
| |
finpro.cpp
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
|
#include "finpro.h"
FinanceApp::FinanceApp()
{
index = " ";
num = 0.0;
diff = 0.0;
}
string FinanceApp::getWord(string str)
{
string token, store;
for( int i = 0; (token = str[i]) != ' '; ++i )
{
store = str[i];
}
return store;
}
void FinanceApp::indexCheck(string fin_value)
{
int num, ptr = 0; string c_word;
num = fin_value.length();
c_word = getWord(fin_value);
while(ptr<num)
{
if(0==strcmp("Dow",c_word.c_str()))
{
cout << "Testing: 1" << endl;
}
else if((0==strcmp("Nasdaq",c_word.c_str()))||(0==strcmp("nasdaq",c_word.c_str())))
{
cout << "Testing: 2" << endl;
}
else if(0==strcmp("S&P 500",c_word.c_str()))
{
cout << "Testing: 3" << endl;
}
else if(0==strcmp("FTSE 100",c_word.c_str()))
{
cout << "Testing:3" << endl;
}
else if(0==strcmp("DAX",c_word.c_str()))
{
cout << "Testing:4" << endl;
}
else if((0==strcmp("Nikkei 225", c_word.c_str()))||(0==strcmp("NIKKEI 225",c_word.c_str())))
{
cout << "Testing: 5" << endl;
}
else if((0==strcmp("Hang Seng", c_word.c_str()))||(0==strcmp("HANG SENG",c_word.c_str())))
{
cout << "Testing: 6" << endl;
}
else
{
cout << "Testing: 7 " << endl;
}
}
}
| |