0

This program asks you to input three friends and the diameter of their pizza slice.
It then computes the area of the pizza and sees who has the largest slice.

I need some help trying to put getline into my string function to get rid of the repetition in the code.

#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) {

}

3 Answers3

0

There's a '\n' character left in the input stream from your previous formatted text extractions.

That's why the 1st subsequent getline() statement fails afterwards.

You can get more information about how to fix that here:

Why does std::getline() skip input after a formatted extraction?

0

inside main you have to call this function

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;
}
Manvir
  • 670
  • 1
  • 7
  • 14
0

How about a template ?

template <typename T> question(char * question)
{
    T reply;
    cout << question;
    cin >> reply;
    cin.ignore();
    return reply;
}

Use as

double diameter = question("Enter " + first + " pizza diameter :");

Few notes:

  • if you use "" for string, the terminating char is not needed, it's added by compiler.

  • don't add lines after a return, it won't be executed.

Damien
  • 1,370
  • 8
  • 27