菜单结构和循环

Menu Structure and Loops

本文关键字:循环 结构 菜单      更新时间:2023-10-16

我有一个我正在创建的小程序。用Lamens的话来说,我想制作一个菜单,用户可以输入他的选择,将他带到预定的区域,完成后,他将他带回菜单,在那里他可以做出不同的选择。当您按1时,我已经降低了它显示的位置(您将看到我对代码的意思)。但是,在经历了应该做的事情之后,它不会返回菜单,它继续延续到第二个选项。另请说您在菜单上,您想从第二个选项开始,它只是从第一个开始。任何人都可以帮我...

#include <iostream>
using namespace std;
int main()
{
int option;
    cout << "Hello there...nnToday we are going to do a little fun project that I created.nn";
    cin.get();
    cout << "nAs we progress throughout this program, I will explain the different thingsn";
    cout << "that we are going to do. We will be covering some basic C++ excersises.";
    cout << "nnFirst and foremost we will  begin by  covering what I have learned so far....nn";
    cin.get();
    cout << "The first topic we will cover will include what is known as 'variables'.n";
    cout << "what are variables you ask?";
    cin.get();

    int var = 1;
    int main=1;
        do
        {
            cout << "nnnEnter the number of one of the following and I will explain!n";
            cout << "1.integer  2.boolian   3.floats   4.doubles   5.character";
            cout << "nn[when you are done type 'done' to continue]nn";
            cin >> option; //this is where the user puts in his option (1,2,3,4,5,6) I have only 1 and 2 complete
            if (option = 1) //starts with this regardless of input
            {
                cin.ignore();
                cout << "nnnInteger is the variable abbreviated as 'int' this allows C++ to only";
                cout << "nread whole and real numbers. C++ takes this number as stores itn";
                cout << "in a user-defined variable (you can make up what that variable is..)n";
                cin.get();
                cout << "an example syntax would be:";
                cout << "nnint var=[whole;real number]";
                cin.get();
                cout << "nnwhat this implies is that you have 'declared' to the system";
                cout << "nthat you are going to use an 'int' that you have named 'var'n";
                cout << "and in this 'var' you are going to store a real and whole numberninside of it.";
                cout << "nnPress any key to go back to menu";
                cin.get();
            }
            else if (option = 2); //continues with this without going back to menu.
            {
                cin.ignore();
                cout << "nnnBoolian is the variable abbreviated as 'bool' this allows C++ to only";
                cout << "nread true or false ( 0 or 1 ). C++ takes this number as stores itn";
                cout << "in a user-defined variable (you can make up what that variable is..)n";
                cin.get();
                cout << "an example syntax would be:";
                cout << "nnbool var=[true or false]";
                cin.get();
                cout << "nnwhat this implies is that you have 'declared' to the system";
                cout << "nthat you are going to use an 'bool' variable that you have named 'var'n";
                cout << "and in this 'var' you are going to store a either a true or falseninside of it.";
                cout << "nnPress any key to go back to menu";
                cin.get();
            }
        } while (var = 1);

}

问题在于比较操作员

1)if (option = 1),使用if(option == 1)

2)else if (option = 2);,使用else if(option == 2),删除分号

3)while(var = 1) ;,使用while(var ==1 );

C 中的比较是用==操作器进行的:

例如。

 if (option == 1) 
 if (option == 2) 
 while (var == 1);