我正在尝试将多个单词存储到单个字符串变量中。我使用getline(cin,stringname)
,但如果我选择另一个formulation
,程序会跳过输入formulation
的新名称,并在下次循环时将其默认为空白。我如何制作它以便下次循环时可以输入新的formulation
名称?
#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;
}