Could anyone help me with an issue that I have? I have a program that reads from a data file using an array and the data is structured as a long interger. I am trying to break up a long interger in to pairs i.e 141251 into 14:12:15 using c++.

This is a math problem. You want to separate a number into hundreds.

141251 / 100 = 1412 R 51

and

1412 / 100 = 14 R 12

and

14 / 100 = 0 R 14

The C++ remainder operator is %.

Hope this helps.

>This is a math problem.
No, it's not. Reading the data as an integer strikes me as dangerous in this case. I can easily see integer overflow being a potential problem, and that serious affects the robustness of the code. Something like this would be better:

#include <fstream>
#include <iomanip>
#include <iostream>

int convert ( const char *s )
{
  int result = 0;

  // Assuming unsigned single or double digit values
  while ( *s != 0 )
    result = 10 * result + ( *s++ - '0' );

  return result;
}

int main()
{
  std::ifstream in ( "mydata" );

  if ( in ) {
    char buffer[3];

    while ( in>> std::setw ( sizeof buffer ) >> buffer )
      std::cout<< convert ( buffer ) <<':';

    std::cout<<'\n';
  }
}

That way the input method forces integer pairs and avoids the potential overflow. It also greatly simplifies the math (or eliminates it entirely if you choose not to do a manual conversion).

so it is my understanding that you think that using a modulus in this case would cause a problem?

>you think that using a modulus in this case would cause a problem?
No, I think reading integers would cause a problem.

I'm going to quote your PM to me as it has no business being sent to me privately and should have been in this thread.

Thanks for the help with my issue. I have another question for you now that I have use your method of calling my numbered pair the begining of what I wrote is craping out. My entire data file is structured as such
707 141251 141659
321 090537 090748
319 101502 102929
222 105702 110000
305 114202 115111

what I am getting now is 70:7:14:12:51 and so forth. Is there a way to clean this up?

The bad news is that even if you were reading integers, it still wouldn't work due to the whitespace. What you probably want to do is compact the whitespace after reading a whole line, the use a stringstream to complete the solution:

#include <cctype>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>

int convert ( const char *s )
{
  int result = 0;

  // Assuming unsigned single or double digit values
  while ( *s != 0 )
    result = 10 * result + ( *s++ - '0' );

  return result;
}

void compact ( char *s )
{
  char *p = s;

  while ( *s != '\0' ) {
    if ( !isspace ( *s ) )
      *p++ = *s;

    ++s;
  }

  *p = '\0';
}

int main()
{
  std::ifstream in ( "test.txt" );

  if ( in ) {
    char line[1024];

    if ( in.getline ( line, sizeof line ) ) {
      compact ( line );

      std::stringstream split ( line );
      char buffer[3];

      while ( split>> std::setw ( sizeof buffer ) >> buffer )
        std::cout<< convert ( buffer ) <<':';

      std::cout<<'\n';
    }
  }
}

I see where I made my error in my code now. I kept trying to use the in.get() instead of in.getline statement. Thank you for your help.

This question has already been answered. Start a new discussion instead.