I'm working though Programming: Principles and Practice using C++. And I can't get my program to accept a double then a string. It terminates as soon as I enter in a string.
If I enter any number i.e. 100 it works as expected, outputting the smallest so far and the largest.
If I enter 100m, 100 m, 100, m the program outputs the smallest so far and the largest but I can't get it to output the string unit "m".
I'm teaching myself so any input at all even if it isn't related to this question would be greatly appreciated.
Thank you
I have tried the val1 hitting enter then the string. I've tried https://github.com/bewuethr/stroustrup-ppp/blob/master/chapter04/chapter04_drill.cpp and I can't even get this program to run on netBeans I've added a general string "unit"
#include "std_lib_facilities.h"
int main ()
{
double val1 = 0; // initialized
double val1_conv = 0; //intialized
double smaller = INFINITY; // initialized
double larger = -INFINITY; // initialized
vector<double> compare; // empty vector of doubles
string unit = ""; // Added this to see if it would allow a string, I've also tried removing the = ""
string meter = "m";
string centi = "cm";
string foot = "ft";
string inch = "in";
cout << "Please input a value follow by unit (ie cm,m, in,ft), us | to stop" << endl; // put into
while (cin >> val1 >> unit) // cin "get from"
{
compare.push_back(val1);
if (unit == "m") // Just trying to get the program to accept a string after an double
{
cout << val1 << unit;
}
if (val1 < smaller)
{
smaller = val1; // assignment giving a variable a new value
cout << val1 << " is the smallest so far \n";
compare.push_back(smaller);
}
if (val1 > larger)
{
larger = val1; // assignment giving a variable a new value
cout << val1 << " is the largest so far \n";
compare.push_back(larger);
}
}
return 0; // The book doesn't have us adding this line to everything but I tried it.
}
It acts like as if I didn't add any of the strings or unit to the val
Aucun commentaire:
Enregistrer un commentaire