473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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()");
}

Oct 5 '05 #1
3 1733
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
Oct 5 '05 #2
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
kb******@gascad.at
Oct 5 '05 #3
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.

Oct 5 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matthew Sims | last post by:
Python Newbie here. This is my first time learning object-oriented programming and trying to break out of the usual Korn/Perl/PHP style of programming. Having some difficulty understand some items....
4
by: Jim Bunton | last post by:
New to MySql: I can't seem to get a grab on what is a schema and what is a database So I have seen! the 'Mysql' Database and the Test Database I'm runninng the database Administrator and thinking...
6
by: Steve Lefevre | last post by:
Hey folks -- I have a series of selectboxes on a web form. I would like to have a javascript jump the focus to the next selectbox when the user presses a key. Each box has the values 1 through...
17
by: compassSoftware | last post by:
Hi, I have two horizontal frames, one on top of the other. The top frame is an image map of a street with property boundary's on it. The bottom frame is a table with each row of the table...
4
by: andrew | last post by:
Hi, Let me know if I should ask this in a microsoft jscript group instead as the problem I'm witnessing only seems to appear in IE. Or if you can recommend another group if this is not an...
2
by: Scott Eade | last post by:
I am using select()/focus() to position the cursor at a particular field on a form. This works, but when the user presses the Tab key in IE the cursor jumps to the address bar rather than the next...
2
by: Chris Dunaway | last post by:
I can't believe that this late in the day that no has mentioned yet that VS2005 is now available on MSDN!! I figured there would be shouts ringing though the hills!
4
by: Pupeno | last post by:
Hello, I want to jump over a method in the class hierarchy, that is: If I have class A(object), clas B(A), class C(B) and in C, I want a method to do exactly what A does but not what B does in...
19
by: Steve | last post by:
Hi there, I'm having a problem with a webpage where hoving over the top navigation links causes my footer to jump up to the centre of the page! It only happens with IE7, and works fine with the...
7
by: duli | last post by:
Hi: I would like recommendations for books (in any language, not necessarily C++, C, python) which have walkthroughs for developing a big software project ? So starting from inception, problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...

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.