Hello everybody!

I'm participating in a competition, and I just finished my program. It doesn't really matter what it does, and how. I have encountered a bug I think. I'd appreciate every kind of help, because with the problem, my solution fails for the test file.

I have to read N pieces of data, formatted "STRING_space_STRING_space_INT"

Obviously I used cin >> from >> to >> cost;

When I am finished with reading, I'm waiting for another type of input in another while loop - no COUT used there neither.

The problem : after the last reading from the first sequence, somehow the program writes an endline and then waits for the next set of input...but the test file (cannot be modified) considers it as a bad output...

Has anyone noticed something like that before? There is no space after any of the N lines from the first input sequence. It doesn't write endline before, only at the last input.

Please help me if you can, it's really important and urgent!

(example input for n=3)
NEWYORK PARIS 3
LOSANGELES DUBLIN 5
LONDON PARIS 2

_(cursor jumps here but it should be a line upper)

p.s.: I have tried cin.clear() without luck.

What are you supposed to do with this input? That is the biggest question. Just read it and then shut down the program or output it to the screen?

Hello everybody!

I'm participating in a competition, and I just finished my program. It doesn't really matter what it does, and how. I have encountered a bug I think. I'd appreciate every kind of help, because with the problem, my solution fails for the test file.

I have to read N pieces of data, formatted "STRING_space_STRING_space_INT"

Obviously I used cin >> from >> to >> cost;

When I am finished with reading, I'm waiting for another type of input in another while loop - no COUT used there neither.

The problem : after the last reading from the first sequence, somehow the program writes an endline and then waits for the next set of input...but the test file (cannot be modified) considers it as a bad output...

Has anyone noticed something like that before? There is no space after any of the N lines from the first input sequence. It doesn't write endline before, only at the last input.

Please help me if you can, it's really important and urgent!

(example input for n=3)
NEWYORK PARIS 3
LOSANGELES DUBLIN 5
LONDON PARIS 2

_(cursor jumps here but it should be a line upper)

cin is a memory buffer. When you type something while cin is waiting for input you're adding to that buffer. When you >> you extract from the buffer (thus removing those characters). Unfortunately, when you type, you have to press enter at the end. And the >> does not extract the endline character at the end. So it's still in the buffer.

This isn't a problem if you're exclusively using cin, because cin will ignore any initial invalid characters such as spaces and newlines. So even though the buffer will have your previous newline character before your typed input, when you >>, it will skip it and extract only your typed input. However if you use getline directly after a cin, then you have a problem since getline stops getting when it reaches a newline character, which means it would get nothing.

p.s.: I have tried cin.clear() without luck.

istream::clear() is poorly named, in my opinion. What it actually does is clear the error state flags for the stream, but it's terribly easy to assume that it clears the stream buffer. The member function best suited to clearing the stream buffer is called ignore():

// Read and discard up to streamsize characters or until a newline is detected
cin.ignore(numeric_limits<streamsize>::max(), '\n');

Read and then process data, no output on the screen. I don't know what could be the problem. There is no even output for the questions or anything, only waits for input.

Please post a small program that exhibits the problem. Clearly you're doing something wrong, but without seeing the code, it's very difficult to determine what.

// Eytan Suchard, ANT/ANB Applied Neural Technology/Biometrics, Algorithms.
// Please note: This sample reads character by character until '\n'.
// cin has other reading options and also more sophisticated get options.
// This implementation is very simple and most flexible to tailored demands.

#include <iostream>

using namespace std;

class MAIN_STRING_CLASS
{
  private:
  char *member_str;

  public:

  ~MAIN_STRING_CLASS();
  MAIN_STRING_CLASS(void);
  bool function_read_from_cin(void);
  operator char *();
};

MAIN_STRING_CLASS::MAIN_STRING_CLASS(void)
{
  member_str = new char[1024];
}

MAIN_STRING_CLASS::~MAIN_STRING_CLASS()
{
  if (member_str) delete[] member_str; 
}

bool MAIN_STRING_CLASS::function_read_from_cin(void)
{
  char rfc_c;
  int rfc_count;

  if (!member_str) return false;

  for(cin.clear(),cin.get(rfc_c),rfc_count = 0;
      rfc_c!='\n' && rfc_count < (1024-1);
      cin.get(rfc_c),rfc_count++)
  {
    member_str[rfc_count] = rfc_c;
  }

  member_str[rfc_count] = '\0';

  return true;
}

MAIN_STRING_CLASS::operator char *()
{
  return member_str;
}

void main(void)
{
  MAIN_STRING_CLASS ma_str_class;

  if (ma_str_class.function_read_from_cin())
  {
    cout << "The string is: " << (char *)ma_str_class << "\n";
  }

  cout << "Press Enter to exit.";

  ma_str_class.function_read_from_cin();
}

Please edit your post by putting [code] tags around the code sample. The forum software does not retain indentation by default, so with the code tags, it is very difficult to read the program.

EDIT: I should have been paying closer attention when I replied. The real advice should have been not to hijack old threads.

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.