Why should I put this in....?
Ok, so I'm reading my book on C++, and I've come upon a program that multiplies two variables together, displays the result, and if the user wants to quit, merely enters in two 0's.
It works with a for loop, and a function that is defined.
ie)
long multiply(long a, long b);
How about I just copy and paste the code THEN ask....
here goes...
/*Program to determind the product of two intergers entered by the user. If two zeros are entered, the program terminates.*/
#include <iostream>
#include <cstdlib>
using namespace std;
long multiply(long a, long b);
int main()
{
long value1 = 0, value2 = 0;
cout << "\nProgram to display two intergers, and the result ";
cout << "of multiplying them together." << endl;
for(long result = 1; result != 0; /* empty expression */)
{
value1 = value2 = result = 0;
cout << "\nTwo zeros will end the program." << endl;
cout << "Enter the first number: ";
cin >> value1;
cout <<"Endter the second number: ";
cin >> value2;
if(value1 == 0 && value2 == 0)
break;
result = multiply(value1, value2);
cout << value1 << " multiplied by ";
cout << value2 << " = " << result << endl;
}
system("PAUSE");
return 0;
}
long multiply(long a, long b)
{
long result = a * b;
return(result);
}
Now my question comss to the first line of code after the for loop.
it sets value1 = value2 = result = 0;
But didn't I already set value1 and value2 to 0 at the start of the program?
and isn't in the for loop result set to 0?
When I took that specific section of code out of the program, it worked fine...so why do I need it? Is it good practice to 'make sure' the value's of the variables are set to 0?
Any help is thanked in advance!
:D
/EDIT/ I also am wondering, how do I put code in a message on Gamedev and put it in that little box with the scroll bar, to make it look neater, and tidier, and so it copies correcly and has the colors highlighting specific sections of the code (ie comments, int, long, etc..)? /EDIT/
Of course that''s just my opinion, I could be wrong.
-)(-Dennis Miller-)(-
[edited by - Drevay on March 17, 2003 12:06:32 PM]
quote:/EDIT/ I also am wondering, how do I put code in a message on Gamedev and put it in that little box with the scroll bar, to make it look neater, and tidier, and so it copies correcly and has the colors highlighting specific sections of the code (ie comments, int, long, etc..)? /EDIT/
You'd think that would be a Frequently Asked Question, wouldn't you? If not, it is!
[edited by - petewood on March 17, 2003 12:31:25 PM]
Hmmmm....
/me rubs chin
Well... The exercise was used to teach about functions using loops and such, .... didn't you forget to put the for loop in there?
It's supposed to keep looping untio 2 zero's are entered.
Do you suggest any GOOD books on C++ then?
?
/EDIT/ Just a test....sorry /EDIT/
Of course that's just my opinion, I could be wrong.
-)(-Dennis Miller-)(-
[edited by - Drevay on March 17, 2003 1:26:43 PM]
[edited by - Drevay on March 17, 2003 1:27:28 PM]
/me rubs chin
Well... The exercise was used to teach about functions using loops and such, .... didn't you forget to put the for loop in there?
It's supposed to keep looping untio 2 zero's are entered.
Do you suggest any GOOD books on C++ then?
?
#include <iostream>using namespace std;int main(){ cout << "hello world, this is a test :)" << endl ; return 0;}
/EDIT/ Just a test....sorry /EDIT/
Of course that's just my opinion, I could be wrong.
-)(-Dennis Miller-)(-
[edited by - Drevay on March 17, 2003 1:26:43 PM]
[edited by - Drevay on March 17, 2003 1:27:28 PM]
generally speaking, and this might be a personal coding style, if my loop keeps going until a certain event happens, it''s better to use a while loop. If a loop repeats for a (more or less) predetermined number of cycles, it''s better to use a for loop. Anyways, i''m sure this loop is just to teach some principles of how a for loop works. The thing that''s really bothering me is that it seems like it will quit if only a single 0 is entered. if you entered 0 and 55, result would be 0 and the for loop would quit. but maybe i''m missing something.
I''m sure it''s just to teach the principles of a for loop and a function at the same time...
hmmm....
Of course that''''s just my opinion, I could be wrong.
-)(-Dennis Miller-)(-
hmmm....
Of course that''''s just my opinion, I could be wrong.
-)(-Dennis Miller-)(-
Well, why don''t you just make a bool, often called done or some such, initialize it to false, and make the loop run until done.
This would completely eliminate the types of errors in the code related to the fact that in multiplication 0 * anything == 0, therefore you will exit when any 1 zero is typed ...
It just seems that too many people are always trying to use little shortcuts to computer things ... express your exit conditions fully such as:
// test for exit conditions
if( value1 == 0 && value2 == 0)
done = true;
and in general, this habit will serve you well.
This would completely eliminate the types of errors in the code related to the fact that in multiplication 0 * anything == 0, therefore you will exit when any 1 zero is typed ...
It just seems that too many people are always trying to use little shortcuts to computer things ... express your exit conditions fully such as:
// test for exit conditions
if( value1 == 0 && value2 == 0)
done = true;
and in general, this habit will serve you well.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement