urgent: size_t error

A

Aamir Wahid

Hi
i was trying to run a code in c++.net and i got this following error
warning C4267: '=' : conversion from 'size_t' to 'int', possible loss
of data

i am putting my code here if any body correct that error i will be
really appreciate it.

thanks

Aamir

----- CODE --------

#include<iostream>
#include<iomanip>
#include<cctype>
#include<cstring>

#include<cstdlib>
using namespace std;

short mindate =1990;
short maxdate = 2020;
const int num1=25;

struct CustRec
{
short cid;
char pkg;
float hrs;
char date[10];
char msg[30];

};

CustRec customers[num1]= {{100,'A',18,"07182003"},
{101,'d',31,"07192003"},
{102,'b',697,"02022000"},
{103,'C',673,"02021999"},
{104,'a',745,"01311998"},
{105,'B',14,"03051989"},
{106,'c',25,"13051990"},
{107,'C',47,"06311995"},
{108,'a',53,"12345567"},
{109,'b',255,"123456789"},
{110,'D',47,"06311995"},
{120,'C',47,"0A311995"},
{130,'C',47,"0630199A"},
{140,'C',47,"000311995"},
{150,'C',47,"02291995"},
{160,'C',47,"02292000"},
{170,'C',47,"02281995"},
{180,'C',47,"09311995"},
{190,'C',47,"09311995"},
{200,'C',47,"04082020"},
{210,'C',47,"063119"},
{220,'C',0,"05151990"},
{230,'C',47,"03312003"},
{240,'C',47,"06012000"},
{250,'C',47,"07312005"}, //SHORT LENGTH
};

void valrecord(CustRec[],int &,int &,int &);
bool valpkg(CustRec);
bool vallength(CustRec);
bool valdigit(CustRec);
bool valmonth(CustRec,int &, int &, int &);
bool valyear(CustRec, int &);
bool valday(CustRec,int &, int &, int &);
bool valhour(CustRec, int &, int &, int &);
void parseDate(CustRec,int &, int &, int &);
void showdata(CustRec[]);

void main()
{
int nyear=0;
int nmonth =0;
int nday =0;

valrecord(customers,nyear,nmonth,nday);
showdata(customers);

}

void valrecord(CustRec customers[],int &nyear, int &nmonth, int &nday)
{
for(int i=0;i<num1; i++)
{
if(valpkg(customers)==false)
{
strcpy(customers.msg, "invalid pakage");
}
else if(vallength(customers)==false)
{
strcpy(customers.msg, "invalid length pakage");
}

else if(valdigit(customers)==false)
{
strcpy(customers.msg, "invalid digit number");
}
else if(valmonth(customers,nyear,nmonth,nday)== false)
{
strcpy(customers.msg, "invalid month");
}

else if(valday(customers,nyear,nmonth,nday)==false)
{
strcpy(customers.msg, "invalid day");
}

else if(valyear(customers,nyear)==false)
{
strcpy(customers.msg, "invalid year");
}

else if(valhour(customers,nyear,nmonth,nday)==false)
{
strcpy(customers.msg, "invalid hour");
}
}
}

bool valpkg(CustRec customers)

{
if (toupper(customers.pkg)=='A' || toupper(customers.pkg)=='B' ||
toupper(customers.pkg)=='C')
return true;
else
return false;
}

//

bool vallength(CustRec customers)
{
int len1;
len1 = strlen(customers.date);
if(len1!=8)
return false;
else
return true;
}

bool valdigit(CustRec customers)
{
for(int count=0;count < 10;count++)
{
if(isalpha(customers.date[count]))
return false;
}
return true;
}


bool valmonth(CustRec customers,int &nyear,int &nmonth,int &nday)
{

parsedate(customers,nyear,nmonth,nday);

if(nmonth >= 1 && nmonth <= 13)
return true;
else
return false;
}

