comparison between signed and unsigned integer expressions

G

Gary Wessle

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
 
I

Ian Collins

Gary said:
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.
 
F

Frederick Gotham

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.
 
G

Gary Wessle

Frederick Gotham said:
Gary Wessle posted:



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.

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) )
 
I

Ian Collins

Gary said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top