此程序要求您输入三个朋友和他们的披萨片的直径 然后它计算比萨饼的面积,看看谁拥有最大的切片。
我需要一些帮助,试图将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) {
}
答案 0 :(得分:0)
您之前格式化文本提取的输入流中还剩$ ./bin/cbd
C/c : Encode
D/d : Decode
Q/q : Quit
> c
? abcd
Binary rep of a= 01100001
(A-65)(S-83)(X-88)(J-74)(D-68)(D-68)(Y-89)(H-72)
Binary rep of b= 01100010
(C-67)(O-79)(F-70)(H-72)(B-66)(I-73)(Z-90)(D-68)
Binary rep of c= 01100011
(S-83)(U-85)(V-86)(X-88)(P-80)(Q-81)(G-71)(H-72)
Binary rep of d= 01100100
(G-71)(K-75)(F-70)(H-72)(U-85)(J-74)(T-84)(Z-90)
> d
? 1100001
a [97]
> d
? 1100011
c [99]
> foo
error: invalid choice.
> Q
个字符。
这就是为什么随后的第一个'\n'
语句失败的原因。
您可以在此处获取有关如何解决此问题的更多信息:
Why does std::getline() skip input after a formatted extraction?
答案 1 :(得分:0)
在主内部你必须调用这个功能
create or replace function notifications_check_user_id_trigger()
returns trigger as $$
begin
if new.notified_user_id <>
(select answerer_id from answers where id = new.answer_id)
then
raise exception e'answer_id (%) and notified_user_id (%) don\'t match',
new.answer_id,
new.notified_user_id;
end if;
end;
$$ language plpgsql;
create trigger t_50_notifications_check_user_id_trigger
before insert or update
on notifications
for each row execute procedure notifications_check_user_id_trigger()
;
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();
}
答案 2 :(得分:0)
模板怎么样?
template <typename T> question(char * question)
{
T reply;
cout << question;
cin >> reply;
cin.ignore();
return reply;
}
用作
double diameter = question("Enter " + first + " pizza diameter :");
很少注意到:
如果对字符串使用“”,则不需要终止字符,它由编译器添加。
返回后不添加行,不会执行。