Connecting Tech Pros Worldwide Help | Site Map

Help in STRING compare ?

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 7th, 2005, 06:45 PM
tvn007@hotmail.com
Guest
 
Posts: n/a
Default Help in STRING compare ?

Please help, I am not sure why this code not even compile due to
line 39 and 44.
Thanks in advance for any help


1 #include <cctype>
2 #include <iostream>
3 #include <string>
4 #include <vector>
5
6 using std::cin;
7 using std::cout;
8 using std::endl;
9 using std::getline;
10 using std::string;
11 using std::vector;
12
13 double split (const std::string&,int);
14
15 int main()
16 {
17 string s =("1.1,2.3,3.5,4.2");
18
19 const int loc = 3;
20 double results;
21
22 results= split(s,loc);
23 cout <<"RESULT IS"<<" "<< results<<endl;
24
25
26 return 0;
27 }
28
29
30 double split(const string& fw_answer,const int ok )
31 {
32 double data;
33 vector<string> ret;
34 const string separator (",");
35 typedef string::size_type string_size;
36 string_size i = 0;
37
38 while (i != fw_answer.size()) {
39 while (i != fw_answer.size() && (fw_answer[i]==separator))
40 ++i;
41
42 string_size j = i;
43
44 while (j != fw_answer.size() && !(fw_answer[j]==separator))
45 ++j;
46
47 if (i != j) {
48 ret.push_back(fw_answer.substr(i, j - i));
49 i = j;
50 }
51
52 }
53 for (vector<string>::size_type i = 0; i != ret.size(); ++i)
54 if (i==ok){
55 data = atof(ret[i].c_str());
56 cout <<"HERE it is" <<" "<< ret[i] << endl;
57 }
58 return data;
59 }
60


  #2  
Old November 7th, 2005, 07:05 PM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Re: Help in STRING compare ?

tvn007@hotmail.com wrote:
[color=blue]
> Please help, I am not sure why this code not even compile due to
> line 39 and 44.
> Thanks in advance for any help
>
>
> 1 #include <cctype>
> 2 #include <iostream>
> 3 #include <string>
> 4 #include <vector>
> 5
> 6 using std::cin;
> 7 using std::cout;
> 8 using std::endl;
> 9 using std::getline;
> 10 using std::string;
> 11 using std::vector;
> 12
> 13 double split (const std::string&,int);
> 14
> 15 int main()
> 16 {
> 17 string s =("1.1,2.3,3.5,4.2");
> 18
> 19 const int loc = 3;
> 20 double results;
> 21
> 22 results= split(s,loc);
> 23 cout <<"RESULT IS"<<" "<< results<<endl;
> 24
> 25
> 26 return 0;
> 27 }
> 28
> 29
> 30 double split(const string& fw_answer,const int ok )
> 31 {
> 32 double data;
> 33 vector<string> ret;
> 34 const string separator (",");[/color]

Try:
char const separator = ',';

[color=blue]
> 35 typedef string::size_type string_size;
> 36 string_size i = 0;
> 37
> 38 while (i != fw_answer.size()) {
> 39 while (i != fw_answer.size() && (fw_answer[i]==separator))
> 40 ++i;
> 41
> 42 string_size j = i;
> 43
> 44 while (j != fw_answer.size() && !(fw_answer[j]==separator))
> 45 ++j;
> 46
> 47 if (i != j) {
> 48 ret.push_back(fw_answer.substr(i, j - i));
> 49 i = j;
> 50 }
> 51
> 52 }
> 53 for (vector<string>::size_type i = 0; i != ret.size(); ++i)
> 54 if (i==ok){
> 55 data = atof(ret[i].c_str());
> 56 cout <<"HERE it is" <<" "<< ret[i] << endl;
> 57 }
> 58 return data;
> 59 }
> 60[/color]


Best

Kai-Uwe Bux
  #3  
Old November 7th, 2005, 07:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Help in STRING compare ?

On Mon, 07 Nov 2005 11:37:51 -0800, tvn007 wrote:[color=blue]
> Please help, I am not sure why this code not even compile due to
> line 39 and 44.
> Thanks in advance for any help
>
>
> 1 #include <cctype>[/color]

Consider, for all your posts after this one, dropping the line numbers and
simply commenting the lines you're talking about, like this;

if (blahbla) { // line # 42
[color=blue]
> 2 #include <iostream>
> 3 #include <string>
> 4 #include <vector>
> 5
> 6 using std::cin;
> 7 using std::cout;
> 8 using std::endl;
> 9 using std::getline;
> 10 using std::string;
> 11 using std::vector;
> 12
> 13 double split (const std::string&,int);
> 14
> 15 int main()
> 16 {
> 17 string s =("1.1,2.3,3.5,4.2");
> 18
> 19 const int loc = 3;
> 20 double results;
> 21
> 22 results= split(s,loc);
> 23 cout <<"RESULT IS"<<" "<< results<<endl;
> 24
> 25
> 26 return 0;
> 27 }
> 28
> 29
> 30 double split(const string& fw_answer,const int ok )
> 31 {
> 32 double data;
> 33 vector<string> ret;
> 34 const string separator (",");[/color]

Why not simply
const char separator(',');

???
[color=blue]
> 35 typedef string::size_type string_size;
> 36 string_size i = 0;
> 37
> 38 while (i != fw_answer.size()) {
> 39 while (i != fw_answer.size() && (fw_answer[i]==separator))[/color]

You're trying to compare a char (fw_answer[i]) to a string (separator).
As soon as you make 'separator' a plain 'char', your problem will be
solved.
[color=blue]
> 40 ++i;
> 41
> 42 string_size j = i;
> 43
> 44 while (j != fw_answer.size() && !(fw_answer[j]==separator))[/color]

Same problem
[color=blue]
> 45 ++j;
> 46
> 47 if (i != j) {
> 48 ret.push_back(fw_answer.substr(i, j - i));
> 49 i = j;
> 50 }
> 51
> 52 }
> 53 for (vector<string>::size_type i = 0; i != ret.size(); ++i)
> 54 if (i==ok){
> 55 data = atof(ret[i].c_str());
> 56 cout <<"HERE it is" <<" "<< ret[i] << endl;
> 57 }
> 58 return data;
> 59 }
> 60[/color]

V
  #4  
Old November 7th, 2005, 07:35 PM
tvn007@hotmail.com
Guest
 
Posts: n/a
Default Re: Help in STRING compare ?

Thanks all for the help !

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.