0

I am trying to store multiple words into a single string variable. I used getline(cin,stringname) but if I choose to do another formulation, the program skips over inputting a new name for the formulation and defaults it as blank the next time around the loop. How do I make it so that I can input a new formulation name the next time around the loop?

#include <iostream>
#include <string>
using namespace std;

//Declaring Variables
string formulation;

int main()
{
    int counter = 1, treatment;
    double hardness, max=-78688, max2 = -78687;
    char answer;

    //Input

    do{

    cout << "What is the name of the formulation?\n";
    getline(cin,formulation);
    cout << "How many treatments will the formulation " << formulation << " use?\n";
    cin >> treatment;


        while (treatment < 3)
        {
            cout << "Each formulation must use atleast three treatments. Please enter a valid value." << endl;
            cin >> treatment;
        }

        for (treatment; counter <= treatment; counter++ )
        {

            cout << "Please enter the hardness for treatment " << counter << " of formulation " << formulation <<
            ": ";
            cin >> hardness;
            while ((hardness <= 0) || (hardness > 11))
            {
                cout << "The value you have entered is invalid. The hardness must be greater than zero and less than or equal to 11. Please enter a new value" << endl;
                cin >> hardness;
             }

            if (hardness > max)
            {
                 max2 = max;
                 max = hardness;
            }
            else if (hardness > max2)
            {
                max2 = hardness;
            }


         }

         //output
         cout << "The highest hardness for " << formulation <<" is " << max << " mohs." << endl<< "The second highest hardness for " << formulation << " is " << max2 << " mohs." << endl;

        cout << "Are there any more formulations? [y/n]" << endl;
        cin >> answer;

    } while ((((answer == 'y') || answer == 'Y')) && (counter = 1)&& (max=-78688) && (max2=-78687));

    return 0;
}
Noah
  • 1
  • 1

0 Answers0