Using istream in as a function parameter

S

Steph W.

Hello,

I had a quick question about using istream/cin as a function parameter. I'mworking on a homework problem that is requiring me to create a recursive function called Max that returns the max value of a sequence of numbers thatis entered in by the user. We are not allowed to use any type of data structure and thats why I chose to use istream/cin. Here is what I have so far:


#include <iostream>
#include <istream>

using namespace std;

int MAX(istream x);

int main()
{
int number;
char option;

cout << "This program will print out the maxium value that the user inputs..";
do
{
cout <<"Enter in values to find the maxium number. Enter in -1 to stop: ";
cout << MAX(cin) << endl;
cout <<"Would you like to run the program again? (Y/N): ";
cin >> option;
}while(toupper(option) =='Y');

return 0;
}

int MAX(istream x)
{
int largest =0;
if (x <= 0)
return largest;
if ((x % 2 == 0) && (largest < x)) //IntelliSense: no operator "%" matches these operands

{
largest = x; // IntelliSense: no suitable conversion function from "std::istream" to "int" exists

return MAX(x);
}
else
MAX(x);
return return largest;
}




Thank you in advance for any advice.
 
V

Victor Bazarov

Hello,

I had a quick question about using istream/cin as a function parameter. I'm working on a homework problem that is requiring me to create a recursive function called Max that returns the max value of a sequence of numbers that is entered in by the user. We are not allowed to use any type of data structure and thats why I chose to use istream/cin. Here is what I have so far:


#include <iostream>
#include <istream>

using namespace std;

int MAX(istream x);
[..]

Make sure to pass your stream by a reference. You can't copy streams,
they have copy c-tors protected.
Thank you in advance for any advice.

V
 
P

Paul N

Hello, I had a quick question about using istream/cin as a function parameter. I'm working on a homework problem that is requiring me to create a recursive function called Max that returns the max value of a sequence of numbers that is entered in by the user. We are not allowed to use any type of data structure and thats why I chose to use istream/cin. Here is what I have so far: #include <iostream> #include <istream> using namespace std; int MAX(istream x); int main() { int number; char option; cout << "This program will print out the maxium value that the user inputs."; do { cout <<"Enter in values to find the maxium number. Enter in -1 to stop: "; cout << MAX(cin) << endl; cout <<"Would you like to run the program again? (Y/N): "; cin >> option; }while(toupper(option) =='Y'); return 0; } int MAX(istream x) { int largest =0; if (x <= 0) return largest; if ((x % 2 == 0) &&(largest < x)) //IntelliSense: no operator "%" matches these operands { largest = x; // IntelliSense: no suitable conversion function from "std::istream" to "int" exists return MAX(x); } else MAX(x); return return largest;} Thank you in advance for any advice.



Hello, I had a quick question about using istream/cin as a function parameter. I'm working on a homework problem that is requiring me to create a recursive function called Max that returns the max value of a sequence of numbers that is entered in by the user. We are not allowed to use any type of data structure and thats why I chose to use istream/cin. Here is what I have so far: #include <iostream> #include <istream> using namespace std; int MAX(istream x); int main() { int number; char option; cout << "This program will print out the maxium value that the user inputs."; do { cout <<"Enter in values to find the maxium number. Enter in -1 to stop: "; cout << MAX(cin) << endl; cout <<"Would you like to run the program again? (Y/N): "; cin >> option; }while(toupper(option) =='Y'); return 0; } int MAX(istream x) { int largest =0; if (x <= 0) return largest; if ((x % 2 == 0) &&(largest < x)) //IntelliSense: no operator "%" matches these operands { largest = x; // IntelliSense: no suitable conversion function from "std::istream" to "int" exists return MAX(x); } else MAX(x); return return largest;} Thank you in advance for any advice.

[Apologies if Google has messed up the formatting the way it appears to have...]

Since this is homework, I'll just give you a hint - you need to think aboutwhether x is the input stream or is the value you read from it. IntelliSense is giving you good advice here.

You might also like to consider whether "largest" should be passed around, and if so, where.
 
S

Steph W.

Thanks for all the help thus far! I've rewritten the code and it complies and runs, but my function always returns a zero. It has something to do with my logic, but I can't figure it out. A point in the right direction would be very helpful!


#include <iostream>
#include <istream>

using namespace std;

int MAX(istream& user_input);

int main()
{

char option;

cout << "This program will print out the maxium value that the user inputs.";
do
{
cout <<"Enter in values to find the maxium number. Enter in -1 to stop: ";
cout << MAX(cin) << endl;
cout <<"Would you like to run the program again? (Y/N): ";
cin >> option;
}while(toupper(option) =='Y');

return 0;
}

int MAX(istream& user_input)
{
int largest;
int x;
user_input >> x;
if (x <= 0)
return largest;
if ((x % 2 == 0) && (largest < x)) //

{
largest = x; //

return MAX(user_input);
}
else
MAX(user_input);
return largest;
}
 
S

Steph W.

The only thing I can think of is that x is only getting the very last value in the cin stream. Would this be a valid guess? I thought cin.getline was only suppose to be used for strings.
 
V

Victor Bazarov

[..]
int MAX(istream& user_input)
{
int largest;
int x;
user_input >> x;

Your 'largest' is *local* and uninitialized. If 'x' is negative, you're
using 'largest' as the return value. What value do you think it has at
that time?

Your 'x' is uninitialized. If the extraction from the stream fails for
some reason, 'x' is unchanged. You're using it below in some
expressions. What value do you think it has in that situation?
if (x <= 0)
return largest;
if ((x % 2 == 0) && (largest < x)) //

{
largest = x; //

return MAX(user_input);

If you're going to return whatever the function returns, what's the
point of assigning anything to 'largest'? Every invocation of 'MAX'
function has its own 'largest' object - it's *local*. Recursion does
*not* make local variables of the caller available to the called function.
}
else
MAX(user_input);
return largest;

Again, 'largest' is uninitialized. What's the point of returning its
value (which value does it have?) in this case?

Also, consider that C++ is not Python. Indenting has no particular
syntactic meaning.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top