我需要帮助弄清楚如何将 getline 放入 presentStringPrompt 以摆脱重复代码
Posted
技术标签:
【中文标题】我需要帮助弄清楚如何将 getline 放入 presentStringPrompt 以摆脱重复代码【英文标题】:I need help figuring out how to put getline into presentStringPrompt to get rid of repetitious code 【发布时间】:2017-06-30 03:01:00 【问题描述】:这个程序要求你输入三个朋友和他们的披萨片的直径。 然后它计算比萨饼的面积并查看谁拥有最大的切片。
我需要一些帮助来尝试将 getline 放入我的字符串函数中以消除代码中的重复。
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
string presentStringPrompt(string);
double presentDoublePrompt(string);
double computeAreaOfCircle(double);
const double pi = 3.14159;
int main()
string first;
string second;
string third;
double diameter;
double radius;
double area1;
double area2;
double area3;
cout << "Enter the name of the first friend: \0";
getline(cin, first);
cout << "Enter " << first << " pizza diameter : \0";
cin >> diameter;
cin.ignore();
radius = diameter / 2;
area1 = pi * radius * radius;
cout << "Enter the name of the second friend: \0";
getline(cin, second);
cout << "Enter " << second << " pizza diameter : \0";
cin >> diameter;
cin.ignore();
radius = diameter / 2;
area2 = pi * radius * radius;
cout << "Enter the name of the third friend: \0";
getline(cin, third);
cout << "Enter " << third << " pizza diameter : \0";
cin >> diameter;
radius = diameter / 2;
area3 = pi * radius * radius;
cout << showpoint << fixed << setprecision(1);
cout << " The area of " << first << "'s pizza is " << area1 << " inches squared\0" << endl;
cout << " The area of " << second << "'s pizza is " << area2 << " inches squared\0" << endl;
cout << " The area of " << third << "'s pizza is " << area3 << " inches squared\0" << endl;
if (area1 >= area2 && area1 >= area3)
cout << first << " has the largest slice of pizza." << endl;
else if (area2 >= area1 && area2 >= area3)
cout << second << " has the largest slice of pizza." << endl;
else
cout << third << " has the largest slice of pizza" << endl;
cin.ignore();
cin.get();
return 0;
string presentStringPrompt(string)
string value;
getline(cin,value);
return value;
cin.ignore();
double presentDoublePrompt(string)
double computeAreaOfCircle(double)
【问题讨论】:
使用 std:array在您之前提取的格式化文本的输入流中留下了一个 '\n'
字符。
这就是为什么随后的第一个 getline()
语句随后失败的原因。
您可以在此处获取有关如何解决此问题的更多信息:
Why does std::getline() skip input after a formatted extraction?
【讨论】:
【参考方案2】:在 main 你必须调用这个函数
int main()
cout << "Enter the name of the first friend: \0";
first = presentStringPrompt(first);//or you can use pass by reference that way you don't have to do first =
cout << "Enter " << first << " pizza diameter : \0";
cin >> diameter;
cin.ignore();
string presentStringPrompt(string value)
getline(cin,value);
return value;
【讨论】:
【参考方案3】:模板怎么样?
template <typename T> question(char * question)
T reply;
cout << question;
cin >> reply;
cin.ignore();
return reply;
用作
double diameter = question("Enter " + first + " pizza diameter :");
几点说明:
如果使用""作为字符串,则不需要终止字符,由编译器添加。
return 后不要添加行,它不会被执行。
【讨论】:
以上是关于我需要帮助弄清楚如何将 getline 放入 presentStringPrompt 以摆脱重复代码的主要内容,如果未能解决你的问题,请参考以下文章