如何返回这个简单的C ++程序的主菜单 [英] How do I return to the main menu on this simple C++ program

查看:798
本文介绍了如何返回这个简单的C ++程序的主菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道如何在阅读每个子菜单后添加一个选项以返回主菜单。



Just simply want to know how I can add an option to go back to the main menu after reading each sub-menu.

#include <iostream>

using namespace std;


bool processMenu() {
    

	cout << "Main Menu" << endl;
	cout << "What would you like to read about?" << endl;
	cout << "1. Programming Paradigms" << endl;
	cout << "2. Different Programming languages, and their uses" << endl;
	cout << "3. Different C++ Features and their uses" << endl;


	char choice;
	cin >> choice;
	

	if (choice=='1') {
		cout << "A programming paradigm is a fundamental style of computer programming, serving as a way of building the structure and elements of computer programs. Imperative Paradigm: The language provides statements, such as assignment statements , which explicitly change the state of the memory of the computer. Functional: In this paradigm we express computations as the evaluation of mathematical functions. Logic: In this paradigm we express computation in exclusively in terms of mathematical logic. Object-Oriented: In this paradigm we associate behaviour with data-structures called objects which belong to classes which are usually structured into a hierarchy. " << endl;	
	} else if (choice=='2') {
		cout << "Java: Considered the perfect language for developers and programmers to learn. Most utilised for mobile based apps and for creating desktop applications. Python: Considered the simplest language to learn, Python is an extremely useful 'all in one' launguage. Used for anything from web animations to use interfaces. C and C#: C is a general-purpose, high-level language first implemented in 1972. It's easy to learn and handles low level activities well. C# is a highly expressive language, similar in structure to C++, C and even Java. Utilises many features not avilible in the Java language.  " << endl;
	} else if (choice=='3') {
		cout << "C++ can be used to develop new data types called 'classes'. By working with these 'classes', we can develop new libraries. Aswell as this, C++ provides templates and several templates and keywords not found in the C language, giving a wider range of useage. C++ has a huge function library and is a highly flexible language. Used for many, many things, including system software, operating systems, compilers, editors and data bases. " << endl;
	} else {
	    
		return false;
	}
	return true;
}

int main () {


    while (!processMenu()) {


    }

    cout << endl << "Thank you for Reading." << endl;

    return 0;

}

推荐答案

请参阅我对该问题的评论。



这不是一个真正的菜单,这只是解释选项的文本。没有返回这样的概念。最简单的方法是:每次操作后再次输出菜单。首先,用较小的单元,函数和可能的类/结构来破坏你的代码(你正在写关于C ++,所以你打算自己使用C ++吗?:-)); showOptions可能就是其中之一。并一次又一次地调用它。不要进一步复杂化;它需要更高级的UI,使其成为图形用户界面。



但即使这不是最好的事情。大多数仅限控制台的实用程序根本不是交互式的,它们从命令行获取所有输出。这样,命令可以写入批处理文件,再次编辑和使用,等等。在仅限控制台的应用程序中使用交互式UI对用户来说太尴尬和不方便。有一些例外:系统应用程序作为完全成熟的命令解释器工作,但我不确定它是否有任何意义,特别是对你而言。即使是最好的这类应用也会带来无限的混乱;比方说,CMD.EXE经常被误用。老实说,选择纯粹的命令行输入。



最后,你怎么能发送到你的 cout 所以有关编程的许多错误和无能的陈述?差 cout 可能会变红。 :-)



-SA
Please see my comment to the question.

This is not a "real" menu, this is just the text explaining the options. There is no such concept as "return" to it. The simplest approach is: output menu again after each action. First of all, break your code in smaller units, functions and possibly into classes/structs (you are writing about C++, so are you going to use C++ yourself? :-)); "showOptions" could be one of them. And call it again and again. Don't go into further complexity; it you need more advanced UI, make it Graphical UI.

But even this is not the best thing. Most console-only utilities are not interactive at all, they take all output from the command line. This way, the commands can be written in a batch file, edited and used again, and so on. Using interactive UI in a console-only application is way too awkward and inconvenient to the users. There is a small number of exceptions: system applications working as fully-fledged command interpreters, but I'm not sure it can make any sense, especially for you. Even the best of such applications present infinite source of confusion; say, CMD.EXE is often badly misused. Honestly, opt for pure command-line input.

And finally, how can you send to your cout so many false and incompetent statements about programming? Poor cout might get red. :-)

—SA


这篇关于如何返回这个简单的C ++程序的主菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