Name: Anonymous 2010-09-16 20:40
So i wrote this program just as a small starter to get the name of a substance and get its ph level from user. then it tells the user if the substance is either an acid, base, or neutral.
But...
i'm having trouble with the second part at getline(cin, sub2); the whole program just collapses there. at first i thought it was a logic error, but i found none..
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string sub1, sub2, sub3; //declares variables to be used
double ph1, ph2, ph3; //decalares ph levels to be used
cout<< "Please enter the name of the first substance" << endl; //asks for 1st substance
getline(cin, sub1); // gets substance 1 information w/ spaces
cout << "Enter the Ph level of " << sub1 << endl; // enter ph of substanace 1
cin >> ph1; // gets ph level from user
if (ph1 < 0 || ph1 > 14) //declares ph to be only in levels of 0-14
{ cout << sub1 << " is not a vaid PH level\n";
}
else if (ph1 <= 6 && ph1 <= 14) //Acid requirements
{ cout << sub1 << " is an Acid\n";
}
else if (ph1 >= 8 && ph1 <= 14) //base requirements
{ cout << sub1 << " is a Base\n";
}
else if (ph1 == 7 && ph1 <= 14) //neutral requirements
{ cout << sub1 << " is neutral\n";
}
cout<< "Please enter the name of the second substance" << endl; //asks for second substance name
getline(cin, sub2); //gets second subs name w/ spaces
cout << "Enter the Ph level of " << sub2 << endl; //asks for ph of sub2
cin >> ph2; //get ph of level from user
if (ph2 < 0 || ph2 > 14) //declares ph to be only in levels of 0-14
{ cout << sub2 << " is not a vaid PH level\n";
}
else if (ph2 <= 6 && ph2 <= 14) //Acid requirements
{ cout << sub2 << " is an Acid\n";
}
else if (ph2 >= 8 && ph2 <= 14) //base requirements
{ cout << sub2 << " is a Base\n";
}
else if (ph2 == 7 && ph2 <= 14) //neutral requirements
{ cout << sub2 << " is neutral\n";
}
cout<< "Please enter the name of the third substance" << endl; //asks for third substance name
getline(cin, sub3); //gets third subs name w/ spaces
cout << "Enter the Ph level of " << sub3 << endl; //asks for ph of sub3
cin >> ph3; //get ph of level from user
if (ph2 < 0 || ph2 > 14) //declares ph to be only in levels of 0-14
{ cout << sub2 << " is not a vaid PH level\n";
}
else if (ph2 <= 6 && ph2 <= 14) //Acid requirements
{ cout << sub2 << " is an Acid\n";
}
else if (ph2 >= 8 && ph2 <= 14) //base requirements
{ cout << sub2 << " is a Base\n";
}
else if (ph2 == 7 && ph2 <= 14) //neutral requirements
{ cout << sub2 << " is neutral\n";
}
return 0;
}
But...
i'm having trouble with the second part at getline(cin, sub2); the whole program just collapses there. at first i thought it was a logic error, but i found none..
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string sub1, sub2, sub3; //declares variables to be used
double ph1, ph2, ph3; //decalares ph levels to be used
cout<< "Please enter the name of the first substance" << endl; //asks for 1st substance
getline(cin, sub1); // gets substance 1 information w/ spaces
cout << "Enter the Ph level of " << sub1 << endl; // enter ph of substanace 1
cin >> ph1; // gets ph level from user
if (ph1 < 0 || ph1 > 14) //declares ph to be only in levels of 0-14
{ cout << sub1 << " is not a vaid PH level\n";
}
else if (ph1 <= 6 && ph1 <= 14) //Acid requirements
{ cout << sub1 << " is an Acid\n";
}
else if (ph1 >= 8 && ph1 <= 14) //base requirements
{ cout << sub1 << " is a Base\n";
}
else if (ph1 == 7 && ph1 <= 14) //neutral requirements
{ cout << sub1 << " is neutral\n";
}
cout<< "Please enter the name of the second substance" << endl; //asks for second substance name
getline(cin, sub2); //gets second subs name w/ spaces
cout << "Enter the Ph level of " << sub2 << endl; //asks for ph of sub2
cin >> ph2; //get ph of level from user
if (ph2 < 0 || ph2 > 14) //declares ph to be only in levels of 0-14
{ cout << sub2 << " is not a vaid PH level\n";
}
else if (ph2 <= 6 && ph2 <= 14) //Acid requirements
{ cout << sub2 << " is an Acid\n";
}
else if (ph2 >= 8 && ph2 <= 14) //base requirements
{ cout << sub2 << " is a Base\n";
}
else if (ph2 == 7 && ph2 <= 14) //neutral requirements
{ cout << sub2 << " is neutral\n";
}
cout<< "Please enter the name of the third substance" << endl; //asks for third substance name
getline(cin, sub3); //gets third subs name w/ spaces
cout << "Enter the Ph level of " << sub3 << endl; //asks for ph of sub3
cin >> ph3; //get ph of level from user
if (ph2 < 0 || ph2 > 14) //declares ph to be only in levels of 0-14
{ cout << sub2 << " is not a vaid PH level\n";
}
else if (ph2 <= 6 && ph2 <= 14) //Acid requirements
{ cout << sub2 << " is an Acid\n";
}
else if (ph2 >= 8 && ph2 <= 14) //base requirements
{ cout << sub2 << " is a Base\n";
}
else if (ph2 == 7 && ph2 <= 14) //neutral requirements
{ cout << sub2 << " is neutral\n";
}
return 0;
}