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[i])==false)
{
strcpy(customers[i].msg, "invalid pakage");
}
else if(vallength(customers[i])==false)
{
strcpy(customers[i].msg, "invalid length pakage");
}
else if(valdigit(customers[i])==false)
{
strcpy(customers[i].msg, "invalid digit number");
}
else if(valmonth(customers[i],nyear,nmonth,nday)== false)
{
strcpy(customers[i].msg, "invalid month");
}
else if(valday(customers[i],nyear,nmonth,nday)==false)
{
strcpy(customers[i].msg, "invalid day");
}
else if(valyear(customers[i],nyear)==false)
{
strcpy(customers[i].msg, "invalid year");
}
else if(valhour(customers[i],nyear,nmonth,nday)==false)
{
strcpy(customers[i].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[i];
}
year[j]='\0';
nyear =atoi(year);
for (int i=0,j=0;i<2;i++,j++);
{
month[j]=customers.date[i];
}
month[j]='\0';
nmonth= atoi(month);
for (int i=2,j=0;i<4;i++,j++)
{
day[j]=customers.date[i];
}
day[j]= '\0';
nday=atoi(day);
}
void showdata(CustRec customers[])
{
for(int i=0;i<num1;i++)
{
cout<<customers[i].cid <<" "
<<customers[i].pkg <<" "
<<customers[i].hrs <<" "
<<customers[i].date <<" "
<<customers[i].msg <<endl;
}
} 8 2003
"Aamir Wahid" <aa********@hotmail.com> skrev i meddelandet
news:d6*************************@posting.google.co m... 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 bo**@telia.com
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;
--
Adam Clauss ca*****@tamu.edu
"Bo Persson" <bo**@telia.com> wrote in message
news:uw**************@tk2msftngp13.phx.gbl... "Aamir Wahid" <aa********@hotmail.com> skrev i meddelandet news:d6*************************@posting.google.co m... 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 bo**@telia.com
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?
--
Adam Clauss ca*****@tamu.edu
"Sean Cavanaugh" <se***@gearboxsoftware.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl... Adam Clauss wrote: 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 C style cast always has the chance to devolve into the equivalent of a reinterpret_cast, which can have pretty bad side effects if it happens when you weren't expecting it to. static_cast only allows the cast if its possible (which includes downcasting a class hierarchy blindly
however).
Adam Clauss wrote: 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;
}
--
sashan http://sashan.netfirms.com
"Adam Clauss" <ca*****@tamu.edu> wrote... 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 bo**@telia.com
Adam Clauss wrote: 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");
}
"Carl Daniel [VC++ MVP]" <cp******@nospam.mvps.org> wrote in message 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.
Aaron Queenan wrote: "Carl Daniel [VC++ MVP]" <cp******@nospam.mvps.org> wrote in message 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.
Only classes with virtual functions support RTTI, according to the C++
Standard.
-cd This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William Xuuu |
last post by:
Hi,
This's a way of defining size_t and ssize_t in Linux:
//"linux/types.h"
typedef __kernel_size_t size_t;
typedef __kernel_ssize_t ssize_t;
//"asm/posix_types.h"
typedef unsigned...
|
by: |
last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I
keep getting the following error:-
Error In:...
|
by: drazbliz |
last post by:
Hello,
I am running the following code segment in visual studio 2005.
size_t cchSize = 10*sizeof(TCHAR);
LPTSTR temp = (LPTSTR)malloc(cchSize);
temp = '\0';
_tcscat_s(temp, cchSize,...
|
by: dembla |
last post by:
Hey Frnds can anyone help me in this
i need a program in 'c'
PROGRAM to print NxN Matrix
9 1 8 1 2 3
2 7 3 as 4 5 6
6 4 5 7 8 9
in sorted form
|
by: Mark Odell |
last post by:
I've always declared variables used as indexes into arrays to be of
type 'size_t'. I have had it brought to my attention, recently, that
size_t is used to indicate "a count of bytes" and that using...
|
by: Gary Wessle |
last post by:
Hi
in the gsl, I am trying to use a function but getting this error
****************************************************************
read_data.o: In function `read_data::matrix_the_file()':...
|
by: Tubular Technician |
last post by:
Hello, World!
Reading this group for some time I came to the conclusion that
people here are split into several fractions regarding size_t,
including, but not limited to,
* size_t is the...
|
by: jacek.dziedzic |
last post by:
Hi!
On a machine where size_t is 64-bit, unsigned long is 32-bit, how
does one construct a size_t literal that says 2^32? Typing in
size_t x = 4294967296UL;
complains about the value being...
|
by: Juha Nieminen |
last post by:
Unknownmat wrote:
VS2005 has a bug related to this. When you use size_t, it internally
converts it to 'unsigned int'. In some situations it forgets that the
type was actually size_t and only...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |