Reputation: 85
I'm trying to learn C++ from a Python background. I've been finding it difficult to find C++ learning materials that have engaging examples or toy program guides that feature something simple yet meaningful to me (don't get me wrong - there's tonnes of explanatory material but I find I learn best through toy examples that nonetheless have some utility).
So as an exercise, I want to write a program in C++ that can basically store a bunch of sentences that are being inputted line-by-line, and then print out those sentences all at once. It may be a little easier to explain if I just show how I would write this in Python 3:
print("Your notes are now being recorded. Type 'e' and press enter to stop taking notes.")
note_list = []
for i in note_list:
a = input()
if a == 'e':
break
note_list.append(a)
for i in note_list:
print(i)
I'm not under any illusions that this can be expressed nearly as easily in C++, but I'm having trouble knowing how to replicate the string storage component.
At the moment I'm shamefully only able to express my starting prompt and initialise a string value in C++ as follows:
# include <iostream>
# include <string>
int main()
{
std::cout << "Your notes are now being recorded. Type 'e' and press enter to stop taking notes.\n";
std::string x;
int flag = 1;
while (flag == 1)
{
// presumably a function will go here that adds any input unless
// it's 'e' which will make the flag = 0
std::getline(std::cin, x) // some variation of this maybe?
}
// Once I have my collection of strings (an array of strings?) I assume I
// use a C++ for loop very similar to how I might use a Python for loop.
return 0;
}
Could I please have some direction as to how I might achieve my goal? Even just some general directions to specific resources would be fantastic.
Again, my main point of uncertainty is in figuring out how to store each string in a way similar to the way I would simply append them a list in Python (or at least as easily as possible).
My apologies if the question is a little broad.
Thanks.
Upvotes: 0
Views: 86
Reputation: 428
What you're looking for here is two different things: storing input until a condition is met, and then outputting each of those stored inputs.
To collect the inputs, given that you don't know how many to expect, I'd use a vector - it's similar to a python list in that it is a dynamic collection type. You'll need to include it:
#include <vector>
There are multiple ways to get a value into a vector, but push_back(value)
works similarly to python's list.append
- it adds the specified value to the end of the collection.
std::vector<std::string> inputs; // A vector (of strings) to collect inputs
while(true){ // Infinite loop
// Retrieve input
std::string input;
std::getline(std::cin, input);
// Check for terminal input
if(input == "e"){
break;
}
// Add the input to our collected inputs
inputs.push_back(input);
}
To output the stored inputs, you could use a traditional for loop, but coming from a python background you may find that a range-based for loop (also referred to as a for-each loop) feels more familiar.
// Range-based for loop
for(const auto& output : inputs){
std::cout >> output >> endl;
}
Upvotes: 3