Reputation: 177
I was given a pseudo code to translate into C++ :
Set a Boolean variable “first” to true.
While another value has been read successfully
If first is true
Set the minimum to the value.
Set first to false.
Else if the value is less than the minimum
Set the minimum to the value.
Print the minimum
And here are my codes :
bool first = true;
bool read_value = true;
int value = 0;
int minimum = 0;
cout << "Enter an integer : " ;
cin >> value;
while(cin.hasNextInt()) // error here
{
cout << "Enter another integer : " ;
cin >> minimum;
if( first == true){
minimum = value;
first = false;
}else if ( value < minimum){
minimum = value;
}
}
cout << minimum;
system("PAUSE");
return 0;
There is error at the hasNextInt there. And I don't really know what the pseudo code wants. Can somebody explain to me?
Thanks in advance.
Upvotes: 0
Views: 1796
Reputation: 28178
That is stupid pseudo code. We can do better without some worthless bool...
int min = std::numeric_limits<int>::max();
for(int val; cin >> val)
{
if(val < min)
min = val;
}
cout << "Minimum: " << min << '\n';
Upvotes: 0
Reputation: 158499
This is closer to code you want:
cout << "Enter an integer : " ;
while( cin >> value )
{
if( first == true){
minimum = value;
first = false;
}else if ( value < minimum){
minimum = value;
}
cout << "Enter another integer : " ;
}
Upvotes: 0
Reputation: 2340
There is no hasNextInt()
function in the standard C++ libraries (and that's why you can't compile). There is one in Java, however!
Upvotes: 1