Advertisement

C++ Workshop - Functions, Parameters, & Scope (Ch. 5)

Started by June 26, 2006 12:22 PM
45 comments, last by me_minus 15 years, 5 months ago
It's difficult for editors to proof read code. :)
I mentioned some typos a while ago and wondered at that stage if they could be fed back to improve the next edition. I don't know how many of us are here following the weekly threads, but the book is being reviewed systematically so wouldn't it be logical for comments to be passed on? I appreciate that editors may find it difficult to see errors in code, so why not "use" the help that we might be able to offer via this forum?

Even if detailed comments weren't fed back, it would be possible to bring this workshop to the attention of the authors and editors so they could go through the comments that have been raised.
Advertisement
Quote:
Original post by CondorMan
I mentioned some typos a while ago and wondered at that stage if they could be fed back to improve the next edition. I don't know how many of us are here following the weekly threads, but the book is being reviewed systematically so wouldn't it be logical for comments to be passed on? I appreciate that editors may find it difficult to see errors in code, so why not "use" the help that we might be able to offer via this forum?

Even if detailed comments weren't fed back, it would be possible to bring this workshop to the attention of the authors and editors so they could go through the comments that have been raised.


One should be able to contact the author of the book if there are some contact informations available - or simply contact the book editor (don't be affraid by them - they are kind and respectful; after all, you are their customers) to get in touch with the author.

Regards,
Hello all,

I've started the workshop 3 days ago and I'm trying to catch up... although it will be a bit hard :).

I'm currently finishing chapter 5... and I'm trying to catch up ...

Great work on the workshop by the way! If it wasn't for this
I wouldn't have started on this quest to learn C++!

// This post has been edited after I saw how big a fool I was
// for making the foolish question I made =P
No question is too foolish.. at least in my book. :)
were the answers for these questions posted somewhere? :D
Advertisement
Which questions?
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote:
Original post by Alphabat
Hello all,

I've started the workshop 3 days ago and I'm trying to catch up... although it will be a bit hard :).

I'm currently finishing chapter 5... and I'm trying to catch up ...

Great work on the workshop by the way! If it wasn't for this
I wouldn't have started on this quest to learn C++!







I am in the same boat so I quote the above :)

I was working on chapter five and I typed in the code from listing 5.2 (converting Fahrenheit to Celsius) and I keep getting an error:

// Practice#include<iostream>float Convert(float);  //function prototypeint main(){		float tempFer;	float tempCel;		std::cout << "\nPlease enter a temp in Fahrenheit: ";	std::cin >> tempFer;	std::cout << "\nHere is the temp in Celsius: ";	std::cout << tempCel << std::endl;		return 0;}float Convert(float tempFer){	float tempCel;	tempCel = ((tempFer - 32) * 5) / 9;	return tempCel;}


The error I get is:

e:\documents and settings\dave\my documents\visual studio 2005\projects\practice\practice\practice.cpp(16) : warning C4700: uninitialized local variable 'tempCel' used

I can't figure out the problem, I tried initializing it to '0' but then the function always returns '0' :(

Nevermind, I figured it out, I forgot to call the function :p ...


// Practice#include<iostream>float Convert(float);  //function prototypeint main(){		float tempFer;	float tempCel;		std::cout << "\nPlease enter a temp in Fahrenheit: ";	std::cin >> tempFer;        tempCel = Convert(tempFer); // Forgot this :(	std::cout << "\nHere is the temp in Celsius: ";	std::cout << tempCel << std::endl;		return 0;}float Convert(float tempFer){	float tempCel;	tempCel = ((tempFer - 32) * 5) / 9;	return tempCel;}
Hi guys,

Just wanted to ask about a part of the text in the book that goes like this:

Page. 130 Last paragraph states the following and I quote

Quote:

Last-in, first out means that whatever is added to the stack last is the first thing taken off. This differs from most queues in which the first in is the first out (like a line at the theater).


Shouldn't the queues be first in, last out?

Meaning:
Stack: First in, First out 'FiFo'
Queu: First in, Last out 'FiLo'?

By the way, JWalsh, if your reading this, congratulation on your son, I know it's late, but, better late then never right.


Edited: due to additional question.

Exercise:5_6

Using the call to a function as an expression? Is that often done or good coding habit?

[source = C++]// Example(s) and exercise(s) from the book: Sams Teach Yourself// Authors: Jesse Liberty and Bradley Jones// Combination with the online tutorial classes from Jeromy Walsh on Gamedev.#include <iostream>double Division(unsigned short int num1, unsigned short int num2);int main(){	double result;	if ((result = Division(0, 1)) == -1) // <--- this part???		std::cout << "Dividing by zero is not possible!" << std::endl;	else		std::cout << result << std::endl << std::endl;			char ch;	std::cout << "\nQuit program by pressing any letter or number!\n";	std::cin >> ch;	return 0;}double Division(unsigned short int num1, unsigned short int num2){	if (num2 == 0)		return -1;	else		return num1/num2;}


[Edited by - J0Be on May 20, 2007 9:36:10 AM]
Quote:
Original post by J0Be
Hi guys,

Just wanted to ask about a part of the text in the book that goes like this:

Page. 130 Last paragraph states the following and I quote

Quote:

Last-in, first out means that whatever is added to the stack last is the first thing taken off. This differs from most queues in which the first in is the first out (like a line at the theater).


Shouldn't the queues be first in, last out?

Meaning:
Stack: First in, First out 'FiFo'
Queu: First in, Last out 'FiLo'?

Nope, the book has it right. Think of a stack of papers. Someone puts a new paper on the top of the stack and you grab the top most paper to work on it. The last page put on the stack is the first one you grab. Likewise, if someone puts one page on top of another the stack gets higher and higher. The first page put down can't be removed until all the pages above it are removed. Hence the 'first in last out'.

As for a queue, if you were standing in line for a drink of water and got there first, would you be the last person to get a drink? No! You'd be the first person to get a drink. If five people got in line, say first Alice, then Bob, Carrie, Dave, and finally Eliza, then Alice would leave first, then Bob, Carrie, Dave, and finally Eliza. Or ordering from a restaurant that can only cook one thing at a time. Or being put on hold for a help-line where it tells you 'there are 20 people ahead of you".
Quote:

Exercise:5_6

Using the call to a function as an expression? Is that often done or good coding habit?


The exact type of line
if ((result = Division(0, 1)) == -1) // <--- this part???

That is, the call to a function itself is pretty common, but assigning the result while at the same time comparing the result to something else is something I personally avoid. I would have written that code like this:

int result = Division(0,1);if(result == -1)


However, as I said, it IS pretty common to use a function as an expression. It depends on the function and it depends on the expression though. The problem with that particular example you gave is that the function returned either an error code (-1) or a result. This wouldn't be so bad if there wasn't valid input that would give -1 as a result (say Division(-2, 2); ). There are a number of different ways to write a function to take care of this kind of situation. If you use exceptions you can just throw an error, you can write the code under the assumption that no one will ever give 0 in the denominator(add an assert(denominator != 0); ). Maybe you can write something like this:

bool Division(double numerator, double denominator, double &result){  if (denominator == 0)  {    return false;  }  result = numerator / denominator;  return true;}int main(){  int answer;  if(Division(-2, 2, answer) )  {    cout << "Answer: " << answer << endl;  }  else  {    cout << "Divide by zero error" << endl;  }}


Hope that helps.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement