I have encountered a couple of problems:

a. I need to convert a string of numbers seperated by commas into integers. I have had help before today and know how to convert one string (thread was posted today). But I dont know how to do a simpler version of it given the fact that i have pages and pages of strings to convert. (I will be reading the file from wordpad).

b. How would i do a search within a given string (e.g 48098, 50933, 3129, 5) for a value that is say greater than or equal to y, where y is some given constant? Would a "for loop" make sense?

Thanks!

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

I have encountered a couple of problems:

a. I need to convert a string of numbers seperated by commas into integers. I have had help before today and know how to convert one string (thread was posted today). But I dont know how to do a simpler version of it given the fact that i have pages and pages of strings to convert. (I will be reading the file from wordpad).

b. How would i do a search within a given string (e.g 48098, 50933, 3129, 5) for a value that is say greater than or equal to y, where y is some given constant? Would a "for loop" make sense?

Thanks!

Well what have u got kiddo? Do u no how to use fstreams?

And for your information it do not matter if you have pages and pages of strings to convert.

If you have a method which works for just one case, u should B able 2 use it 2 handles pages and pages and pages... [tex]ad[/tex] [tex]infinitum[/tex]

K, this is an example code for converting a string to integer, for right now...just for converting. Ill add the fstream bit in later.

#include <sstream>
#include <string>  
#include <iostream>

using namespace std;

bool StringToInt(const string &s, int &i);

int main(void)
{
  string s1 = "12";
  string s2 = "ZZZ";
  int result;
  
  if (StringToInt(s1, result))
  {
    cout << "The string value is " << s1
         << " and the int value is " << result << endl;
  }
  else
  {
    cout << "Number conversion failed" <<endl;
  }
  if (StringToInt(s2, result))
  {
    cout << "The string value is " << s2
         << " and the int value is " << result << endl;
  }
  else
  {
    cout << "Number conversion failed on " <<s2 <<endl;
  }    
  return(0);
}

bool StringToInt(const string &s, int &i)
{
  istringstream myStream(s);
  
  if (myStream>>i)
    return true;
  else
    return false;
}

So, lets say the string im dealing with is set up like this.....
48909, 3453, 7739, 35
34534, 5455, 7344, 40
23423, 5345, 7089, 45
67867, 7853, 7634, 50

Ill use the above code to convert them all into intergers. But now, i want to find an integer that is between 7050 and 7100. (which is in BOLD); kind of like a string search. So, how do i do that???

Thanks in advance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.