bool valday(CustRec customers,int &nyear,int &nmonth,int &nday)
{

int chDay[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(nmonth==2)
{
if(nyear % 4==0)
if((nday > 0) && (nday <=29))
return true;
else
return false;
else if((nday > 0)&& (nday <=28))
return true;
else
return false;
}
else if((nday > 0)&& (nday <=chDay[nmonth -1]))
return true;
else
return false;
}

bool valyear(CustRec customers,int &nyear)
{
if((nyear < mindate) || (nyear > maxdate))
return false;
else
return true;
}


bool valhour(CustRec customers,int &nyear,int &nmonth,int &nday)
{
int checkhour[12]={744,672,744,720,744,720,744,744,720,744,720,744};
if(nmonth==2)
{
if(nyear %4 == 0)
if (customers.hrs >=0 && customers.hrs <=696)

return true;
else
return false;

if (customers.hrs >=0 && customers.hrs <=672)

return true;
else
return false;
}
else
if (customers.hrs >=0 && customers.hrs <=checkhour[nmonth-1])

return true;
else
return false;
}

void parsedate(CustRec customers,int &nyear,int &nmonth,int &nday)
{
char year[5];
char month[3];
char day[3];

for (int i=4,j=0;i<8;i++,j++)
{
year[j]=customers.date;
}
year[j]='\0';
nyear =atoi(year);

for (int i=0,j=0;i<2;i++,j++);
{
month[j]=customers.date;
}
month[j]='\0';
nmonth= atoi(month);

for (int i=2,j=0;i<4;i++,j++)
{
day[j]=customers.date;
}
day[j]= '\0';
nday=atoi(day);
}

void showdata(CustRec customers[])
{
for(int i=0;i<num1;i++)
{
cout<<customers.cid <<" "
<<customers.pkg <<" "
<<customers.hrs <<" "
<<customers.date <<" "
<<customers.msg <<endl;
}
}
 
B

Bo Persson

Aamir Wahid said:
Hi
i was trying to run a code in c++.net and i got this following error
warning C4267: '=' : conversion from 'size_t' to 'int', possible loss
of data

size_t is unsigned while int is signed. This means that some values
cannot be converted between these types. The compiler obviously
doesn't know whether it will work or not at this point. Do you know?

If you know for certain that the conversion will *always* work, you
can tell that to the compiler buy doing a static_cast<int>(value) at
the proper location. If you can't tell either, there is a real
problem!


Bo Persson
(e-mail address removed)
 
A

Adam Clauss

Somethign I've always wondered...
What is the point of static_cast/dynamic_cast?
Why not just a normal cast?
ex:
int x = (int)some_size_t_var;
 
A

Adam Clauss

Hmm... so if I'm understanding this write - as long as you are careful with
what you write, it shouldn't make a difference.
It will only catch mistakes in the event you try to cast something that
isn't "compatible" (for lack of a better word). Which, if you code it
right, shouldn't be necessary anyhow right?
 
S

sashan

Adam said:
Hmm... so if I'm understanding this write - as long as you are careful with
what you write, it shouldn't make a difference.
It will only catch mistakes in the event you try to cast something that
isn't "compatible" (for lack of a better word). Which, if you code it
right, shouldn't be necessary anyhow right?
Just to clarify if you are using static_cast it won't catch the mistake
if you cast blindly down a hierarchy. A dynamic_cast will return 0 if
the object pointed to has a mismatched type. For example:

//assume Base is the base class for Derv1 and Derv2
int main()
{
Base* pBase[2];
pBase[0] = new Derv1;
pBase[1] = new Derv2;

//static_cast performs the cast
Derv1* pSCast = static_cast<Derv1*>(pBase[1]);

//dynamic_cast returns 0 because pBase[1] points to an object of
type Derv2 but we are trying to cast to an object of Derv1
Derv1* pDCast = dynamic_cast<Derv1*>(pBase[1]);

delete pBase[0];
delete pBase[1];
return 0;
}
 
B

Bo Persson

Adam Clauss said:
Hmm... so if I'm understanding this write - as long as you are careful with
what you write, it shouldn't make a difference.

The fact that you need a cast is a bit suspect, in the first place.
:)
It will only catch mistakes in the event you try to cast something that
isn't "compatible" (for lack of a better word). Which, if you code it
right, shouldn't be necessary anyhow right?

