float functions?

I made a program that acts as a calculater, outputs like this...

+ 5
result is now 5
- 2
result is now 3... and so on

so i have the functions for each operation ( +, -, *, /, ^, q(quit)), once im in the function (after they choose there operation), i need to test and make sure the value they enter is a float value and not a character or symbol, because if its a character (or a symbol) i jus get a runtime error, so i was wondering if there were any functions to test if its a float value or like what could I do in that situation.

here is one of my functions and i would like test the value they enter if its valid, thanks!

float multiply (float)
{
float local;

cin >> local;

num = num * local; // multiplied to result

return num;
}

....i dont know that doesnt really help lol.
The code example from line 19 to 29 does exactly what you want, you just need to change int to float
Last edited on
lol ok i did that but ill try again, thanks.
hmmm i dont know i cant get it to work... any ideas?
The article works fine. You probably just aren't doing it right. Code plz
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134

#include <iostream> // 
#include <cmath> //
#include <cctype> //

using namespace std; // standard namespace

char operation; //declared variables
float num;
float addition(float); // function protoype
float subtraction(float);
float multiply(float);
float divide(float);
float power(float);

int main() // main function
{
	cout << "Calculator Program!" << endl; // title
	cout << endl;
	
	cout << "The following operations are valid:" << endl;
	cout << "+	add" << endl;
	cout << "-	subtract" << endl;
	cout << "*	multiply" << endl;
	cout << "/	divide" << endl;
	cout << "^	power" << endl;
	cout << "q	quit" << endl;
	cout << endl;

	cout << "Please enter the operation you would like to do and then the number: " << endl; // prompt to choose operation
	cin >> operation; // choose operation

	while (operation) // while loop when operation is chosen
	{
		switch (operation) // switch statement for operation
		{
		case '+' : addition(num); // addition function
			cout << "the result is now " << num << endl;
			break;
	
		case '-' : subtraction(num); // subtraction function
			cout << "the result is now " << num << endl;
			break;

		case '*' : multiply(num); // multiplication function
			cout << "the result is now " << num << endl;
			break;

		case '/' : divide(num); // division function
			cout << "the result is now " << num << endl;
			break;

		case '^' : power(num); // power function
			cout << "the result is now " << num << endl;
			break;

		case 'q' : 
		case 'Q' :
			cout << "The final result was " << num << endl;
			cout << endl;
			cout << "Thanks for using the calculator program!" << endl;
						return 0; // ends program
			break;

		default: 
			cout << "invalid operation entered, please try again." << endl; // if a letter or symbol other than the cases was picked
		}

		cin >> operation;
	}

}

float addition(float) // addition function
{
	float local; // local variable 

	cin >> local; // number they want to add

	num = num + local; // added to last result
	return num; // new result is returned

}

float subtraction (float) // subtraction function
{
	float local;

	cin >> local;

	num = num - local; // subtracted from result

	return num;
}

float multiply (float)
{
	float local;
	
	cin >> local;
		
	num = num * local;
		
	return num;
}

float divide (float)
{
	float local;
	
	cin >> local;

	if (local  = '0')
	{
		cout << "Cannot divide by zero, thank you." << endl;
			return 1;
	}
	else

	num = num / local; // divided by result

	return num;
}

float power (float)
{
	float local;

	cin >> local;

	num = pow(num, local); // result raised to power

	return num;
}
You aren't using the code from the article in there, you are reading directly from cin to your variable
i know but like how would i incorporate that code from the article into my code, i tried and couldnt get it to work.
First, you should get input outside the functions addition, subtraction etc. and pass it as argument.
Then, instead of cin >> local you should do the stringstream thing


Notice: Line 113 - in function 'divide' - : if (local = '0') means if ( assign the ASCII value for the character '0' to 'local' )
yea.... thats what i was gonna do but i figured i may as well see if i can do it this way, alright thanks.
yea.... thats what i was gonna do but i figured i may as well see if i can do it this way, alright thanks.
haha k i put input value outside of the functions and now i cant get it to work ill show you how i tried and i jus put it in the addition switch cause didnt wanna make my program even longer....

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
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream> // 
#include <cmath> //
#include <cctype> //
#include <sstream> // 
#include <string> // 

