Thread: Checking Blank Lines ??

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    5

    Checking Blank Lines ??

    I am having a problem on Checking Empty Lines in my program

    my input.txt:
    Code:
    4
    
    4
    
    0 0 1 2
    1 0 0 0
    1 3 5 4
    0 6 3 2
    
    
    0 0 1 2
    1 7 5 0
    2 3 5 6
    0 6 5 2
    
    1:0 4 2 0

    what i want to do is to put a comma where ever there is an empty line , so I can distinguish between these numbers and store them inside an intger array to do mathematical operations between them.

    my code looks like this for now:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    #include <sstream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    ifstream File("input.txt");
    string line;
    string num;
    string array[50];
    string comma;
    int i=0;
    
        while (getline(File,line)) {
            
            istringstream s(line);
            
            if (line.empty()){
            comma =",";
            }
            while ( s >> num ) {
            ar[i]=num+comma;
            i++;
            }
        }
        
    return 0;
    }
    Can this be done ? or is there any other method to distinguish between these numbers and store them inside an intger array ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    Why do you need to insert the commas? Why not directly parse them and place them in the respective integer arrays?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Why do you need to insert the commas? Why not directly parse them and place them in the respective integer arrays?
    because I want to distinguish between these numbers ...
    5 <= #number of items
    4 <= #types of items

    0 0 1 2 <= matrix #1
    1 0 0 0
    1 3 5 4
    0 6 3 2
    0 0 1 4

    0 0 1 2 <= matrix #2
    1 7 5 0
    2 3 5 6
    0 6 5 2
    0 6 5 6

    1 5 2 0 <= Available items

    and I want to do Subtraction between matrix #1 and matrix #2 ..

    hope my problem is clear ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    No, it is not clear why you cannot just parse. For example, once you encounter the first number, then a blank line, it seems to me that you can act on it by creating a square matrix of that dimension. Then you encounter the next number, then a blank line, you can create another square matrix of that dimension. When you ecnounter yet another number, and realise that there's no blank line and in fact there are multiple numbers on the same line, it is time to go back to the first square matrix (which is presumably stored in a container) and fill it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    5
    Quote Originally Posted by laserlight View Post
    No, it is not clear why you cannot just parse. For example, once you encounter the first number, then a blank line, it seems to me that you can act on it by creating a square matrix of that dimension. Then you encounter the next number, then a blank line, you can create another square matrix of that dimension. When you ecnounter yet another number, and realise that there's no blank line and in fact there are multiple numbers on the same line, it is time to go back to the first square matrix (which is presumably stored in a container) and fill it.
    because the numbers and matrix size inside input file can be changed,for example :

    6 <= #number of items instead of 5, so this means means +1 row
    5 <= #types of items instead of 4, so this means means +1 column

    0 0 1 2 7 <= matrix #1
    1 0 0 0 4
    1 3 5 4 2
    0 6 3 2 1
    0 0 1 4 5
    0 0 1 4 6

    4 3 1 7 5 <= matrix #2
    1 4 0 0 1
    2 1 5 4 2
    0 6 5 2 5
    4 3 7 4 0
    0 2 2 4 1

    1 5 2 0 <= Available items

    Iam really beginner at this, if this can be done,can u show me some example of a code ?
    thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    Quote Originally Posted by rakk92
    because the numbers and matrix size inside input file can be changed
    That's not a big deal precisely because the matrix dimensions are provided before the entries. If they weren't, then you would have to either grossly overallocate memory and hope that you have enough for each matrix, or expand the matrices as you read the entries. This way, you can create matrices with the given dimensions at once, then read the entries (and report an input error if there are too few or too many entries for the given matrix).

    Quote Originally Posted by rakk92
    Iam really beginner at this, if this can be done,can u show me some example of a code ?
    I recommend that you tackle the matrix subtraction first. This way, you will think about and decide upon the matrix representation first. For example, will you be creating a matrix class? If so, what will the member variables be, keeping in mind that the matrices can have different dimensions at run time?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 11:51 AM
  2. Discarding blank lines?
    By Blurr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 12:30 PM
  3. Question About Blank Lines in Text Files
    By Zildjian in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 04:31 PM
  4. Blank Lines in a menu
    By ColdFire in forum Windows Programming
    Replies: 2
    Last Post: 07-05-2002, 03:51 PM