Sort of. In C++ static_cast, const_cast, and interpret_cast do
different things, without checking. You have to tell the compiler
though exactly *which* type of cast you intended. A C style cast just
does whatever is needed, whether intended or not, in a "just do it"
kind of style.

A dynamic_cast is new for C++, because it handles class hierachies
with inheritance. It checks, possibly at runtime, that the cast is
actually valid. In C you don't have that problem, because you don't
have classes.


The style of these casts are also intentionally long and ugly looking,
as a hint that that they shouldn't really be used much. They are also
easy to spot in the source code, so you would have to come up with a
good excuse during the code walkthru!


Of course, if you always write bug free code in a style that the
maintainance guys understand, none of his really matters. :)



Bo Persson
(e-mail address removed)
 
S

Sean Cavanaugh

Adam said:
Hmm... so if I'm understanding this write - as long as you are careful with
what you write, it shouldn't make a difference.
It will only catch mistakes in the event you try to cast something that
isn't "compatible" (for lack of a better word). Which, if you code it
right, shouldn't be necessary anyhow right?

Right, static_cast catches mistakes. This is the cast you should use
95+% of the time, with the majority of the remainder becoming
dynamic_cast to deal with C++, and the even smaller remainder left over
for a true reinterpret_cast. Try compiling this test program (after
commenting out the lines that cause VC7 to choke, check out the output).
The Variable crD had a reinterpret_cast applied to it, but you
couldn't tell by looking at the code or compile output. crB was caught
by the compiler. crC has a nice beautiful reinrepret_cast on it I can
search for through the code easily, which is vital for code maintenance.


#include <stdio.h>

class Bar
{
public:
int x;
Bar() : x(1) {}
int GetValue() const { return x; }
};

class Cat
{
public:
int x;
Cat() : x(2) {}
int GetValue() const { return x; }
};

class Foo
{
public:
int x;
Bar m_Bar;

operator Bar&()
{
return m_Bar;
}
Foo() : x(0) {}
int GetValue() const { return x; }
};

int main(int argc, const char** argv)
{
Foo f;
Bar b;
Cat c;

Foo& frA = f;
Bar& brA = b;
Cat& crA = c;

Foo& frB = static_cast<Foo&>(f);
Bar& brB = static_cast<Bar&>(f);
Cat& crB = static_cast<Cat&>(f);

Foo& frC = reinterpret_cast<Foo&>(f);
Bar& brC = reinterpret_cast<Bar&>(f);
Cat& crC = reinterpret_cast<Cat&>(f);

Foo& frD = (Foo&)f;
Bar& brD = (Bar&)f;
Cat& crD = (Cat&)f;

printf("frA = %d\n", frA.GetValue());
printf("brA = %d\n", brA.GetValue());
printf("crA = %d\n", crA.GetValue());
printf("\n");

printf("frB = %d\n", frB.GetValue());
printf("brB = %d\n", brB.GetValue());
printf("crB = %d\n", crB.GetValue());
printf("\n");

printf("frC = %d\n", frC.GetValue());
printf("brC = %d\n", brC.GetValue());
printf("crC = %d\n", crC.GetValue());
printf("\n");

printf("frD = %d\n", frD.GetValue());
printf("brD = %d\n", brD.GetValue());
printf("crD = %d\n", crD.GetValue());
printf("\n");
}
 
A

Aaron Queenan

Carl Daniel said:
dynamic_cast has additional behavior, since it determies at runtime if the
cast can be made and performs the necessary pointer adjustments, or returns
null (or throws) if the cast cannot be made. dynamic_cast can only be
applied to pointers or references to classes with virtual functions.

.... or which support RTTI.
 

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

Top