用c ++创建演示文稿(我被困)

时间:2018-01-27 02:20:30

标签: c++

我正在从事一个学校项目(我在七年级),这就是为什么我想成为一名计算机程序员。我必须介绍一下计算机程序员是什么以及他们做了什么。我认为以某种方式编写自己的演示文稿是个好主意。我已经对其中的部分内容进行了编码,但我已被卡住了。这是我到目前为止所做的,

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

    }

现在我想要做的就是添加一个&#34; goto&#34;语句的类型,它可以去&#34; int second()&#34;和cout一样新的东西,如#34;什么是编程语言?&#34;然后在用户输入类似&#34; yes&#34;之类的内容后对它们的描述进行描述。任何帮助,将不胜感激。我对c ++很陌生。谢谢:))

5 个答案:

答案 0 :(得分:3)

我认为这个问题更适合codereview,但由于代码不能按原样编译,我们也可以帮助您修复损坏的代码(然后将其带到代码审查中)

首先,让我们为您的代码设置格式。这是一项有用的技能,因为它可以帮助其他编码人员帮助您编写更好的代码:

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

最简单的格式化方法是将其复制到IDE中,使用IDE进行格式化,然后将其复制回来,选择代码并按下enter image description here按钮。

现在解决这个问题。

您的问题似乎集中在控制程序的流程 - 能够以一种让用户控制的方式从一个阶段过渡到下一个阶段,并且只有在用户做出决定后才将控制委托给您的程序。

问题

  1. 要求用户输入1
  2. 显示以下文字
  3.   

    计算机程序员计算出设计,编写,   测试,调试和维护计算机的源代码   程序

    1. 询问用户是否要继续
    2. 如果是,请显示以下内容:
    3.   

      什么是编程语言?

      4b中。如果没有,请结束该计划。

      1. 询问用户是否要继续
      2. 等等
      3. 正如您所看到的,确实有一种模式,这种模式归结为以下几点:

        • 询问用户想要做什么
        • 做吧
        • 重复操作,直到用完幻灯片或用户不想继续

        就这样,我们已经抽象出了复杂性,并且只专注于遵循模式。

        请注意repeat部分,因为这样可以使此模式适用于演示文稿的多张幻灯片。有很多方法可以代表repeat部分,为此你应该找到一些很好的教程来教你一些。我不打算详细描述所有这些(只搜索youtube,你会发现很多),但对于这个特殊问题,代表你的模式的最佳方法是使用do-while循环。

        以下是它的样子:

        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);
        

        这是伪代码,但以下是它如何转换代码:

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

        一些注释

        • 我用vector来抓住幻灯片。这是C ++中最推荐的集合类型。还有很多其他的,但在大多数情况下,矢量会很好地为你服务。
        • cin >>通常在读完之后不会进入下一行,所以我不得不手动将它移到下一行。这是cin.ignore(100, '\n');
        • 的原因

        正如我在开头所说的那样,这个问题更适合codereview,所以请把我在这里展示给你的内容,在你了解更多内容后进行更改,然后再审核再次由https://codereview.stackexchange.com/的人们提供。

答案 1 :(得分:2)

我认为你可以尝试这样的模式:

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

}

答案 2 :(得分:0)

使用goto可以完成如下。您也可以使用SWITCH..CASE来完成相同的操作。

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

答案 3 :(得分:-3)

首先,你想成为一名程序员真是太棒了,祝你工作顺利。你当然可以在这里询问学习c ++,但这个网站专注于问答,而不是教学。你在这里有一个非常具体的问题,但它可以通过各种各样的方式解决,我相信你很快就会知道。

我建议的演示文稿是忽略输入,因此您的代码中没有多少分支。然后你可以直接在第一部分之后直接添加下一部分。

train()

答案 4 :(得分:-4)

关于语言选择: C ++绝对是错误的语言。因为它是现存最复杂的编程语言(it's not an opinion, it's Science)。如果不了解C ++中包含的其他子集语言,则无法完全理解C ++。例如,C。

不要听那些说你可以在没有完全理解的情况下编码的人。你会浪费你的时间,不会成为真正的工程师。

C是人类历史上最稳定且持续受尊重的编程语言。大多数现代CS名人如Mark Zucker等等都是以C.
开头的 C可以像C ++一样令人印象深刻,如果这是你感兴趣的话。

关于问题: 您正在做的是具有有限确定输入的控制台对话框。在CS中它被称为有限自动机&#34;或者&#34;状态机&#34;。 State machine可以表示为一堆圆圈(状态)和它们之间的箭头:&#34;如果下一个输入是那么 &#34> ;。选择一个起始圆和结束圆。即你的程序到达时终止。这样的图表真的那么简单。

逐步解决方案(填空):

0)定义IO。 您的输入:1整数:

 int input;

你的输出:const字符串。

1)绘制状态机图,它们之间有状态和箭头。将每个箭头表示为Integer,output_string。 例如,&#39; 1,&#34;计算机程序员数字......&#34;&#39; - 是从起始状态(0)到某个其他状态(1)的箭头。 2)为状态创建整数:      int state = 0; 3)将你的状态机翻译成图表如下:(你不需要转到。循环可以作为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;
       }
   }
}

您需要了解while循环,Switch语句,printf()和scanf()。维基百科没问题。

在你这样做之后把代码放在main函数中并做出必要的包含并且你很好。你需要自己完成你的作业。