4

I'm working on a school project (I'm in seventh grade) and it's about why I want to be a computer programmer. I have to make a presentation of sorts about what a computer programmer is and what they do. I thought it would be a good idea to code my own presentation in a way. I've coded some of it already but i'm stuck. This is what I have so far,

    #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;
     }

    }

Now what i want to be able to do is add a "goto" type of statement where it can go to something like "int second()" and cout something new like "what are programming languages?" and then a description of what they are after the user inputs something like "yes". Any help would be appreciated. I'm really new to c++. Thanks :)

Seth. C
  • 59
  • 2
  • PHP, Python - this is what I'd suggest starting with. Not C++ – Marcin Orlowski Jan 27 '18 at 02:23
  • C++ is just fine, where there is a will, there is a way. – user1095108 Jan 27 '18 at 02:24
  • 6
    Anyways, if you feel the urge to insert a `goto`, you are probably in dire need of a good C++ book. – Baum mit Augen Jan 27 '18 at 02:25
  • 1
    I've used python but there seemed to be more use of c++ in what i was interested in doing, so now i'm here. – Seth. C Jan 27 '18 at 02:27
  • 5
    There is nothing wrong with starting with C++, but it is not the most straight forward language, mostly due to historical reasons. I would *highly* recommend learning it from a good(!) book. – Baum mit Augen Jan 27 '18 at 02:29
  • do you have any in mind? I wouldn't mind buying some online – Seth. C Jan 27 '18 at 02:30
  • 1
    https://stackoverflow.com/q/388242/3002139 I personally used "The C++ programming language", but I had some prior experience in programming. – Baum mit Augen Jan 27 '18 at 02:30
  • anyone know how to do this though? haha – Seth. C Jan 27 '18 at 02:49
  • 3
    It sounds like you are asking for a comprehensive description of C++ control flow facilities. I know I already mentioned books twice, but that's really the most effective way to learn the basics, as opposed to asking broad questions like this one. – Baum mit Augen Jan 27 '18 at 02:54
  • You might first try to work through some examples of what sequence of input and output you might have. Most real-life programs can behave very differently when given different inputs. Do you want a program that always prints out the same paragraphs of text (pausing for you to input something before each paragraph), or do you have something else in mind? That's the kind of question to ask yourself before you choose any particular C++ control keywords. – David K Jan 27 '18 at 02:59
  • try a loop plus a switch function. :) – Jem Eripol Jan 27 '18 at 03:36
  • Straustrup's "The C++ Programming" language is a book for advanced programmers and professionals, don't even think to start with that. Start with C language and the classical book "C Programming Language" be Kernigan and Ritchie. – iantonuk Jan 27 '18 at 04:07
  • C++ Programming, From Problem Analysis to Program Design by D.S. Malik, was a good book to start for me without any previous programming experience. It describes the basics of the C++ and programming with extensive coding examples after every concept. Once you are done with it, there is always the infamous C++ primer plus. – Luc Aux Jan 27 '18 at 05:22
  • 1
    I believe Stroustrup book's ["Programming, Principles and Practice Using C++"](http://stroustrup.com/Programming/) is a good book is you start programming. The author is the creator of C++ and this book is made for apprentice coder. – Oliv Jan 27 '18 at 09:46

5 Answers5

3

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 enter image description here 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

  1. Ask the user to enter a 1
  2. Display the following text

A computer programmer figures out the process of designing, writing, testing, debugging, and maintaining the source code for computer programs

  1. Ask the user if they wanted to continue
  2. If so, display the following:

what are programming languages?

4b. If not, end the program.

  1. Ask the user if they wanted to continue
  2. 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/.

smac89
  • 39,374
  • 15
  • 132
  • 179
2

I think you can try a pattern like this:

#include <iostream>
#include <string>

using namespace std;

void q1()
{
    cout << "A computer programmer figures out the process of "
            "designing, writing, testing, debugging, and maintaining the source "
            "code for computer programs.\n";
}

void q2()
{
    cout << "what are programming languages? ...\n";
}

// void q3() ... ... ...

int main()
{
    string question = "1";
    cout << "Type [1] to begin... ([99] for quiting): ";

    cin >> question;

    /* while loop: http://en.cppreference.com/w/cpp/language/while */
    while (question != "99") {

        /* if statement: http://en.cppreference.com/w/cpp/language/if */
        if (question == "1") {
            q1();   // this is a "function call", you are invoking q1() 
        }

        else if (question == "2") {
            q2();
        }

        // else if(... q3() ... q4() ... and so on.

        /* read a new response for checking in the while condition */
        cout << "Next? ";
        cin >> question; 
    }

    return 0;

}
Loreto
  • 674
  • 6
  • 20
0

Using the the goto can be accomplished as below. You can also use the SWITCH..CASE to accomplish the same.

