468,496 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

comparison between signed and unsigned integer expressions


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<stringall_cols;
vector<stringcol;
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,intget_counts();

/** takes a number n and returns a vector
* containing the nth column of the data file
**/
vector<stringget_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,intfile_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[i]);
}

vector<stringfile_info::get_col(int y){
set_col(y);
return col;
}

thanks
Jul 29 '06 #1
4 15803
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.
Jul 29 '06 #2
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
Jul 29 '06 #3
Frederick Gotham <fg*******@SPAM.comwrites:
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) )
Jul 29 '06 #4
Gary Wessle wrote:
Frederick Gotham <fg*******@SPAM.comwrites:

>>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.
Jul 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Timothee Groleau | last post: by
11 posts views Thread by Frederick Gotham | last post: by
2 posts views Thread by Frederick Gotham | last post: by
14 posts views Thread by Mosfet | last post: by
6 posts views Thread by Kislay | last post: by
5 posts views Thread by evanevankan2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.