Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > comparison between signed and unsigned integer expressions

Reply
Thread Tools

comparison between signed and unsigned integer expressions

 
 
Gary Wessle
Guest
Posts: n/a
 
      07-29-2006

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:air;
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[i]);
}

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

thanks
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-29-2006
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.
 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      07-29-2006
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
 
Reply With Quote
 
Gary Wessle
Guest
Posts: n/a
 
      07-29-2006
Frederick Gotham <(E-Mail Removed)> 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) )
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-29-2006
Gary Wessle wrote:
> Frederick Gotham <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why, warning: comparison between signed and unsigned integer expressions? Lox C Programming 13 05-31-2012 06:11 PM
comparison between signed and unsigned int compcreator@gmail.com C Programming 6 09-21-2007 07:37 PM
comparison between signed int and unsigned int Alex C Programming 3 04-26-2006 05:20 AM
conversion of signed integer to unsigned integer junky_fellow@yahoo.co.in C Programming 14 06-18-2005 02:29 PM
comparison between signed and unsigned John Buckley C Programming 2 10-24-2003 11:53 AM



Advertisments