I think this question is more suited for codereview, but since the code does not compile as is, we may as well help you with your broken code (and then you take it to codereview)
First, let's format your code. This is a useful skill to learn because it helps other coders help you write better code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string question;
cout << "Type [1] to begin...";
cin >> question;
if(question == "1") {
cout << "A computer programmer figures out the process of
designing, writing, testing, debugging, and maintaining the source c
ode for computer programs";
return 0;
}
}
Easiest way to format is to copy it into an IDE, use the IDE to format, then copy it back here, select the code and press the
button.
Now to solve this problem.
Your question seems centred around controlling the flow of the program - being able to transition from one stage to the next in a way that puts the user in control and only delegates control back to your program once the user has made a decision.
The problem
- Ask the user to enter a
1
- Display the following text
A computer programmer figures out the process of designing, writing,
testing, debugging, and maintaining the source code for computer
programs
- Ask the user if they wanted to continue
- If so, display the following:
what are programming languages?
4b. If not, end the program.
- Ask the user if they wanted to continue
- etc, etc
As you can see, there is indeed a pattern and this pattern comes down to the following:
- Ask what the user wants to do
- Do it
- Repeat until you run out of slides or the user doesn't want to continue
And just like that, we have abstracted away the complexity and are only focused on following the pattern.
Pay attention to the repeat
part because that is what allows this pattern to work for more than one slide of your presentation. There are many ways to represent the repeat
part, and for that you should find some good tutorials to teach you some of them. I won't bother describing all of them (just search youtube, you will find tons), but for this particular problem, the best way to represent your pattern is with a do-while
loop.
Here is what it will look like:
do {
// Ask the user a question
// Get the user's input
// validate the user's input
// if they want to see the slide show it
// other wise, leave this loop
while (I have not run out of slides);
This is psuedo-code, but here is how it transforms your code:
#include <iostream>// cin, cout
#include <string> // string
#include <vector> // vector
#include <cstddef> // size_t
using namespace std;
int main() {
vector<string> slides = {
"A computer programmer figures out the process of"
"designing, writing, testing, debugging, and maintaining the source c"
"ode for computer programs",
"what are programming languages?",
// Add more here
};
size_t current_slide_index = 0;
string user_response;
do {
cout << "Type [1] to continue: ";
cin >> user_response;
cin.ignore(100, '\n'); // This is used to skip to the next line
if (user_response == "1") {
cout << slides.at(current_slide_index) << std::endl;
} else {
break;
}
} while (++current_slide_index < slides.size());
cout << "Happy learning\n";
return 0;
}
A few notes
- I used a
vector
to hold the slides. This is the most recommended collection type in C++. There are many others, but for the most part, a vector will serve you well.
cin >>
does not normally go to the next line after reading something, so I had to manually shift it to the next line. That's the reason for cin.ignore(100, '\n');
As I said in the beginning, this question is more suited for codereview
, so take what I've shown you here, make your changes as you learn more about it, and later on have it reviewed once again by the folks at https://codereview.stackexchange.com/.