> while (x || y != 00) ;
1. Remove the ; at the end
2. This isn't basic. If you want to compare both with 0, then do
while (x != 0 || y != 0)
> z = (x,y);
Meaning what?
z = y;
is what you've written.
z is int, y is double, so that's where the first problem is.
> int (int x, int y) ;
Go back to your book and study how functions are declared.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
while (x || y != 00) ;
You cannot compare in that fashion and get the result you want. What your statement says is while ( x OR y ) not equal to 0 which compares the boolean result of ORing x and y, comparing that result to 0. To do what you want (continue while neither x nor y is a 0) try
while ( x != 0 && y != 0 )
{
//do stuff here
}
This will continue to execute until one or the other of x and y is assigned 0.
Also note that the semicolon following your while statement becomes the action it execute - an empty statement.
Your adding function needs a name, and you need to use that name where you execute the function (line 19 )
And, you need to either place the adding function before main( ), or put a prototype for the function before main( ). The compiler needs to see the function before you can use it.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
When things get this bad, honestly, just start over.
http://www.cplusplus.com/doc/tutorial/
Understand how to use functions and make simple expressions, also, keep your code well formatted. Here is how your current code would look well formatted, but all the mistakes are still there.
#include<iostream>
using namespace std;
int add(int x, int y) // your missing something here..
int main ()
{
int x, y;
cout << "Welcome to the Functions Program! To quit enter 00." << endl;
cout << "Input two numbers." << endl;
cin >> x, y;
while (x != 00 || y != 00) // Padding with extra 0 changes the number base to octal, which I doubt you need here
{
int z;
z = add(x,y);
cout << "The result is " << z;
return 0; // Whats the point in the while loop, if this is here
}
int add(int x, int y)
{
return (x+y);
}
}
I have highlighted your mistakes, see if you can fix them.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
A good effort :) (except for some of the formatting)
On line 23, would it be better to just replace that while with an if, as that way it will not run into an endless loop printing the result?
Change it from this:
while (x != 0 || y != 0) {
to: if (x != 0 || y != 0) {
Hope this helps.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
If you want the program to ask for multiple inputs, you need to re-prompt and re-accept input inside the while loop.
Try putting it right after you display an answer.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
Woo! you did it without anybody writing the code for you.. (it's a nice change)
but, here are just a few final touches to it.. along with formatting it for you (for the second time)
#include <iostream>
using namespace std;
int add(int x, int y) {
int r;
r = x + y;
return(r);
}
int main () {
int x, y;
cout << "Welcome to the Functions Program! To quit enter 0." << endl;
cout << "Input two numbers." << endl;
cin >> x >> y;
while (x != 0 || y != 0) {
int <strong>z = add(x, y);</strong>
cout << "The result is " << z;
cout << "<strong>\n</strong>Would you like to continue? If so enter another two numbers, if not enter 0" << endl;
cin >> x >> y ;
}
}
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
'\n' is simply equivalent to a newline, you could have easily replaced it outside the qoutes as endl as you have done with the other lines if you had wanted.. it was just easier to do that :icon_wink: .
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
Use a switch statement, it should look along the lines of..
switch ( z ) {
case 1: // Add
{
}
break;
case 2: // Subtract
{
}
break;
case 3: // Multiply
{
}
break;
case 4: // Divide
{
}
break;
}
You will have to understand and add most of the functionality yourself :) For more help on this topic .
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
Your while loop says "Stay here until the user enters a zero for both x and y. Even if I enter two zeroes, I still have to enter an operation option?
You really shouldn't have input values and stop or go value in the same block like that. For the kind of problem you're doing, a good structure might be something like:
do
{
get option ( 1,2,3,4 or 0 to quit)
if( option not 0 )
{
get inputs (x and y)
switch( option )
{
//cases 1-4 as you've done
default: //bad input message
}
}
}while ( option not 0 );
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228