comparison between signed and unsigned integer expressions

Discussion in 'C++' started by Gary Wessle, Jul 29, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    I am writing a code to open a space delimited data file, return the
    number of rows and columns, as well as return the nth column where n
    is user define number.
    I put all the cells in a loooong vector and loop in an incremental way
    to select the column of choice, here is the code which works but gives
    a warning

    ****************************************************************
    g++ -Wall -g -c -o useful.o useful.cpp
    useful.cpp: In member function 'void file_info::set_col(int)':
    useful.cpp:54: warning: comparison between signed and unsigned integer expressions
    ****************************************************************

    **************** useful.h ****************
    #ifndef USEFUL_H
    #define USEFUL_H
    #include <vector>
    using std::vector;

    class file_info
    {
    int nRows, nCol;
    string file_name;
    vector<string> all_cols;
    vector<string> col;
    void stock_take();
    void set_col(int);

    public:

    file_info(string const& fileName);
    ~file_info();

    /** returns a pair of values, the first is the
    * number of rows in a space delimited data file
    * and the second is the number of columns.
    **/
    pair<int,int> get_counts();

    /** takes a number n and returns a vector
    * containing the nth column of the data file
    **/
    vector<string> get_col(int);

    };


    #endif

    **************** useful.cpp ****************
    #include <utility>
    using std::pair;
    using std::make_pair;

    #include <string>
    using std::string;

    #include <fstream>
    using std::ifstream;

    #include <sstream>
    using std::stringstream;

    #include "useful.h"


    file_info::file_info( string const& fileName )
    : file_name( fileName ){
    nCol = 0;
    nRows = 1;
    stock_take();
    }
    void file_info::stock_take() {
    ifstream in(file_name.c_str());
    string line;
    getline(in, line);
    stringstream input( line.c_str() );

    string word;
    while(input >> word) {
    nCol++; // init'd by constructor
    all_cols.push_back(word);
    }

    while (getline(in, line)){
    nRows++; // init'd by constructor
    stringstream input( line.c_str() );
    string word;
    while(input >> word)
    all_cols.push_back(word);
    }

    }
    file_info::~file_info() {}

    pair<int,int> file_info::get_counts()
    {
    return make_pair(nRows, nCol);
    }

    void file_info::set_col(int x){
    for(int i = (x-1); i < all_cols.size(); i = (i+nCol) ) //<<LINE 54
    col.push_back(all_cols);
    }

    vector<string> file_info::get_col(int y){
    set_col(y);
    return col;
    }

    thanks
     
    Gary Wessle, Jul 29, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Ian Collins Guest

    Gary Wessle wrote:
    > Hi
    >
    > I am writing a code to open a space delimited data file, return the
    > number of rows and columns, as well as return the nth column where n
    > is user define number.
    > I put all the cells in a loooong vector and loop in an incremental way
    > to select the column of choice, here is the code which works but gives
    > a warning
    >
    > ****************************************************************
    > g++ -Wall -g -c -o useful.o useful.cpp
    > useful.cpp: In member function 'void file_info::set_col(int)':
    > useful.cpp:54: warning: comparison between signed and unsigned integer expressions
    > ****************************************************************
    >

    I think you have forgotten to ask a question!

    Change you loop counter to an unsigned to remove the warning.

    --
    Ian Collins.
     
    Ian Collins, Jul 29, 2006
    #2
    1. Advertising

  3. Gary Wessle posted:

    > for(int i = (x-1); i < all_cols.size(); i = (i+nCol) ) //<<LINE 54



    You know the answer already. Either make them both signed, or make them both
    unsigned.

    (unsigned)i < all_cols.size()

    i < (int)all_cols.size()


    I myself only use signed integer types when I may need to store a negative
    number.

    --

    Frederick Gotham
     
    Frederick Gotham, Jul 29, 2006
    #3
  4. Gary Wessle

    Gary Wessle Guest

    Frederick Gotham <> writes:

    > Gary Wessle posted:
    >
    > > for(int i = (x-1); i < all_cols.size(); i = (i+nCol) ) //<<LINE 54

    >
    >
    > You know the answer already. Either make them both signed, or make them both
    > unsigned.
    >
    > (unsigned)i < all_cols.size()
    >
    > i < (int)all_cols.size()
    >
    >
    > I myself only use signed integer types when I may need to store a negative
    > number.
    >
    > --
    >
    > Frederick Gotham


    yes, I tried to cast things around and after I could not do it I
    posted. I now remember the syntax, this line below fixed it.

    for(int i = (x-1); static_cast<unsigned> (i) < all_cols.size(); i = (i+nCol) )
     
    Gary Wessle, Jul 29, 2006
    #4
  5. Gary Wessle

    Ian Collins Guest

    Gary Wessle wrote:
    > Frederick Gotham <> writes:
    >
    >
    >>Gary Wessle posted:
    >>
    >>
    >>> for(int i = (x-1); i < all_cols.size(); i = (i+nCol) ) //<<LINE 54

    >>
    >>
    >>You know the answer already. Either make them both signed, or make them both
    >>unsigned.
    >>
    >> (unsigned)i < all_cols.size()
    >>
    >> i < (int)all_cols.size()
    >>
    >>
    >>I myself only use signed integer types when I may need to store a negative
    >>number.
    >>
    >>--
    >>
    >>Frederick Gotham

    >
    >
    > yes, I tried to cast things around and after I could not do it I
    > posted. I now remember the syntax, this line below fixed it.
    >
    > for(int i = (x-1); static_cast<unsigned> (i) < all_cols.size(); i = (i+nCol) )


    Why?

    Just make everything unsigned, unless a negative index makes sense. The
    above looks plain daft.

    --
    Ian Collins.
     
    Ian Collins, Jul 29, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Buckley

    comparison between signed and unsigned

    John Buckley, Oct 23, 2003, in forum: C Programming
    Replies:
    2
    Views:
    493
    Dan Pop
    Oct 24, 2003
  2. Alex
    Replies:
    3
    Views:
    675
    Michael Mair
    Apr 26, 2006
  3. Replies:
    6
    Views:
    404
    Army1987
    Sep 21, 2007
  4. Replies:
    4
    Views:
    411
  5. Lox
    Replies:
    13
    Views:
    2,442
    Joe Pfeiffer
    May 31, 2012
Loading...

Share This Page