-1

I want to output every data that I entered in the for loop in a tabular form. Unfortunately, the output only displays the last set of data that I entered. Any solution to solve this?

#include <iostream>

using namespace std;

int main()
{   
    double voltage, rpm, current, range;
    int total; // total set enter by user
    
    cout << "Please enter how many sets of data you want to calculate:";
    cin >> total;
    cout << "\n";
    
    for(int i=1; i<=total; i++)
    {
        cout << "Enter he voltage " << i << ":";
        cin >> voltage;
        cout << "Enter the current " << i << ":";
        cin >> current;
        cout << "Enter the rpm " << i << ":";
        cin >> rpm;
        cout << endl;
        double range = (voltage*1.2) + (current*0.5) + (rpm* 0.2);
    }
    cout << "Voltage\t\tCurrent\t\tRPM\t\tRange\n";                 
    for(int i=1; i<=total; i++)
    {
        cout << voltage << "\t\t" << current << "\t\t" << rpm
             << "\t\t" << range << endl;
    }
    return 0;
}

Anatoly
  • 20,799
  • 3
  • 28
  • 42

2 Answers2

2

This variables

double voltage,rpm,current,range;

each can only store a single double. To store many doubles, use a std::vector<double>:

#include <iostream>
#include <vector>

using std::cout;
using std::cin;

int main()
{   
    std::vector<double> voltages;
    int total; //total set enter by user
    
    cout << "Please enter how many sets of data you want to calculate:";
    cin >> total;
    cout << "\n";
    
    for(int i = 1; i <= total; i++)
    {
            double voltage;
            cout << "Enter he voltage " << i << ":";
            if (cin >> voltage) {
                voltages.push_back(voltage);
            } else {
                cout << "invalid input. Try again \n";
            }
    }
    cout << "Voltage\n";                   
    for (const auto& volt : voltages) {
        cout << volt << "\n";
    }
}

Instead of using many vectors you should use a data structure:

struct my_data {
    double voltage;
    double rpm;
    double current;
    double range;
}

And use a std::vector<my_data>.

Once you have that you should take a look at how to overload the input operator << for that data structure. But one thing at a time ;).

PS Don't use spaces so sparingly. Readability of code cannot be overestimated. And take a look at Why is “using namespace std;” considered bad practice?. Sooner or later it will create you problems.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

You can create a vector of tuple to store the required data.

#include <iostream>
#include <vector>
#include <tuple>

using namespace std;

int main()
{   double voltage,rpm,current,range;
    int total; //total set enter by user
    
    cout << "Please enter how many sets of data you want to calculate:";
    cin>>total;
    cout<<"\n";
    
    std::vector<std::tuple<double, double, double, double>> data;
    
    for(int i=1;i<=total;i++)
        {
            cout<<"Enter he voltage "<<i<<":";
            cin>>voltage;
            cout<<"Enter the current "<<i<<":";
            cin>>current;
            cout<<"Enter the rpm "<<i<<":";
            cin>>rpm;
            cout<<endl;
            double range= (voltage*1.2) + (current*0.5) + (rpm* 0.2);
        
            data.emplace_back(std::make_tuple(current, current, rpm, range));
        }
        
    cout<<"Voltage\t\tCurrent\t\tRPM\t\tRange\n";                   
    for(const auto& tup : data)
        cout<<std::get<0>(tup)<<"\t\t"<<std::get<1>(tup)<<"\t\t"<<std::get<2>(tup)<<"\t\t"<<std::get<3>(tup)<<endl;
return 0;
}
Harry
  • 2,177
  • 1
  • 19
  • 33
  • 2
    imho `tuple` just like `pair` is for when you cannot give names to the type or its members. I'd rather use a custom struct. Though, what I like about this answer is that you made it all the way to what OP was asking for, while mine doesn't – 463035818_is_not_an_ai Nov 27 '20 at 16:37