Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > jumping beens, well... jumping programs, aaahuuu-aah

Reply
Thread Tools

jumping beens, well... jumping programs, aaahuuu-aah

 
 
jalkadir
Guest
Posts: n/a
 
      10-05-2005
By jump I mean ending unexpectedly.
The program below, like the jumping beens, has an unusual behaviour.
When the noted lines are not present the program terminates
disregarding the rest of the lines.

I have seen this type of behaviour before, but I can't find to recall
the source, the needy greedy source, of the problem. I, however, was
able to remember that the problem was resolved, temporarily, by using
'std::cin.get()', but I am afraid this will lead to a bug in the
future.

Can anyone help me by explaining what causes this type of behaviour,
why my solution works and, most importantly, how to solve the problem.

Thanks in advance

---
void getData(){
jme::Address address2;
std::string str;
char* cstr;
std::cout.flush();
//std::cin.get().flush();

str.clear();
std::cout << "House/Appartment # ";
std::cin >> str;
std::cin.get(); // Without this line the program jumps <====
address2.setUnitNumber(str);

std::cout << "Enter street name: ";
std::cin.get(cstr,255);
address2.setStreetName(cstr);

str.clear();
std::cout << "City/Town: ";
std::cin >> str;
address2.setCity(str);

str.clear();
std::cout << "Province/State: ";
std::cin >> str;
address2.setProvince(str);

str.clear();
std::cout << "Country: ";
std::cin >> str;
address2.setCountry(str);

str.clear();
std::cout << "Postal Code: ";
std::cin >> str;
address2.setPostalCode(str);
std::cin.get();

Display(address2, "getData()");
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-05-2005
jalkadir wrote:
> By jump I mean ending unexpectedly.


By throwing an exception maybe? Use 'try/catch' to see if that's so.

> The program below, like the jumping beens, has an unusual behaviour.


What's a "been"?

> When the noted lines are not present the program terminates
> disregarding the rest of the lines.


Have you tried debugging it? Does it jump (throw) or does it just run
through all of them and you don't get to see how it executes them all?

> I have seen this type of behaviour before, but I can't find to recall
> the source, the needy greedy source, of the problem. I, however, was
> able to remember that the problem was resolved, temporarily, by using
> 'std::cin.get()', but I am afraid this will lead to a bug in the
> future.
>
> Can anyone help me by explaining what causes this type of behaviour,
> why my solution works and, most importantly, how to solve the problem.
>
> Thanks in advance
>
> ---
> void getData(){
> jme::Address address2;
> std::string str;
> char* cstr;
> std::cout.flush();
> //std::cin.get().flush();
>
> str.clear();
> std::cout << "House/Appartment # ";
> std::cin >> str;
> std::cin.get(); // Without this line the program jumps <====


Often, when you read a 'string' from a stream, the \n is left in the
stream, and needs to be read before the next "get" is attempted. I
recommend using

std::cin.ignore(INT_MAX, '\n');

instead of 'get'.

Beyond that I don't know what to tell you. Since you didn't post your
_complete_ program and the input you're giving it, there is no way to
reproduce your behaviour (except accidentally). Read the FAQ 5.8.

> address2.setUnitNumber(str);
>
> std::cout << "Enter street name: ";
> std::cin.get(cstr,255);
> address2.setStreetName(cstr);
>
> str.clear();
> std::cout << "City/Town: ";
> std::cin >> str;
> address2.setCity(str);
>
> str.clear();
> std::cout << "Province/State: ";
> std::cin >> str;
> address2.setProvince(str);
>
> str.clear();
> std::cout << "Country: ";
> std::cin >> str;
> address2.setCountry(str);
>
> str.clear();
> std::cout << "Postal Code: ";
> std::cin >> str;
> address2.setPostalCode(str);
> std::cin.get();
>
> Display(address2, "getData()");
> }
>


V
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-05-2005
jalkadir wrote:
>
> By jump I mean ending unexpectedly.
> The program below, like the jumping beens, has an unusual behaviour.
> When the noted lines are not present the program terminates
> disregarding the rest of the lines.
>
> I have seen this type of behaviour before, but I can't find to recall
> the source, the needy greedy source, of the problem. I, however, was
> able to remember that the problem was resolved, temporarily, by using
> 'std::cin.get()', but I am afraid this will lead to a bug in the
> future.
>
> Can anyone help me by explaining what causes this type of behaviour,
> why my solution works and, most importantly, how to solve the problem.
>
> Thanks in advance
>
> ---
> void getData(){
> jme::Address address2;
> std::string str;
> char* cstr;
> std::cout.flush();
> //std::cin.get().flush();
>
> str.clear();
> std::cout << "House/Appartment # ";
> std::cin >> str;
> std::cin.get(); // Without this line the program jumps <====


Whatever 'jump' means.
I guess you mean, the next input seems to be ignored.
Well that is easy to explain: When you enter some number, what *exactly*
do you enter? You press eg. the keys '1', then '3' (because you want to
enter the number 13) and then? Then you press 'return'. And that 'return'
is still waiting to be processed after cin >> str has fetched the '1' and '3'.
That is why you intoduced the read of a single character: To read that 'return'
still waiting for input. If you don't do it now, then the next get will grab
his hands on that waiting 'return' and conclude that the user made an empty input.

> address2.setUnitNumber(str);
>
> std::cout << "Enter street name: ";
> std::cin.get(cstr,255);


Ouuuh. That is a big no-no.
cstr is a pointer. But a pointer to where? Where is the memory
where get() should put the characters and whos address you take
from cstr?
Answer: There is none! cstr is an uninitialized pointer and only god
knows where it points to in memory. Why didn't you use the variable 'str'
as you did above to get a string from the user?


--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
jalkadir
Guest
Posts: n/a
 
      10-05-2005
Thanks Vic for your prompt response.

First of all, I'd like to get the beens thing out of the way. In
reality it is spelled beans.
The expression "jumping beans" is an expression commonly used by
people from Mexico, but because of their accent one, jokingly and
without the intention to disrespect them, refer to the use of the
expression as "Jumping Beens" or even "Jumping Beeens" to try
to convey the idea that in the word 'beans' the sound of the letter
E is extended, as it is the way the some Mexicans pronounce the word
beans.

And as my second and final point in this message, thanks again for your
help, I will be trying that ASAP.

To my fellow Mexican friends, my dearest regards and apologies if in
some way I have offended you.

Adios, good bye, Ciao, Assalamu Alaykum.

 
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
Jumping wireless Connections Greg Wireless Networking 13 08-24-2005 03:05 AM
Jumping wireless Connections Greg Wireless Networking 11 07-14-2005 09:00 PM
Number of ring before jumping to hunt group member duncan.smith Cisco 2 11-25-2004 11:03 PM
DataGrid: jumping to the append row (asterisk) David Bird via .NET 247 ASP .Net 0 04-16-2004 02:07 PM
Holy JUMPING frameset Batman! Jerry ASP .Net 0 01-27-2004 07:16 PM



Advertisments