[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: jumping beens, well... jumping programs, aaahuuu-aah
From: |
Thomas Maeder |
Subject: |
Re: jumping beens, well... jumping programs, aaahuuu-aah |
Date: |
Wed, 05 Oct 2005 20:20:47 +0200 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux) |
"jalkadir" <jalkadir@gosonic.ca> writes:
> void getData(){
> jme::Address address2;
> std::string str;
> char* cstr;
In general, it's a good idea to define variable where you initialize
them. If that's not possible, move the definition just above the
initialization.
Also, avoid overly long functions. I personally dislike functions of
>=10 lines - they are just longer that what I can easily grasp.
> std::cout.flush();
> //std::cin.get().flush();
>
> str.clear();
Pointless. str is already empty.
> std::cout << "House/Appartment # ";
> std::cin >> str;
As every input operation, this can fail. First check for success, then
use str. E.g.:
std::string str;
if (std::cin >> str)
; // use str
else
; // handle input failure
> std::cin.get(); // Without this line the program jumps <====
> address2.setUnitNumber(str);
>
>
>
> std::cout << "Enter street name: ";
> std::cin.get(cstr,255);
Starting here, your program has undefined behavior. cstr is read but
has never been initialized. That's one reason to define where you
initialize.
In this case, using a char * doesn't make sense, by the way. Use
std::string - this is what it was created for.