[2 problems] getline - do while loop condition ?

Because the problems had been solved, I decide to add the answer to top post:
The solution: http://www.cplusplus.com/forum/beginner/189400/#msg917755
Explain: http://www.cplusplus.com/forum/beginner/189400/#msg918156
More hint:
1. http://www.cplusplus.com/forum/beginner/189400/#msg918164 (array problem)


I'm sorry for stupid title, but I think that is the most general title I could come up with. Anyway, I got 2 problems which I cannot figure it out the solution. First, let's take a look on my code:

CPP: cpp.sh/65zv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
using namespace std;

class Number {
	int arrPos = 0;
	int arr[];
	public:
		void get(void);
		void print(void);
};

int main() {
	Number n;
	n.get();	
	n.print();
	return 0;
}

void Number::get(void) {
	string stop;
	do {
		cout << "Enter number: ";
		cin >> arr[arrPos];
		arrPos++;
		cout << "Stop now? [y|n] ";
		getline(cin, stop);//Problem: Can't use it
	} while(stop != "y" || stop != "Y");//Problem: the statements would reach to condition.
}

void Number::print(void) {
	string verbForm = ( (arrPos - 1) != 0) ? "is the number" : "are numbers";
	cout << "\nHere " << verbForm << " you have added: \n";
	for (int i = 0; i == arrPos; i++) {
		string end = (i == arrPos) ? ".\nThat's all." : ", ";
		cout << arr[i] << end;
	}
}


The idea:
My idea is let user enter a natural numbers as much as they please then print all of it out.

More infos:
- Windows 10 OS
- Compiler C++11 | CGG 4.9.2 | C++ standard library
- Notepad++ | CMD DOS on Window
=> All of above are my tools and environment that I worked.


Here are 2 problems:
1. Function getline() cannot work probably. Don't know why?
2. The condition in do-while loop doesn't work as I excepted.


My solution:
1. I gave up on using getline() and using cin >> instead. But it soon stuck the 2nd problem.
2. I'm totally given up on this problem.


I hope sb could gimme a hand to find out how this code actually work and a better solution to fix it. Thanks in advanced.
Last edited on
The do-while problem is easy to fix:
while(stop != "y" && stop != "Y");//Notice that we are using AND instead of OR
So this way, if stop is "y" OR "Y", it would stop. This would also work:
while(!(stop == "y" || stop == "Y"));
I'm not sure what caused the getline() problem. Maybe it has something to do with the stream buffer? Try adding this after getline()(if it doesn't work, try adding it before):
1
2
cin.ignore(255, '\n');
cin.clear();


And if I were you, I would use a single char, since the response is either 'y' or 'Y'; it would save you a lot of memory. And you can use cin >> stop; and then clear the buffer in case user entered more than one char, instead of doing it using a std::string.
Thanks a lot for you help, but I still have a little confusion.

1. I don't understand why should && works but || didn't. If stop is not "y" and "Y", the loop will continue, which means the stop must have 2 characters (values) "y" and "Y" at the same time. I just want to input 1 value, "y" or "Y" is fine. So your suggestion (&&) really confuses me. Could you please clarify how the condition works with loop?

2. Yeah, I agree with you solution that using cin. Actually I did. But I just wonder how come getline() didn't work. And you said it could involve 'stream buffer'. I guess I will do some research on it. But do you have any ideas what 'stream buffer' is, how it play a role in in/output?

I'm sorry for asking so much, but since I'm newbie and I wanna learn more (in careful way) about it. I suppose I've no choice but asking and looking. I hope you don't mind to help me.

Thanks again.
1. The logic is a bit hard to explain... Just think about it this way: If stop is not "y" AND that stop is not "Y" either, the expression before and after the && evals to true, and loop continues. If stop is "Y" or "y", one of stop != "Y" and stop != "y" would eval to false, which stops the loop. I know this seems very complicated, but it really is not. Try looking at the second solution, while(!(stop == "y" || stop == "Y"));, and see if you can understand it better than the first one.

2. Here's some info on stream buffers: http://www.learncpp.com/cpp-tutorial/131-input-and-output-io-streams/ The whole chapter 13 is on streams. Don't worry if you can't understand it for now, since streams are in chapter 13, not 1 (which proves if you just want to print to the screen, you don't need to worry that much). Also I suggest you take a look at learcpp.com, it's a pretty good website. I first learned C++ from there.
I have no idea why getline() doesn't work. Sorry.
Line 7: How many ints do you think are being allocated? Hint: None. That is an array of size 0. Any attempt to store into that array will cause undefined behavior. It should cause a bounds error if you're running a debug build.



Last edited on
@Tyler T : Thanks a lot, that was a big help, I can't expect anything more. Thank you very much.

@AbstractionAnon : Thanks for you help, I hope I could ask you about the term 'undefined behavior'. Since I'm still a newbie to C++ so that term is also new to me too. What problem could it cause to my program? At the moment I run, I still didn't see yet. And what should I do in order to prevent 'undefined behavior' problem?
Regarding 'undefined behaviour'.

A programming language like C++ can be thought of as like a contract, an agreement, between the programmer and the machine. It contains a set of rules which define exactly what will happen when code is written which follows those rules.

The agreed rules can be thought of as an enclosed space, with well defined boundaries. Inside that space, everything is well-ordered and understood. Outside of the boundary is the whole of the rest of the universe, anything at all can happen there.

What problem could it cause to my program? At the moment I run, I still didn't see yet.

There possibilities are limitless. You might not notice anything wrong. The program might give incorrect results. It might cause some other unrelated part of the program to malfunction. The program could crash. The entire computer could crash. Anything.

And what should I do in order to prevent 'undefined behavior' problem?

Try to write valid code which follows not just the required syntax, but also proper usage. C++ is a very flexible and powerful language, and with that comes responsibility.
Last edited on
Topic archived. No new replies allowed.