User Input Issue

Hi,

I was wondering if someone could take a look at my code and advise me how I get these functions to loop until user enters a float value:

void celsius_input (float &celsius_value);//prototype to get user to input temperature in Celsius
void fahrenheit_input (float &fahrenheit_value);//prototype to get user to input temperature in Fahrenheit

[Sorry I posted wrong functions in original post]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//Author:       
//Date: 	04/10/2013
//Title:        Temperature Scale Convertor
//Description:  User selects the conversion from a menu then inputs value to be converted, converted value is outputted

#include <iostream>
#include <cstdlib>//header to allow program to use clear screen command

using namespace std;

int menu (int &selection);//prototype for menu function

void celsius_input (float &celsius_value);//prototype to get user to input temperature in Celsius
void fahrenheit_input (float &fahrenheit_value);//prototype to get user to input temperature in Fahrenheit

float convert_to_fahrenheit (float celsius_value, float celsius_converted);//prototype for converting from Celsius to Fahrenheit
float convert_to_celsius (float fahrenheit_value, float fahrenheit_converted);//prototype for converting from Fahrenheit to Celsius

void celsius_output (float fahrenheit_value, float fahrenheit_converted);//prototype that outputs converted Fahrenheit temperature in Celsius
void fahrenheit_output (float celsius_value, float celsius_converted);//prototype that outputs converted Celsius temperature in Fahrenheit

int main ()
{
 	float celsius_value;//declaring variable of type float to hold temperature in Celsius
	float fahrenheit_value;//declaring variable of type float to hold temperature in Fahrenheit
	float celsius_converted;//declaring variable of type float to hold value of temperature in Fahreheit after the Celsius Temperature has been converted
	float fahrenheit_converted;//declaring variable of type float to hold value of temperature in Celsius after the Fahrenheit Temperature has been converted
	int selection = 0;//initialising variable of type int to hold menu choice inputted by user. Value of 0 assigned to ensure menu in menu function is displayed at least once

 	menu (selection);//calling function to display menu and get user's selection
    //cout << "\nSelection value is: " << selection;//used in unit testing menu function - commented out once unit testing completed

	//introducing 2nd unit to be tested - switch statement
	//unit testing conducted 05/10/13
	//used cout statements to verify that that the correct case was selected
	//test data used = 1, 2 & 3
	//compiled with no syntax errors
	//unit testing completed 05/10/13 - programme working as intended, i.e. correct switch case being selected using value passed out of menu function
	//uses value from selection to select the correct switch case
	switch(selection)
	{
		case 1 : //cout << "Option 1 chosen from menu";//used in unit testing switch statement - commented out once unit testing completed
				 celsius_input (celsius_value);//calling celsius_imput function to get value from user
                 //cout << "\nCelsius Temperature is: " << celsius_value;//used in unit testing celsius_input function - commented out once unit testing completed
                 fahrenheit_output (celsius_value, celsius_converted);//calling fahrenheit_output function to output converted Celsius temperature in Fahrenheit
				 break;//to stop fall through
		case 2 : //cout << "Option 2 chosen from menu";//used in unit testing switch statement - commented out once unit testing completed
                 fahrenheit_input (fahrenheit_value);//calling fahrenheit_imput function to get value from user
                 //cout << "\nFahrenheit Temperature is: " << fahrenheit_value;//used in unit testing fahrenheit_input function - commented out once unit testing completed
                 celsius_output (fahrenheit_value, fahrenheit_converted);//calling fahrenheit_output function to output converted Celsius temperature in Fahrenheit
				 break;//to stop fall through
		case 3 : system("CLS");//clears screen
				 cout << "Exit chosen from menu. Programme terminating." << endl;
				 //cout << "Option 3 chosen from menu";//used in unit testing switch statement - commented out once unit testing completed
				 break;//to stop fall through
		//default can't be reached due to restrictions on selection variable
		default : cout << "\n Invalid selection";
		//no break required in the default case
	}

 	int x;
	cin >> x;
	return 0;
}

int menu (int &selection)
{
	while (selection < 1 || selection >3)//while loop used to keep displaying menu until user enters a valid menu choice
	{
		system("CLS");//clears screen
		//displays menu to screen
		cout << "Menu" << endl;
		cout << "=======" << endl;
		cout << "(1)\tConvert Celsius to Fahrenheit" << endl;
		cout << "(2)\tConvert Fahrenheit to Celsius" << endl;
		cout << "(3)\tExit" << endl;
		cout << "\nEnter choice from menu: ";//user prompted to select from menu
		//read input from user
		cin >> selection;//selection variable initialised using value inputted by user
		cin.clear();//clears error state of stream
       	cin.ignore(100, '\n');//discard characters remaining in the input buffer
	}
}

