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

J

jalkadir

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

Victor Bazarov

jalkadir said:
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
 
K

Karl Heinz Buchegger

jalkadir said:
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?
 
J

jalkadir

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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top