Can't Find Error, PLS CHECK URGENT!!!
Jan 27, 2010 at 7:02am UTC
This is a program I created which reads data grom the Yahoo Finance page but despite the program being able to compile, when I execute, it doesn't read my HTML file, I dunno why.Pls suggest the error.Compiler shows none.
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
class IndexDetails{
public :
IndexDetails();
void MatchPattern();
void Parser();
void getValue();
private :
string line;
string value;
string diff;
};
IndexDetails::IndexDetails()
{
line = " " ;
value = " " ;
diff = " " ;
}
void IndexDetails::Parser()
{
int k = 0;
while (k < line.length())
{
if ((line[k] == '<' )||(line[k] == '>' )||(line[k] == '/' ))
{
line[k] = ' ' ;
}
k++;
}
}
void IndexDetails::MatchPattern()
{
int num; int ptr = 0;
num = line.length();
while (ptr<num)
{
if (0 == strcmp("Dow" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("Nasdaq" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("S&P 500" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("FTSE 100" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("DAX" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("Nikkei 225" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("Hang Seng" , line.c_str()))
{
getValue();
}
else if (0 == strcmp("Straits Times Index" , line.c_str()))
{
getValue();
}
else
{
cout << "None " << endl;
}
system("pause" );
}
}
void IndexDetails::getValue()
{
string check("id" ); size_t found; int y= 0; int digi;
while (y < line.length())
{
found = line.find(check);
if (found != string::npos)
{
digi = int (found);
if (isdigit(line[y]))
{
value = line[y] + value;
}
}
}
}
int main()
{
ifstream inFile;
inFile.open("finance.htm" );
IndexDetails temp;
if (!inFile)
{
cout << "The file does not exist. " << endl;
return 1;
}
else
{
string alpha;
getline(inFile,alpha);
temp.MatchPattern();
}
inFile.close();
system("pause" );
return 0;
}
Last edited on Jan 27, 2010 at 4:16pm UTC
Jan 27, 2010 at 7:14am UTC
num = line.length();
where in length of line is 0
1 2 3 4 5 6
IndexDetails::IndexDetails()
{
line = " " ;
value = " " ;
diff = " " ;
}
Jan 27, 2010 at 7:50am UTC
Actually, I think I realise a mistake I made. I wanted to read line by line but line is declared as a private variable in my class. Now, for getline when I pass the variable line, the compiler doesn't compile so I had to create another string variable alpha. Bcose of this, I realise now line contains nth so then its meaningless.
The code should actually be like this:
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
class IndexDetails{
public :
IndexDetails();
void MatchPattern();
void Parser();
void getValue();
private :
string line;
string value;
string diff;
};
IndexDetails::IndexDetails()
{
line = " " ;
value = " " ;
diff = " " ;
}
void IndexDetails::Parser()
{
int k = 0;
while (k < line.length())
{
if ((line[k] == '<' )||(line[k] == '>' )||(line[k] == '/' ))
{
line[k] = ' ' ;
}
k++;
}
}
void IndexDetails::MatchPattern()
{
int num; int ptr = 0;
num = line.length();
while (ptr<num)
{
if (0 == strcmp("Dow" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("Nasdaq" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("S&P 500" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("FTSE 100" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("DAX" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("Nikkei 225" ,line.c_str()))
{
getValue();
}
else if (0 == strcmp("Hang Seng" , line.c_str()))
{
getValue();
}
else if (0 == strcmp("Straits Times Index" , line.c_str()))
{
getValue();
}
else
{
cout << "None " << endl;
}
system("pause" );
}
}
void IndexDetails::getValue()
{
string check("id" ); size_t found; int y= 0; int digi;
while (y < line.length())
{
found = line.find(check);
if (found != string::npos)
{
digi = int (found);
if (isdigit(line[y]))
{
value = line[y] + value;
}
}
}
}
int main()
{
ifstream inFile;
inFile.open("finance.htm" );
IndexDetails temp;
if (!inFile)
{
cout << "The file does not exist. " << endl;
return 1;
}
else
{
getline(inFile,line);
temp.MatchPattern();
}
inFile.close();
system("pause" );
return 0;
}
But this doesn't compile.
Jan 27, 2010 at 9:04am UTC
line in main was not declared..
create a member function that set the value of line in your class then
pass each line to that function prior to call MatchPattern
Jan 27, 2010 at 9:38am UTC
line has been declared as a private string variable in my class. I created a function to set the value but nothing works. Could someone suggest some other way to change things?
Last edited on Jan 27, 2010 at 9:39am UTC
Jan 27, 2010 at 1:44pm UTC
1 2 3 4 5 6 7 8 9 10 11 12
void IndexDetails::Parser()
{
int k = 0;
while (k < line.length())
{
if ((line[k] == '<' )||(line[k] == '>' )||(line[k] == '/' ))
{
line[k] = ' ' ;
}
k++;
}
}
HTML works using elements and a node tree. As far as I can see, you're not preserving this. It's impossible to tell an element from text using the method you're trying.
Jan 27, 2010 at 3:08pm UTC
Now I m able to read my HTML file but correct results are not being produced, can someone help run my program for me?Its very important & urgent that my program work. I would be really grateful if someone helped me identify & provide the correct results for my program.It compiles but results r wrong.
Main Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include "indexDetails.h"
using namespace std;
int main(int argc, char *argv[])
{
IndexDetails temp;
temp.readFile();
system("PAUSE" );
return EXIT_SUCCESS;
}
Header File:
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
#ifndef INDEXDETAILS_H
#define INDEXDETAILS_H
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include "indexDetails.h"
using namespace std;
class IndexDetails{
public :
IndexDetails();
void readFile();
void MatchPattern();
void Parser();
void getValue();
private :
string line;
string value;
string diff;
};
#endif
cpp FILE:
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include "indexDetails.h"
using namespace std;
IndexDetails::IndexDetails()
{
value = " " ;
diff = " " ;
}
void IndexDetails::Parser()
{
int k = 0;
while (k < line.length())
{
if ((line[k] == '<' )||(line[k] == '>' )||(line[k] == '/' ))
{
line[k] = ' ' ;
}
k++;
}
MatchPattern();
}
void IndexDetails::MatchPattern()
{
int num; int ptr = 0;
num = line.length();
while (ptr<num)
{
if (0 == strcmp("Dow" ,line.c_str()))
{
cout << "Index Name: " << "Dow" << endl;
getValue();
}
else if (0 == strcmp("Nasdaq" ,line.c_str()))
{
cout << "Index Name: " << "Nasdaq" << endl;
getValue();
}
else if (0 == strcmp("S&P 500" ,line.c_str()))
{
cout << "Index Name: " << "S&P 500" << endl;
getValue();
}
else if (0 == strcmp("FTSE 100" ,line.c_str()))
{
cout << "Index Name: " << "FTSE 100" << endl;
getValue();
}
else if (0 == strcmp("DAX" ,line.c_str()))
{
cout << "Index Name: " << "DAX" << endl;
getValue();
}
else if (0 == strcmp("Nikkei 225" ,line.c_str()))
{
cout << "Index Name: " << "Nikkei 225" << endl;
getValue();
}
else if (0 == strcmp("Hang Seng" , line.c_str()))
{
cout << "Index Name: " << "Hang Seng" << endl;
getValue();
}
else if (0 == strcmp("Straits Times Index" , line.c_str()))
{
cout << "Index Name: " << "Straits Times Index" << endl;
getValue();
}
else
{
cout << "None " << endl;
}
system("pause" );
}
}
void IndexDetails::getValue()
{
string check("id" ); size_t found; int y= 0; int digi;
while (y < line.length())
{
found = line.find(check);
if (found != string::npos)
{
digi = int (found);
if (isdigit(line[y]))
{
value = line[y] + value;
}
}
}
cout << "The value is:" << value << endl;
}
void IndexDetails::readFile()
{
ifstream inFile ("finance.htm" );
if (inFile.is_open())
{
while (!inFile.eof() )
{
getline(inFile,line);
Parser();
}
inFile.close();
}
else
cout << "Unable to open file" ;
}
Jan 27, 2010 at 3:21pm UTC
finance.htm is a sample HTML Finance Page taken from Yahoo Finance, Singapore. I would be highly grateful to everyone who could test my program & get it working for me. I have been struggling with this for a long while now.
Topic archived. No new replies allowed.