using namespace std; // standard namespace

char operation; //declared variables
float variable;
float num;
float addition(float, float); // function protoype
float subtraction(float, float);
float multiply(float, float);
float divide(float, float);
float power(float, float);

int main() // main function
{
	cout << "Calculator Program!" << endl; // title
	cout << endl;
	
	cout << "The following operations are valid:" << endl;
	cout << "+	add" << endl;
	cout << "-	subtract" << endl;
	cout << "*	multiply" << endl;
	cout << "/	divide" << endl;
	cout << "^	power" << endl;
	cout << "q	quit" << endl;
	cout << endl;

	cout << "Please enter the operation you would like to do and then the number: " << endl; // prompt to choose operation
	cin >> operation; // choose operation
	
	float myNumber = 0;

	while (operation) // while loop when operation is chosen
	{
		switch (operation) // switch statement for operation
		{
		case '+' :
			getline(cin, variable);
			stringstream myStream(variable);
			if (myStream >> myNumber)
			{
				cout << "invalid number" << endl;
				break;
			}
			else
			{
				addition(variable, num); // addition function
				cout << "the result is now " << num << endl;
				break;
			}
	
		case '-' : 
				subtraction(variable, num); // subtraction function
				cout << "the result is now " << num << endl;
				break;

		case '*' : 
				multiply(variable, num); // multipliation function
				cout << "the result is now " << num << endl;
				break;

		case '/' :
				divide(variable, num); // division function
				cout << "the result is now " << num << endl;
				break;
			
		case '^' : 
				power(variable, num); // power function
				cout << "the result is now " << num << endl;
				break;
	
		case 'q' : 
		case 'Q' :
			cout << "The final result was " << num << endl;
			cout << endl;
			cout << "Thanks for using the calculator program!" << endl;
						return 0; // ends program
			break;

		default: 
			cout << "invalid operation entered, please try again." << endl; // if a letter or symbol other than the cases was picked
		}

		cin >> operation;
	}

}

float addition(float, float) // addition function
{ 
	num = num + variable; // added to last result
	return num; // new result is returned

}

float subtraction (float, float) // subtraction function
{
	num = num - variable; // subtracted from result

	return num;
}

float multiply (float, float)
{		
	num = num * variable;
		
	return num;
}

float divide (float, float)
{
	if (variable == 0)
	{
		cout << "Cannot divide by zero, thank you." << endl;
			return 1;
	}
	else

	num = num / variable; // divided by result

	return num;
}

float power (float, float)
{
	num = pow(num, variable); // result raised to power

	return num;
}
You should put it before the switch, in this way you'll get input for all the functions
cant prof is kinda crazy about how the output should be so and need to choose operation first then number ( + 4 ect.) and how would i incorporate that code from the other thread into mine cause how i have it in the adding one does not work, thanks.
cant prof is kinda crazy about how the output should be so and need to choose operation first then number
It would be after getting the operation:
1
2
3
4
5
6
7
//pseudocode
get operation
get number
switch operation
{
    do calculations
}
Writing the same input code each time is just a waste of time.
Anyway, you can put the input part in a function and call it when you want:
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
bool GetInput ( float &value )
{
    int number;
    string input;
   getline(cin, input);
   stringstream myStream(input);
   if (myStream >> number)
   {
       value = number;
       return true; // input succeeded
   }
   return false; // failed
} 


//...


switch (operation) // switch statement for operation
{
   case '+' :
       float local;
       if ( !GetInput(local) )
            cout << "invalid input";
    //etc. 


how would i incorporate that code from the other thread into mine cause how i have it in the adding one does not work
I don't get what you are asking, can you be more clear?
An answer to the very first post :

To know if the input is a correct expression, just check the ASCII code of each character.The function should check if (x >= '0' and x <= '9') or (x = 'simbol1') or (x = 'simbol2').... where x is a char vector (for example). Just do that comparison for every component until you find '\0' . If all the vector components verify the if-statement, then the expression is ready to go, and you can process it.



Last edited on
Topic archived. No new replies allowed.