-
I study your example and I have to say that is NOT good to combine the cin.ignore() method with the method of conversion. There is a problem in input stream and it's difficult to discover it. Anyway here is my source:
Do you think that I can avoid the cin.clear and cin.ignore parts ?
Code:
//
// File: temp.cc
// Author: blackslash13
//
// Created on 29 Νοέμβριος 2007, 11:06 μμ
//
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <limits>
bool inputDouble(std::string prompt, double &val)
{
std::string input;
for(;;)
{
std::stringstream sstr;
std::cout << prompt;
if (!std::getline(std::cin, input))
return false; // Failed to read input.
sstr << input;
if (sstr >> val) {
return true;
}
else
{
std::cout << "Error, not a valid number" << std::endl;
}
}
}
double fahrenheitToCelsius(double fahrenheit)
{
double celsius;
celsius = (fahrenheit-32)/1.8;
return celsius;
}
double celsiusToFahrenheit(double celsius)
{
double fahrenheit;
fahrenheit = 1.8 * celsius + 32 ;
return fahrenheit;
}
void makeTable(double start, double end, double step, double choice)
{
using namespace std;
cout.setf(ios::fixed);
double i(0),fahr,celsius;
if(choice==1)
{
for(i=start; i<=end; i+=step)
{
fahr=celsiusToFahrenheit(i);
cout << setprecision(2) << setw(5) << i << "\t\t" << fahr << endl;
}
}
else
{
for(i=start; i<=end; i+=step)
{
celsius=fahrenheitToCelsius(i);
cout << setprecision(2) << setw(5) << i << "\t\t" << celsius << endl;
}
}
}
//
//
//
int main(int argc, char** argv) {
{
using namespace std;
double choice,choice2;
double degree,lowBound,maxBound,step;
cout << "Select a number" << endl
<< "1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius" << endl
<< "Your Choice:";
cin >> choice;
while(!cin.good() || choice<1 || choice>2)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Not valid selection.... Your Choice:";
cin >> choice;
}
if(choice == 1)
{
cout << "Select a number" << endl
<< "1. Specific conversion\n2. Print table list" << endl
<< "Your Choice:";
cin >> choice2;
while(!cin.good() || choice2<1 || choice2>2)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Not valid selection.... Your Choice:";
cin >> choice2;
}
/* Thrash the garbage from input strem */
cin.clear();
cin.ignore(1,'\n');
if(choice2==1)
{
while(!inputDouble("Enter Celsius temperature degree:", degree) || degree<=-316 || degree>=316)
{
cout << "Not valid selection....";
}
cout << degree << " Celsius = " << celsiusToFahrenheit(degree) << " Fahrenheit." << endl;
}
else
{
while(!inputDouble("Enter Celsius lower bound degree:",lowBound) || lowBound<=-316 || lowBound>=316)
{
cout << "Not valid selection....";
}
while(!inputDouble("Enter Celsius maximum bound degree:",maxBound) || maxBound<=-316 || maxBound>=316)
{
cout << "Not valid selection....";
}
while(!inputDouble("Enter increment value for the list:",step) || step<=0)
{
cout << "Not valid selection....";
}
cout << "Celsius\t\tFahrenheit" << endl;
makeTable(lowBound, maxBound, step,1);
}
}
else
{
cout << "Select a number" << endl
<< "1. Specific conversion\n2. Print table list" << endl
<< "Your Choice:";
cin >> choice2;
while(!cin.good() || choice2<1 || choice2>2)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Not valid selection.... Your Choice:";
cin >> choice2;
}
/* Thrash the garbage from input strem */
cin.clear();
cin.ignore(1,'\n');
if(choice2==1)
{
while(!inputDouble("Enter Fahrenheit temperature degree:", degree) || degree<=-316 || degree>=316)
{
cout << "Not valid selection....";
}
cout << degree << " Fahrenheit = " << fahrenheitToCelsius(degree) << " Celsius." << endl;
}
else
{
while(!inputDouble("Enter Fahrenheit lower bound degree:",lowBound) || lowBound<=-316 || lowBound>=316)
{
cout << "Not valid selection....";
}
while(!inputDouble("Enter Fahrenheit maximum bound degree:",maxBound) || maxBound<=-316 || maxBound>=316)
{
cout << "Not valid selection....";
}
while(!inputDouble("Enter increment value for the list:",step) || step<=0)
{
cout << "Not valid selection....";
}
cout << "Fahrenheit\tCelsius" << endl;
makeTable(lowBound, maxBound, step,2);
}
}
return 0;
}
return (EXIT_SUCCESS);
}
-
Sure, just use inputInt for your menu choices [or something similar] - you can do this:
Code:
bool inputChoice(std::string str, int &choice, int min, int max) {
while(inputInt(str, choice))
{
if (choice >= min && choice <= max) {
return true;
cout << "Not valid selection.... Your Choice:";
}
return false;
}
inputChoice("Select a number\n"
"1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius\n"
"Your Choice:",
choice, 1, 2);
I just hacked the above up right now, so you may need to do a little bit of debugging and "beautification" of it, but as a principle it should work OK. No cin.ignore() or cin.clear() there...
--
Mats
-
Thanks matsp, you rock dude!!!!
:)