如何从主(C )访问函数中的值
How to access values within functions from the main (C++)
#include <iostream>
#include <cassert>
using namespace std;
//constants:
const int MIN_HEIGHT = 3;
const int MIN_WIDTH = 3;
const int MAX_HEIGHT = 20;
const int MAX_WIDTH = 60;
const char H = 'h';
const char W = 'w';
const char B = 'b';
const char F = 'f';
const char Q = 'q';
//prototypes:
void drawRectangle(int row, int col, char border, char fill);
void displayChoices();
char getChoice(char h, char w, char b, char f, char q);
int main()
{
char border, fill, choice;
cout << endl << "Welcome! n";
drawRectangle (10,10, '#','*');
cout << endl << "Choose from the following: n";
displayChoices();
getChoice(H, W, B, F, Q);
//drawRectangle();//not sure how to get the changed value from getChoice
//if height is changed, update height. If width is changed, update width of rect.
//if border is changed, update rectangle border. If fill is changed, update rectangle fill
}
//draws rectangle
void drawRectangle(int row, int col, char border, char fill)
{
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if ((i == 0) || (i == (row - 1)) || (j == 0) || (j == (col - 1))) {
cout << border;
}
else {
cout << fill;
}
}
cout << endl;
}
}
//diplays users choices between height, width
//border and quit
void displayChoices()
{
cout << "h) Change the height.n";
cout << "w) Change the width.n";
cout << "b) Change the border character.n";
cout << "f) Change the fill character.n";
cout << "q) Quit program.n";
}
//takes users choice and asks for the new value or character
char getChoice(char h, char w, char b, char f, char q) {
char choice, newBorder, newFill;
int newHeight, newWidth, count = 0;
cin >> choice;
while ((choice != h && choice != w && choice != b && choice != f && choice != q)) {
cout << "Not a valid choice. Choose again.n";
cin >> choice;
}
if (choice == q)
return 0;
else if (choice == h) {
cout << "Enter new height between " << MIN_HEIGHT << " and " << MAX_HEIGHT << ": n";
cin >> newHeight;
while ((newHeight < MIN_HEIGHT) || (newHeight > MAX_HEIGHT)) {
cout << "That number is not in range. Try again.n";
cout << "Enter a number between " << MIN_HEIGHT
<< " and " << MAX_HEIGHT << ": ";
cin >> newHeight;
} return newHeight;
}
else if (choice == w) {
cout << "Enter new width between " << MIN_WIDTH << " and " << MAX_WIDTH << ": n";
cin >> newWidth;
while ((newWidth < MIN_WIDTH) || (newWidth > MAX_WIDTH)) {
cout << "That number is not in range. Try again.n";
cout << "Enter a number between " << MIN_WIDTH
<< " and " << MAX_WIDTH << ": ";
cin >> newWidth;
} return newWidth;
}
else if (choice == b) {
cout << "Enter new border character: n";
cin >> newBorder;
return newBorder;
}
else if (choice == f) {
cout << "Enter new fill character: n";
cin >> newFill;
return newFill;
}
}
我正在尝试创建一个应用程序来绘制一个矩形,其中用户可以更改矩形的高度,宽度,边框和填充(一次)。我很难理解如何访问/保存用户的输入(更改为矩形)并绘制具有更新值的新矩形。我刚刚学习C ,因此我的代码甚至可能不是解决此问题的正确方法。任何帮助或指导将不胜感激。
我肯定会首先建议您尝试一些较小的程序,如果您是在C 中定义功能的新程序。
您定义为函数getChoice(char, char, char, char, char)
的返回类型的类型是char
,但是函数中的所有返回值(NewBorder除外)均为类型int
,它是不合适的。
实际上,从函数" getChoice"的名称来看,该功能只能返回用户的"选择"而不是进一步发展,并且应在其他地方进行进一步的说明。
当用户输入'q'时,getChoice中的返回0语句不会导致主函数退出,而仅导致getChoice函数返回int值0。
我还考虑了一个判断用户选择时的所有if-else语句。
在我根据您的程序编写的以下代码中,在主函数内执行了更多说明。
#include <iostream>
#include <cassert>
using namespace std;
//constants:
const int MIN_HEIGHT = 3;
const int MIN_WIDTH = 3;
const int MAX_HEIGHT = 20;
const int MAX_WIDTH = 60;
const char H = 'h';
const char W = 'w';
const char B = 'b';
const char F = 'f';
const char Q = 'q';
//prototypes:
void drawRectangle(int row, int col, char border, char fill);
void displayChoices();
char getChoice(char h, char w, char b, char f, char q);
int main()
{
char border = '#', fill = '*', choice;
int newHeight = 0;
int newWidth = 0;
char newBorder = (char)0;
char newFill = (char)0;
int height = 10, width = 10;
cout << endl << "Welcome! n";
while (true)
{
drawRectangle(height, width, border, fill);
cout << endl << "Choose from the following: n";
displayChoices();
choice = getChoice(H, W, B, F, Q);
//if height is changed, update height. If width is changed, update width of rect.
//if border is changed, update rectangle border. If fill is changed, update rectangle fill
switch (choice)
{
case Q:
return 0;
case H:
cout << "Enter new height between " << MIN_HEIGHT << " and " << MAX_HEIGHT << ": n";
cin >> newHeight;
while ((newHeight < MIN_HEIGHT) || (newHeight > MAX_HEIGHT)) {
cout << "That number is not in range. Try again.n";
cout << "Enter a number between " << MIN_HEIGHT
<< " and " << MAX_HEIGHT << ": ";
cin >> newHeight;
}
height = newHeight;
break;
case W:
cout << "Enter new width between " << MIN_WIDTH << " and " << MAX_WIDTH << ": n";
cin >> newWidth;
while ((newWidth < MIN_WIDTH) || (newWidth > MAX_WIDTH)) {
cout << "That number is not in range. Try again.n";
cout << "Enter a number between " << MIN_WIDTH
<< " and " << MAX_WIDTH << ": ";
cin >> newWidth;
}
width = newWidth;
break;
case B:
cout << "Enter new border character: n";
cin >> newBorder;
border = newBorder;
break;
case F:
cout << "Enter new fill character: n";
cin >> newFill;
fill = newFill;
break;
}
}
return 0;
}
//draws rectangle
void drawRectangle(int row, int col, char border, char fill)
{
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if ((i == 0) || (i == (row - 1)) || (j == 0) || (j == (col - 1))) {
cout << border;
}
else {
cout << fill;
}
}
cout << endl;
}
}
//diplays users choices between height, width
//border and quit
void displayChoices()
{
cout << "h) Change the height.n";
cout << "w) Change the width.n";
cout << "b) Change the border character.n";
cout << "f) Change the fill character.n";
cout << "q) Quit program.n";
}
//takes users choice and asks for the new value or character
char getChoice(char h, char w, char b, char f, char q) {
char choice, newBorder, newFill;
int newHeight, newWidth, count = 0;
cin >> choice;
while ((choice != h && choice != w && choice != b && choice != f && choice != q)) {
cout << "Not a valid choice. Choose again.n";
cin >> choice;
}
return choice;
}
使用"使用名称空间std"实际上也是一种不好的做法;声明,但这可能会减少初学者的很多麻烦,因此您现在不需要关心这一点。请记住,您应该在一段时间后摆脱它。
相关文章:
- 使用不带参数的函数访问结构元素
- 删除目录函数访问被拒绝
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 如何使用 C++ 中的继承函数访问派生类中的局部变量
- 我想要一个具有子函数的函数访问相同的命名函数,而不使用它取决于其子类的类
- 有没有办法通过 main 函数访问受保护的矢量大小而无需将其转换为公共?
- 如何在C++通过公共函数访问私有函数成员?
- 限制variadic模板类中的构造函数访问
- 从析构函数访问模板类构造函数的参数,可以吗?
- 变量 z 无法从其他函数访问变量 x
- 从静态成员函数访问私有非静态类变量 - C++
- 如何在 C++ 中从 void 返回函数访问变量
- 在类中使用映射并通过其他类的 get() 和 set() 函数访问值
- 无法从另一个函数访问文件范围变量的内容
- 是否可以从 C++ 中的构造函数访问对象名称?
- 使用C 中的共享_ptr从公共静态成员函数访问私有构造函数
- 使用静态成员函数访问静态数据成员
- 为什么我们可以从 C++ 中的成员函数访问 non_member 函数
- 如何使用 c++ 中的函数访问全局变量?
- 如何从 C++ 中的主函数访问类中的私有构造函数?