i am new to c++, currently working on an assignment, now i'm stuck in skipping the comment lines in the txt file.
the txt file is something like this:

#com1
A1
A2
A3

#com2
B1
B2
B3
...

what i need to do is to enter commands to display the text file contents. like when i enter cmd1, it shows A1-A3, enter cmd2 it shows B1-B3. but i don't know how to find the strings and skip those lines. the code i've got is

cout << "Please enter command: ";
cin >> command;

if(command == "cmd1") {
    inFile.open("data.txt");
    inFile >> product; //product repesents A1-A3..

while (inFile.good())
{
      ??????????????
      ??????????????

      cout << product << endl;
      inFile >> product;
}
}

the question marks is where i'm stuck, i tried cin.ignore() but it doesn't seem to work, i want to code something that can search the txt, find the exact string "#com1", and skip this line, help please... thanks in advance!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Check the first non-whitespace character. If it starts a comment, continue the loop:

inFile.open("data.txt");

while (inFile >> product) {
  if (product[0] == '#')
    continue;

  cout << product << '\n';
}

Ed's code assumes that there won't be any leading whitespace. The code also shows you how to cleanly control a loop based on whether input was successful or not. You can use the input itself as the loop condition, and that saves the before and after code duplication that you had before. :)

Ed's code assumes that there won't be any leading whitespace.

Ed's code also assumes the words on each line won't contain whitespaces :)

If Niek would program this, it would look more like

inFile.open("data.txt");

if (inFile.is_open() )
{
    while (getline(inFile,product)) 
    {
      if (product[0] != '#')
           cout << product << '\n';
     }
}

It also checks if the file exists. As Edward said in another post he/she made today: Error Checking is good!
;)

Thank you guys it works, sort of.
When I command "cmd1" it displays all the data fom A1-B3, but I only want to show A1-A3 when I enter "cmd1", and B1-B3 when i enter "cmd2".
Also can i claim something else instead of "product[0]" ? say "line[0]", because I have another question of paired arrays like

#com1
A1 B1
A2 B2
A3 B3

#com2
C1 D1
C2 D2
C3 D3

I tried this to show #com1 contents

while (inFile >> product >> price) {
          if (line1[0] == '#')
          continue;

          cout << product << " " << price << endl;
    }

but it won't work. I've been searching this for few days, nothing worked :(

> Ed's code also assumes the words on each line won't contain whitespaces
That assumption was inherited from the original code. It's possible that changing it could silently break the program, so Ed kept the original assumption to avoid a potential bug.

> If Niek would program this, it would look more like
If Edward were doing this for Edward's code, it would be drastically different, but also unsuitable for helping a new C++ programmer. ;) Sometimes personal practices are best ignored to make examples that are easier to digest.

If Edward were doing this for Edward's code, it would be drastically different, but also unsuitable for helping a new C++ programmer.

Yeah... I've seen your codingskills and I agree that it might be a bit to much for the OP to comprehend ;)

This article has been dead for over six months. Start a new discussion instead.