Sign in to follow this  

Is there a better way...?

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is what I'm working off of: "Write a program that overloads a getnumber function. If you pass it a bool argument, that version of the function returns zero or one depending on whether the argument is false or true. If you pass it an int, that version of the program returns the value multiplied by itself." Teach Yourself C++ 6th Edition Page 67 Practice #2 Here is my code for the entire program:
#include <iostream>

short inches;
bool yn;

inline bool getNumber(bool);
inline int getNumber(int);

int main()
{
 std::cout << "Chapter 3";
 std::cout << "\nExtra Practice 2";
 std::cout << "\n\nAre you looking for the measurement of the area in a square? (1=Yes 0=No) ";
 std::cin >> yn;
 
 if (getNumber(yn) == 1)
 {
	 std::cout << "\nWhat is the measurement of one side of the square in inches? ";
	 std::cin >> inches;
	 std::cout << "\nThe area of your square is " << getNumber(inches) << " units.";
 }
 else
 {
 	std::cout << "\nSorry this program is only for calculating the area of a square.";
 }
}

inline bool getNumber(bool b)
{
 return (b ? 1 : 0);
}

inline int getNumber(int i)
{
 return i * i;
}

Is there a different way that would work better to do the overloaded functions? Second question: "Write a function that accepts an int with a default function value of -1. If you call the function with no arguments, it displays a message to that effect. If you pass an argument of other then -1, the function displays the argument value." Teach Yourself C++ 6th Edition Page 67 Practice #3 The program written:
#include <iostream>

int anInt;
bool question;

void myFunc(int i = -1);

int main()
{
 std::cout << "Chapter 3";
 std::cout << "\nExtra Practice 3";
 std::cout << "\n\nDo you wish to enter a number? (1=Yes, 0=No) ";
 std::cin >> question;
 
 if (question)
 {
 	std::cout << "\n\nPlease enter a number: ";
 	std::cin >> anInt;
 	
 	myFunc(anInt);
 }
 else
 {
 	myFunc();
 }
}

void myFunc(int i)
{
 if (i == -1)
 	std::cout << "\nERROR...Nothing new entered...";
 else
 	std::cout << "\nGood job, you got it right!  Here's your number: " << i;
}
[\source]
If the user enters -1 it does what the question asks it to do.  Is there a way to check to see if NOTHING was passed to the myFunc() in general?  In this way getting rid of the possibility that the user will enter -1?

Thank you all in advance for your advice and help.
[Edited by - zychrias on August 20, 2004 2:33:13 AM]

Share this post


Link to post
Share on other sites
2) I tried it, here's what I found out:
When in a console (MSDOS) you can't just hit enter at the prompt for a number. So my problem I guess is how can I tell if someone just enters "-1" in specifically. If they hit ENTER or RETURN it just skips a line and waits for an input.

That was a great idea though, I hadn't thought of the '\n' being passed to it before.

Share this post


Link to post
Share on other sites
im tired and probably read another question wrong, but cant you just do something like:


if (question)
{
std::cout << "\n\nPlease enter a number: ";
std::cin >> anInt;

if(anInt != '')
myFunc(anInt);
else
blahblah
}


edit: that ' is supposed to be '' or 2x'... silly thing :/

Share this post


Link to post
Share on other sites
If you're reading into an integer variable, then it will only hold an integer. Which is what the cin >> will produce for you, and what you pass to the function (which is what it's expecting).

'\n' technically is in that set, but it's equivalent to 13 (because of the whole ascii thing), and because of how cin's operator>> is overloaded, you won't get that 13 by just hitting return at the input prompt. (it's trying to interpret the text as a number written in decimal). "" (the empty string) definitely is not in that set.

So no, you can't really check for the possibility that the input number was -1. And you're not really supposed to be able to. The default argument is provided as a cover-up, really. The compiler basically adds in the default arguments "behind the scenes" to calls to the function that don't have enough arguments. If you don't want that, then overload the function instead (i.e. write a separate version of the function to deal with the shorter argument list).

FWIW though, I've found in practice that you don't really use overloads as often as you might think. They're very nice to have available to you when you do want them, but that doesn't happen all that often.

Share this post


Link to post
Share on other sites

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this