Hi,
A specific set of range
<10,20>
<20,30>
<30,40>
<40,50>
i have series of numbers in a buffer;
say.. 11 14 19 23 28 45 49 is present in char* noInBuff;
Every time if the number falls in specific range for the first time then i have to replace it with a string, and if the next too falls in the same range then i do nothing.. same things continues. and simultaneously i have to concatenate in an a char array.

so at the end i will receiving the output as,
output = [eleven 14 19 twentythree 28 fortyfive 49]

char* temp = strtok(noInBuff, " ")
while(temp != null)
{
if((noInBuf >= 10) && (noInBuff <20) && (count1==0))
{
    replace that no. with string;
   strcat(strname, "string1");
   count1++;
}
else if ((noInBuf >= 20) && (noInBuff <30) && (count2==0))
{
    replace that no. with string, 
   concatenate in another char array;
    count2++;
}
else if ((noInBuf >= 30) && (noInBuff <40) && (count3==0))
{
    replace that no. with string,
   concatenate in another char array;
   count3++;
}
else if ((noInBuf >= 40) && (noInBuff <50) && (count4==0))
{
    replace that no. with string,
   concatenate in another char array;
    count4++;
}
}

count1=count2=count3=count4=0;

so for that i had the above code which will work for the above scenario, but what if in the future i have numbers going beyond 60, 70 then i have to again write the code for the same. what exactly i wanna know is that Is there any other way to make it generic, just by inserting range in the text file, so i do not have to touch the code.

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

I was thinking about using the modulus operator to get every third number, then just increment by ten to get the range...

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{

  vector<int>crap;

  crap.push_back(11);
  crap.push_back(14);
  crap.push_back(19);
  crap.push_back(23);
  crap.push_back(28);
  crap.push_back(45);
  crap.push_back(49);

  //string letter = "11 14 19 23 28 45 49";

  int s = crap.size();


  int range = 10;
  int i;
  for ( i = 0; i < s; i++ )
  {
    if ( i % 3 == 0 )
   { 
     range = range + 10;
     cout << crap[i] << endl;
     cout << "the range is " <<range -10 << " to " << range << endl;
   }
  }
  return 0;
}

my output

user@user-desktop:~$ g++ -Wall test.cpp
user@user-desktop:~$ ./a.out
11
the range is 10 to 20
23
the range is 20 to 30
49
the range is 30 to 40

Hi,
A specific set of range
<10,20>
<20,30>
<30,40>
<40,50>
i have series of numbers in a buffer;
say.. 11 14 19 23 28 45 49 is present in char* noInBuff;
Every time if the number falls in specific range for the first time then i have to replace it with a string, and if the next too falls in the same range then i do nothing.. same things continues. and simultaneously i have to concatenate in an a char array.

so at the end i will receiving the output as,
output = [eleven 14 19 twentythree 28 fortyfive 49]

char* temp = strtok(noInBuff, " ")
while(temp != null)
{
if((noInBuf >= 10) && (noInBuff <20) && (count1==0))
{
    replace that no. with string;
   strcat(strname, "string1");
   count1++;
}
else if ((noInBuf >= 20) && (noInBuff <30) && (count2==0))
{
    replace that no. with string, 
   concatenate in another char array;
    count2++;
}
else if ((noInBuf >= 30) && (noInBuff <40) && (count3==0))
{
    replace that no. with string,
   concatenate in another char array;
   count3++;
}
else if ((noInBuf >= 40) && (noInBuff <50) && (count4==0))
{
    replace that no. with string,
   concatenate in another char array;
    count4++;
}
}

count1=count2=count3=count4=0;

so for that i had the above code which will work for the above scenario, but what if in the future i have numbers going beyond 60, 70 then i have to again write the code for the same. what exactly i wanna know is that Is there any other way to make it generic, just by inserting range in the text file, so i do not have to touch the code.

Arrays or vectors will do you well here. Don't hard-code the numbers into the code. Presumably you have something like this somewhere:

string textNumbers = 
{
    "zero", "one", "two", "three", "four", ...
};

and possibly a map or something that can take you from "seventeen" to 17 or whatever. You need to somehow be able to go from numbers to strings and vice versa, it seems to me. I don't know how you do it now. You may have one already.

You can set up arrays (or some other container) to hold the low and high ranges and count, or have an array of structs:

struct range
{
    int lowRange;
    int highRange;
    int count;
};

range ranges[10];  // or however many ranges you have.

// fill in the ranges
for (int i = 0; i < 10; i++)
{
    range[i].lowRange = i * 10;
    range[i].highRange =  i * 10 + 9;
    count[i] = 0;
}

Now you have your ranges set up. Go back to your original code.

char* temp = strtok(noInBuff, " ")
while(temp != null)
{
     int rangeNum;
     // figure out which range number is in, put in rangeNum

     if(count[rangeNum] == 0)
     {
         // do string replacement
         count[rangeNum]++;
     }
}

// reset count[] to 0
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.