void celsius_input (float &celsius_value)
{
	system("CLS");//clears screen
	cout << "Please enter your temperature in Celsius: ";//prompt user to input temperature value in Celsius
	cin >> celsius_value;//celsius_value variable initialised using user's input
}

float convert_to_fahrenheit (float celsius_value, float celsius_converted)
{
	celsius_converted = ((9*celsius_value)/5) + 32;
	return celsius_converted;
}

void fahrenheit_output (float celsius_value, float celsius_converted)
{
    cout << "\nTemperature in Fahrenheit is: " << convert_to_fahrenheit (celsius_value, celsius_converted) << endl;
}

void fahrenheit_input (float &fahrenheit_value)
{
	system("CLS");//clears screen
	cout << "Please enter your temperature in Fahrenheit: ";//prompt user to input temperature value in Fahrenheit
	cin >> fahrenheit_value;//fahrenheit_value variable initialised using user's input
}

float convert_to_celsius (float fahrenheit_value, float fahrenheit_converted)
{
	fahrenheit_converted = (((fahrenheit_value - 32)/9) * 5);
	return fahrenheit_converted;
}

void celsius_output (float fahrenheit_value, float fahrenheit_converted)
{
    cout << "\nTemperature in Celsius is: " << convert_to_celsius (fahrenheit_value, fahrenheit_converted) << endl;
}
Last edited on
Hi there,

Assuming that integer values are allowed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <sstream>
#include <string>

std::string tmp;
float target_float;

std::cout << "Input a float please: ";
std::cin >> tmp;

std::istringstream iss(tmp);

if ( iss >> target_float )
    //user entered a float
else
   //user didn't enter a float 


You could wrap this into its own function, of course.

All the best,
NwN
Last edited on
Cheers Nwn, I will see if I can adapt that to what I am trying to do and let you know how I got on.
This is a goddamn mess.
First, menu needs to be void as it is returning no value.

You should not make arguments when calling a function that have not yet been prescribed.
immediately after entering the value, you want to call a function with the argument "celsius_converted," yet what the hell is celsius converted?
and why not just say ((9/5)*x)+32 --- like the actual formula suggests.

this is by far the most clustered and complicated way to do something so simple I have ever seen

and by looping until the user enters a float??? do you mean deny non-numeric input??
so the input inquiry will loop if they enter characters that aren't numbers?


if so... then do this :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool   input = false;    //make a bool statement to allow while loop
    float celsius_value;

    while (!input1)
    {
        cout << "Enter degrees in celsius: ";
        string line;
        getline(cin, line);
        istringstream is(line);
        char dummy = '\0';
        if (!(is >> celsius_value) || (is >> ws && is.get(dummy)))
            cout << "Invalid input. Try again!\n";
        else
            input1 = true ;
    }


This will allow only for numerical inputs and no spaces within the entry, unless before an actual character.


EDIT: and include <sstream> and <string>
also, <cstdlib> is not necessary for system CLS
Last edited on
xplainet wrote:
This is a goddamn mess.
First, menu needs to be void as it is returning no value.
menu should either have a void return type or actually return a value. I would be inclined to make most of the functions that take references return a value rather than take a parameter by reference.


xplainet wrote:
You should not make arguments when calling a function that have not yet been prescribed.

Perhaps you could explain what you mean here. Prescribed makes no sense in this context. If you mean 'initialized' I could see the sense in it, however:
xplainet wrote:
immediately after entering the value, you want to call a function with the argument "celsius_converted," yet what the hell is celsius converted?
celsius_converted is obviously meant to be passed by reference given the style of the other functions and the content of celsius_converted. Equally obvious is that it needn't be a parameter to the function at all the way the function is used.


xplainet wrote:
also, <cstdlib> is not necessary for system CLS

Yes, it is.
Last edited on
I'd like to post a basic valid input function.
It's useful, because it will not screw up input if it will be pipelined from file/another program (assuming that it does not have errors) or if you decide to write all values for a program from the beginning:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <limits>
#include <string>

template <typename T>
void validated_input(T& value, std::string message = "invalid value\n")
{
    while(!(std::cin >> value)) {
        std::cout << message;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}
Topic archived. No new replies allowed.