如何从main(C+;+;)访问函数中的值 #包括 #包括 使用名称空间std; //常数: const int MIN_HEIGHT=3; const int MIN_WIDTH=3; const int MAX_HEIGHT=20; const int MAX_WIDTH=60; 常量字符H='H'; 常量字符W='W'; 常量字符B='B'; 常量字符F='F'; const char Q='Q'; //原型: void draw矩形(int行、int列、字符边框、字符填充); 无效显示选项(); char getChoice(char h、char w、char b、char f、char q); int main() { 字符边框,填充,选择; 如果你是C++中定义函数的新手,我当然会建议你尝试一些更小的程序。
您定义为函数如何从main(C+;+;)访问函数中的值 #包括 #包括 使用名称空间std; //常数: const int MIN_HEIGHT=3; const int MIN_WIDTH=3; const int MAX_HEIGHT=20; const int MAX_WIDTH=60; 常量字符H='H'; 常量字符W='W'; 常量字符B='B'; 常量字符F='F'; const char Q='Q'; //原型: void draw矩形(int行、int列、字符边框、字符填充); 无效显示选项(); char getChoice(char h、char w、char b、char f、char q); int main() { 字符边框,填充,选择; 如果你是C++中定义函数的新手,我当然会建议你尝试一些更小的程序。,c++,function,C++,Function,您定义为函数getChoice(char,char,char,char,char,char)的返回类型的类型是char,但函数中的所有返回值(neworder除外)的类型都是int,这是不合适的 事实上,从函数“getChoice”的名称判断,该函数应该只返回用户的“choice”,而不是进一步,进一步的指令应该在其他地方执行 当用户输入“q”时,getChoice中的return 0语句不会导致主函数退出,而只会导致getChoice函数返回int值0 在判断用户的选择时,我还考虑了一个比所有
getChoice(char,char,char,char,char,char)
的返回类型的类型是char
,但函数中的所有返回值(neworder除外)的类型都是int
,这是不合适的
事实上,从函数“getChoice”的名称判断,该函数应该只返回用户的“choice”,而不是进一步,进一步的指令应该在其他地方执行
当用户输入“q”时,getChoice中的return 0语句不会导致主函数退出,而只会导致getChoice函数返回int值0
在判断用户的选择时,我还考虑了一个比所有IF语句更好的切换情况语句。
在我根据您的程序编写的以下代码中,将在主函数内执行更多指令
#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;
}
}
#包括
#包括
使用名称空间std;
//常数:
const int MIN_HEIGHT=3;
const int MIN_WIDTH=3;
const int MAX_HEIGHT=20;
const int MAX_WIDTH=60;
常量字符H='H';
常量字符W='W';
常量字符B='B';
常量字符F='F';
const char Q='Q';
//原型:
void draw矩形(int行、int列、字符边框、字符填充);
无效显示选项();
char getChoice(char h、char w、char b、char f、char q);
int main()
{
字符边框='#',填充='*',选项;
int newHeight=0;
int newWidth=0;
字符顺序=(字符)0;
char newFill=(char)0;
内部高度=10,宽度=10;
coutconst char H='H';
就像写const int one_一百_十七=117;
(有点奇怪)听起来你可以从阅读一本书中获益。
#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;
}