int main()
    {
      string question;
      label:
      cout << "Type [1,2,....] to begin...";
      cin >> question;
      if(question == "1")
    {

      cout << "A computer programmer figures out the process of designing, writing, testing, debugging, and maintaining the source code for computer programs" << endl;   
        goto label;

     }
     if(question == "2")
     {
         cout << " A programming language is a type of written language that tells computers what to do in order to work" << endl;
    }
Paul
  • 448
  • 1
  • 6
  • 14
  • Never use gotos if you want to learn programming. It's a horrible practice, unless you're professional assembly programmer and know what you're doing. No one will let you use goto in high-level production code. – iantonuk Jan 27 '18 at 04:16
  • Please provide **that** solution then. Also, I don't think SO should be used to solve homeworks. – iantonuk Jan 27 '18 at 04:26
-3

First of all it's fantastic that you want to be a programmer and I wish you luck on your assignment. You can certainly ask about learning c++ here, but this site is focused on question and answer, not on teaching. You have a pretty specific question here, but it can be solved in a broad variety of ways, which I'm sure you'll learn about soon.

What I would recommend for a presentation is to ignore the input so you don't have as many branches in your code. Then you can simply add the next part directly after the first.

  string question;
  cout << "Type [1] to begin...";
  cin >> question;

  cout << "A computer programmer figures out the process of 
     designing, writing, testing, debugging, and maintaining the source c 
     ode for computer programs";   

  cout << "Type [1] to continue...";
  cin >> question;

  cout << "Part 2";   


  return 0;
Jay
  • 636
  • 6
  • 19
-4

On the language choice: C++ is definitely the wrong language to start. Because it's the most complicated programming language in existence(it's not an opinion, it's Science). You can't fully understand C++ without understanding other subset languages it contains. For example, C.

Don't listen to people that say you can code without complete understanding. You'll waste your time and won't become real engineer.

C is most stable and continuously respected programming language in Human History. Most modern CS celebrities like Mark Zucker., etc, started with C.
C can look as impressive as C++, if that's what you're interested in.

On the problem: What you're doing is console dialog with finite determined input. In CS it's called "finite automata" or "state machine". State machine can be represented as a bunch of circles(states) and arrows between them: "Do this if the next input is that". Pick a starting circle and the end circle. i.e. your program terminates when it gets there. Such diagram are really that simple.

Step-by-step solution(fill in blanks):

0)Define IO. Your input: 1 integer:

 int input;

Your output: const strings of characters.

1) Draw state machine diagram, with states and arrows between them. Denote each arrow as Integer, output_string. For example, '1, "A computer programmer figures.."' - is an arrow from the starting state (0) to some other state (1). 2) Create integer for states: int state = 0; 3) Translate your state machine into diagram as following:(You don't need goto's. Loop can play as a goto)

while(scanf("%d", &input)){
   switch(state){
   case 0:
      switch(input){
       case 1:
       printf("A programmer blabla\n");
       state = 2;
       break;
       case 2:
       ...
      {
   break;
   case 1:
    ...
   case 10: // - last state
       switch(input){
       ...
       default:
       printf("goodbye");
       return 0; // terminate the program;
       }
   }
}

You need to know about while loops, Switch statements, printf() and scanf(). Wikipedia is ok.

After you did this put the code inside main function and make necessary includes and you're good to go. You need to complete your homework yourself.

iantonuk
  • 1,178
  • 8
  • 28
  • Great idea to use switch-case for this problem, and also to use numbers as the input, however the way you have structured your answer adds more questions than it does answers. For starters, you are suggesting a totally new language than what the OP was asking (C does not equal C++). Next you bring up the topic of FSA's when OP wanted to know how to ask for input and do something with the input. I appreciate your answer, but consider dumbing it down a bit i.e. no FSA's talk or leave that towards the end, and also use C++ because that's the language the question is in. Thanks – smac89 Jan 27 '18 at 04:18
  • C language is a part of C++. Therefore You can't learn C++ without learning C, but you can learn C without learning C++. I can provide you prooflink for C++ standard book saying these words, if you don't believe it. If you think you know know C++ when you don't know C, you don't know either. – iantonuk Jan 27 '18 at 04:22
  • I explained it's pretty clearly in the question. The OP asks about learning guidence, and it's important because that learning C++ without learning C first is wrong. – iantonuk Jan 27 '18 at 04:24
  • 3
    I'm downvoting because you are stating opinion as facts. – Passer By Jan 27 '18 at 05:34
  • Which opinion is stated as a fact? – iantonuk Jan 27 '18 at 05:43
  • 1
    You can't know all of C++ without knowing most of C. But you can definitely learn enough C++ to solve many problems hardly knowing any C. In C++ you can use standard data structures for most needs and you don't have to use manual memory management for a while, whereas in C you need malloc almost from day one. Also "it's important because that learning C++ without learning C first is wrong" is basically the opposite of the consensus opinion in the C++ community; everyone says *not* to teach C++ via C first. – Nir Friedman Jan 27 '18 at 12:50
  • *"C++ ... it's the most complicated programming language in existence"* Well, debatable... https://www.quora.com/Which-is-the-most-difficult-programming-language-to-learn-and-why ;) Also: https://www.youtube.com/watch?v=YnWhqhNdYyk – Bob__ Jan 27 '18 at 18:13