Home > Software engineering >  How to call a function from another function using STL list in C ?
How to call a function from another function using STL list in C ?

Time:02-18

I'm new to data structures in C , I want to write a code using STL list to display the following:

Input for data radius:
Radius 1: 20
Press [Y] for next input: Y
Radius 2: 12
Press [Y] for next input: N

List of Existing Records:
ID:1, Radius: 20, Volume: 33,514.67
ID:2, Radius: 12, Volume: 7,239.17
Total record: 2

I wrote the code like this, but I'm no so sure on how exactly to include the volume value dataVolume() so that I can get the desired output:

#include <iostream>
#include <list>

using namespace std;

struct sphere {
    int recordID;
    double radius, volume;
};

double dataVolume(sphere* values) {
    double v = (4 * 3.14 * (values->radius) * (values->radius) * (values->radius)) / 3.0;
    return v;
}

void dataRadius(sphere* values) {
    int i = 0;
    char choice;
    do {
        cout << "Radius " <<i 1 <<": ";
        cin >> values->radius;
       // values->volume = dataVolume();
        cout << "Press [Y] for next input: ";
        cin >> choice;
        i  ;
    } while (choice == 'Y');

}

void displayData(list<sphere>Record) {
    cout << "List of Existing Records:" << endl;
    list<int>::iterator i;
    int count = 0;
    for (auto i = Record.begin(); i != Record.end(); i  ) {
        cout << "ID: " << count   1 << ", Radius: " << i->radius <<
            ", Volume: " << i->volume << endl;
        count = count   1;
    }
    cout << "Total record: " << count << endl;
}

int main() {

    list<sphere>Record;
    sphere values;
    dataRadius(&values);    
    displayData(Record);
    return 0;
}

CodePudding user response:

double dataVolume(double radius) {
    double v = (4 * 3.14 * radius * radius * radius) / 3.0;
    return v;
}

void dataRadius(sphere* values) {
    int i = 0;
    char choice;
    do {
        cout << "Radius " <<i 1 <<": ";
        cin >> values->radius;
        values->volume = dataVolume(values->radius);
        cout << "Press [Y] for next input: ";
        cin >> choice;
        i  ;
    } while (choice == 'Y');
}
  • Related