473,786 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<stringal l_cols;
vector<stringco l;
void stock_take();
void set_col(int);

public:

file_info(strin g 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<stringge t_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::stringstre am;

#include "useful.h"
file_info::file _info( string const& fileName )
: file_name( fileName ){
nCol = 0;
nRows = 1;
stock_take();
}
void file_info::stoc k_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_b ack(word);
}

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

}
file_info::~fil e_info() {}

pair<int,intfil e_info::get_cou nts()
{
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(a ll_cols[i]);
}

vector<stringfi le_info::get_co l(int y){
set_col(y);
return col;
}

thanks
Jul 29 '06 #1
4 16240
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.s ize()
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.s ize()
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<uns igned(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.s ize()
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<uns igned(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2247
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions. The reference should be 3.9.1 (Fundamental types), and 4.7 (Integral conversions). It seems to me that the Standard doesn't specify: 1) The "value representation" of any of these types, except that (3.9.1/3) "... The range of nonnegative...
3
11036
by: Alex | last post by:
I have a problem about the comparison between signed and unsigned integer. See the following code: int foo() { int i; unsigned int j; if (i < j) {
18
9457
by: Timothee Groleau | last post by:
Hi all, When should I be worried about doing a comparison of signed vs unsigned ints? Could someone give me a example where such a comparison would lead to unwanted results? Thanks, Tim.
11
2683
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int CountOccurrences(unsigned char const val, unsigned char const p,
2
5124
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get promoted to "signed int": signed char
14
24282
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in MultiByte String env (MBCS) and wchar_t if UNICODE #if defined(WIN32) || defined(UNDER_CE)
7
5049
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
6
6458
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but vaguely . Can
5
695
by: evanevankan2 | last post by:
I have a question about the warning 'comparison between signed and unsigned' I get from my code. It comes from the conditional in the outer for loop. I understand the warning, but I'm not sure what is the best way to prevent it. I can just change i to a type size_t or maybe put a cast in the conditional, but I don't know which way that is 'best'? Any ideas? I provided the code below for some context. And while we're at it, could you...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.