Maximum function

I am trying to write a function its job to input values until non-number reached it returns the largest value, this function has no arguments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 double getMaximum()
	{
		double number , max ;
		cout << "Gimme #'s: ";
		cin >> number;
		if (!cin)
			recover();

		for (; cin ;)
		{
			
			cout << "Gimme #'s: ";
			cin >> number;
		}
		return ;
	} //  
any help with this one
closed account (j3Rz8vqX)
Whats the problem?

Line 13.5: if(max<number){max=number;}//?

Reset flags and remove buffer?
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
#include<iostream>
using namespace std;
void recover()
{
    cin.clear();
    cin.ignore(256,'\n');//ignore up to 256 characters - seeking '\n' in buffer
}
double func()
{
    double number , max=0 ;
    cout << "Gimme #'s: ";
    cin >> number;
    if (!cin)
        recover();//No clue what you want!
    for (; cin ;)
    {

        cout << "Gimme #'s: ";
        cin >> number;
        if(max<number){max=number;}
    }
    return max;
}
int main()
{
    cout<<"Largest value entered: "<<func()<<endl;
    cout<<"Custom Exit: Press enter: ";
    cin.get();
    return 0;
}


Not sure what you are asking...
I am sorry for the late reply I was busy with school

anyway, the function's job is to let user input #'s till user enters non-#
the function will return the largest or max number
fadinzr wrote:
I am sorry for the late reply I was busy with school

anyway, the function's job is to let user input #'s till user enters non-#
the function will return the largest or max number


From what i can understand , you can do this :
1.extract a word from cin , in a std::string
1
2
std::string temp;
cin >> temp;

2.check if its a number
1
2
3
bool num=true;
for(auto ch : temp) //c++11 for each loop,change if you are not allowed
   if(!isdigit(ch)) num=false;

3.check the condition
1
2
if(num) ...convert temp to integer , find max and continue
else ...break out of loop and print max


Hope that helps.
Last edited on
If you try to 'cin >>' something that isn't a number into a number variable, a flag will be set on cin. if ( cin ) will tell you whether any flags have been set or not.
Why not simply do something like:

1
2
3
4
5
6
7
8
9
10
while(std::cout << "Please enter a number: " && std::cin >> number)
{
    if(number > max) max = number;
}

//clear error states and ignore anything else
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

return max;
Last edited on
thanks for the help

I don't know much about the std:: stuff
is there any other way?

I am also looking for a variable which it stores all my inputs
and then it return the largest value after input failure ?
If you don't do
using namespace std;
at the start of your code, you have to put std:: before a bunch of things that come from the standard library. Using namespace std is actually bad practice, which is why many people here don't use it.
I am also looking for a variable which it stores all my inputs read the variables into a static array or some sort of dynamic container such as a vector.

http://www.cplusplus.com/reference/stl/
1
2
3
4
5
6
7
8
9
10
11
12
13
double getMaximum()
	{
		double number, max = 0,count = 0;
		cout << "Gimme #'s: ";
		for (number; cin >> number; count++)
		{
			if (number > max)
				max = number;
		}
		recover();
		if (count == 0) die("Undefined value");
		return max;
	}
Topic archived. No new replies allowed.