Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Function problems

Started by glyvin Feb 20, 2007 at 10:33 AM 10 replies 1.6k views
Original Post
glyvin
glyvin
Hey I'm having a little problem, im trying to make a program to help better understand functions and in this program I need a function to take 2 variables and divide them, if the 2nd number is bigger it should return a -1 , else It should return the value. I'm just having a little problem that I can't figure out. Here is the code #include #include int divison(int x, int y); int main() { int x,y; int var; cout << " Please give me 2 numbers, make the 1st greater then the 2nd.\n"; cout << " Please input the 1st number " <<"\n"; cin >> x; cout << " Please input the 2nd number " <<"\n"; cin >>y; var= division(int x, int y); if (var = -1) cout << "Hey your 2nd number was bigger...\n"; else cout << "Number 1 divided by number 2 = "<y) return (x/y); else return ( -1); } So yea I don't know what it is. Do I have to set a value for what is going to be returned, because I will have to display that value. That is what the variable var is intented to do , but it seems to not like that. Any help would be greatly appeciated. Happy coding Glyvin Edit(I changed it now im just getting a Phrase error on line 14
Aardvajk
Aardvajk
if (var = -1)cout << "Hey your 2nd number was bigger...\n";


Should be:

if (var == -1)cout << "Hey your 2nd number was bigger...\n";


In the first case, you are assigning -1 to var, not testing it for equality. Double-equals (==) is the equality test operator in C and C++.

[EDIT] Unrelated note, but you should probably guard against the user entering zero as the second parameter, otherwise your program will crash with a division-by-zero exception thingy.
glyvin
glyvin
thank you very much :) and also im getting a parse error on line 14

var = division(int x,int y);

am I passing that to the function correctly or is there something that I am not seeing?
jouley
jouley
You don't need to declare arguments as ints when you call the function, only when you declare it. So, that line should look like:
var = division(x, y); 


-jouley

[Edit: Your post 26 seconds before mine was exactly what was needed, thanks! The one directly after this post gives the specific message, which is also handy, but again, the line number can be hard to match up with the lines in posts. Between the two, you covered your bases.]
glyvin
glyvin
14 h:\devc__~1\chapte~4\chapte~1\5makin~1.cpp
parse error before `,'
hydrogen
hydrogen
I should add that the reason it throws the error is because it doesn't know what the word "division" refers to on line 14. The compiler is going through the file from top to bottom and expects stuff it reaches to be already be declared previously. As it doesn't know what "division" is on line 14 it throws an error.

The declaration on line 3 is attempting to declare it ahead of time except for the misspell.

Sneftel
Sneftel
Moved to For Beginners.
nobodynews
nobodynews
I took the liberty of rewriting much of the code, mostly to show alternate ways you can write the same program. With the change mentioned by the others then your program should work, but you did a few things that are either wrong(using iostream.h) or at least could be written more cleanly. Most of this is my own opinion so don't take it as gospel. Except for not using iostream.h. Never use iostream.h unless you have a very compelling reason not to.
// in c++ we use iostream without the .h and stdlib without the .h with a c in the front// This is because iostream.h never officially existed as part of standard c++ and// in order to remind people that this isn't C we change stdlib.h to it's c++ version.// You don't use any functions from cstdlib in this example, but I'll keep it in anyway.#include <iostream>#include <cstdlib>// Now that we are using the proper header files, we need to either a)bring all of the// standard symbols into the current scope, b)bring only the standard symbols// we want into the current scope, or c)qualify every standard symbol with std::// I'm going with b)using std::cout;using std::cin;using std::endl;// You spelled division wrong here.  Personally I would have called it divide (as in I want// to divide two numbers) rather than division (I want to division two numbers sounds strange).// However, I will rename x and y as dividend and divisor (see future comment)int division(int dividend, int divisor);int main(){	// x, y, and var aren't always horrible names for variables, but in this case there are	// very good names from mathematics we can steal.  Given c = a / b then a is	// the dividend, b is the divisor, and c is the quotient.  We now have perfectly	// descriptive names for the new variables.	int dividend, divisor, quotient;	// With better names for our variables we can write better instructions to the user:	cout << " Please input the dividend: ";	cin >> dividend;	// Remember: if a is greater than b then this implies that b is less than a.	cout << " Please input the divisor so that it is less than the dividend: ";	cin >> divisor;	quotient = division(dividend, divisor); // don't put int before x or y, they already HAVE a type!	// I added blocks.  They aren't really necessary, but I think it clarifies things... especially when you put	// several comments between the if statement and the output statement like this.	if (quotient == -1)	{		// Reworded your output statement and changed the \n to an endl to be consistant with the else statement		// we could have also rewritten the else statement to use \n		cout << "Hey your divisor was too big..." << endl;	}	else	{		// reworded output statement.		cout << "Dividend / divisor = " << quotient << endl;	}	return 0;}// Again, I added blocks.  This is just a style preference.int division(int dividend, int divisor){	if (dividend > divisor)	{		return (dividend / divisor);	}	else	{		return ( -1);	}}
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
glyvin
glyvin
Hey I really appreciate the help, It explained alot to me , I think im having a little problems with Functions, also I am using the learn C++ in 21 days and im at the end of chapter 5. They have not used the "STD" that you use in the book, could you explain more in depth what that does?

thanks alot for taking time to rewrite that

glyvin
glyvin
glyvin
Also when I run your version I tryed to see if it would return -1 but when i inputed 10 and 20 the program just ended. I thought that might just be my compiler so I added a CIN to make it wait but it still just ends if I enter a small number then a large number?:
any idea?

glyvin
nobodynews
nobodynews
First post:

Short answer: Your book is old and wrong. Get a new book. Try this one: C++: A Dialog

Long answer: I'm not sure when that book was first published, but the official c++ standard was released in 1998. However, c++ existed before this time. This means that everyone did stuff differently. Now, Visual C++ 6.0 was released at about the same time as the standard came out and so did a lot of things different than was meant. Also a lot of people don't know how to write.

Second post:

It works for me, so I'm pretty sure I know what's going on. When a program is over, it closes. A Console program is over when you no longer get input from the user. The problem arises due to the fact you want to see the output of your program. This happens There are a number of possible ways around this, although most aren't 'correct' in the sense that you're abusing the way console programs are meant to be run.

1)Run the program from the command line

This means you open up a console window, find your program, and execute it. The program won't close when you are done

2)Make a loop quit when the user enters in the correct input

Some pseudo code:
do {  // Get input  // Calculate result  // Ask user: "do you wish try again?(y/n) "  // get input, preferably like this:  std::string input;  cin << input;} while(input == "y");


3)Add an extra cin

This should stop the program from ending. This is one of the abuses I was talking about. However, it didn't work for you. Maybe you put it in the wrong location. I can't be sure as you didn't tell me where you put it. Did you put it before the return statement or after? Did you put it somewhere else?

4)Adding a sleep command to pause the ending of the program for a period of time

This is almost a good idea, except there's no portable sleep command. You can write your own, but then you have to start using timers and what-not.

5)Using system("pause")

Probably the worst way to pause your program. What this does is call another program (pause) that outputs "press any key to continue". There are two issues with this. 1)What if this program doesn't exist on the user's computer? 2)What if someone replaces the pause program with a different program with the same name?

6)Use an IDE that does this all automatically when you run the program. For instance, in Visual C++ if you "start without debugging" then the program will stay until you press a key at the end.

I think that's all of the different ways around it.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.