int main()
{
double a=0.0;
double smaller=1/0.0;
double larger=-1/0.0;
std::string unit;
unit= " ";
std::cout<< "Enter a floating point value followed by a unit(cm,m,in,ft)";
while (std::cin>>a>>unit) {
if (unit == "m")
{
std::cout<< a * 100; "cm";
}
elseif (unit == "in")
{
std::cout<< a * 2.54; "cm";
}
elseif (unit == "ft")
{
std::cout<< a * 30.48; "cm";
}
else std::cout<< "Not a unit I know!";
}
}
If the user enters 1m then there should be 100cm, or that`s how I understood it. Now there is only 100. For the original exercise:
Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the value entered. If it`s the smallest so far, write the smallest so far after the number. If it`s the largest so far, write the largest so far after the number.
Add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft, or 3.33m. Accept four units: cm, m, in, ft. Assume conversion factors 1m == 100cm, 1in == 2.54cm, 1ft == 12in. Read the unit indicator into a string. You may consider 12 m (with a space between the number and the unit) equivalent to 12m (without a space).
Reject values without units or with illegal representations of units, such as y, yard, meter, km and gallons.
#include <iostream>
#include <cmath>
#include <cstdio>
int main()
{
double a=0.0;
double smaller=1/0.0;
double larger=-1/0.0;
std::string unit;
unit= " ";
std::cout<< "Enter a floating point value followed by a unit(cm,m,in,ft)";
while (std::cin>>a>>unit) {
if (unit == "m")
{
std::cout<< a * 100 << "cm";
}
elseif (unit == "in")
{
std::cout<< a * 2.54 << "cm";
}
elseif (unit == "ft")
{
std::cout<< a * 30.48 << "cm";
}
else std::cout<< "Not a unit I know!";
if(a>larger) {
larger = a;
std::cout << a << "unit"'\t'"is the largest so far" << std::endl;
}
elseif(a<smaller) {
smaller = a;
std::cout << a << "unit"'\t'"is the smallest so far" << std::endl;
}
elseif(a < larger && a > smaller) {
std::cout << a << "unit" << std::endl;
}
}
}
Getting strange error:
1 2 3 4 5 6 7 8
In function ‘int main()’:
Documents/Program14.cpp:46:35: error: expected ‘;’ before '\x9'
std::cout << a << "unit"'\t'"is the largest so far" << std::endl;
^
Documents/Program14.cpp:52:35: error: expected ‘;’ before '\x9'
std::cout << a << "unit"'\t'"is the smallest so far" << std::endl;
Keep track of the sum of values entered (as well as the smallest and the largest) and the number of values entered. When the loop ends, print the smallest, the largest, the number of values, and the sum of values. Note that to keep the sum, you have to decide on a unit to use for that sum; use meters.
Before connecting the above to my current program, I would write a prototype like this:
double a; // Random number
int b= number of values entered
double i= sum of units;
string sum of units;
sum of units= "Meters";
while (cin>>a) {
for (double a=0; a<i; ++a) // Increment a up to sum which is i
for (int x=0; x<b ; ++x)
// x = value entered (1)
My problem is that I don`t know how to initialize x: How to assign x to number of values entered.