void biggest_number() {
float first_input;
float big_num;
float input;
cout <<"\n You've chosen to Find out The Biggest Number. Type in number to compare\nType 0 if you want to stop the function.\nFirst number : ";
cin >> first_input;
first_input = big_num;
//cout << first_input;
while (1) {
cin >> input;
if (big_num < input) {
input = big_num;
}
else if (input == 0) {
cout << big_num;
break;
}
else{
}
};
};
So I've written a function to find the biggest number. But whatever I put as an input, big_num
always comes out as 0.
I've tried to use cout <<first_input << big_num;
in order to find out where the function doesn't work. You can see from the comment. That part doesn't work and I couldn't figure out why.
CodePudding user response:
You have the assignment first_input = big_num;
reversed. It should be big_num = first_input;
CodePudding user response:
float big_num;
You declare big_num
but haven't assigned it a value, so big_num
is uninitialized. So big_num
can hold any value. Then, you assign first_input
to equal big_num
. So both first_input
and big_num
is holding random values.
It seems kind of strange to me that you input first_input
, then immediately you assign first_input
to equal big_num
. I think that you meant the other way around.
big_num = first_input;
Also:
void biggest_number() {
float first_input;
float big_num;
float input;
cout <<"\n You've chosen to Find out The Biggest Number. Type in number to compare\nType 0 if you want to stop the function.\nFirst number : ";
cin >> first_input;
first_input = big_num;
//cout << first_input;
while (1) {
cin >> input;
if (big_num < input) {
input = big_num;
}
else if (input == 0) {
cout << big_num;
break;
}
else{
}
}; // <-- here
}; // <-- and here
There is two unnecessary semicolons. You should remove it.
note: using namespace std;
is bad practice, so avoid using it.
CodePudding user response:
void biggest_number()
{
float first_input;
float big_num;
float input;
cout <<"\n You've chosen to Find out The Biggest Number. Type in number to compare\nType 0 if you want to stop the function.\nFirst number : ";
cin >> first_input;
big_num = first_input;
cout << "Second number :";
while (1)
{
cin >> input;
if (first_input < input)
{
big_num = input;
cout<< big_num << " Is the big number" << endl;
break;
}
else if (input == first_input)
{
cout << "Both inputs are same" << endl;
break;
}
else
{
cout<< big_num << "Is the big number" << endl;
break;
}
}
}
int main()
{
cout<<"Hello World";
biggest_number();
return 0;
}
CodePudding user response:
See @justANewbie's answer about the actual problem (mixing up your input
and big_num
variables).
I just wanted to provide a more compact solution. It's not important that the code is short in this case, but you might learn some more convenient C coding tricks/styles from it. Additionally, it's sometimes easier to spot errors when the code is compact and has less branching.
This solution uses a common C pattern you might call "read while":
#include <cmath> // for INFINITY
#include <iostream>
void biggest_number() {
float biggest = -INFINITY;
// while we can read a number and it's not zero
for (float input; (std::cin >> input) && input != 0; ) {
// keep track of biggest number found
if (input > biggest) biggest = input;
}
std::cout << biggest;
}