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.