Hi there, been working on a small program that will loop a function which adds two integers until the user enters 00, but can't seem to work out the finer details. A little newer to C++, don't need a complete answer but any advice would be much appreciated

#include<iostream> 

using namespace std; 


int main () 
{ 

	double x, y ;

cout << "Welcome to the Functions Program! To quit enter 00." << endl ;
cout << "Input two numbers." << endl ;
cin >> x, y ;


while (x || y != 00) ;
	{
int z ; 
z = (x,y); 
cout << "The result is " << z; 

return 0 ; 
}


int (int x, int y) ;
{
 int r; r=x+y; 
return (r); 
} 

}

And i'm pulling in these errors;

documents\visual studio 2008\projects\functions\functions\source.cpp(19) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data

c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(26) : error C2144: syntax error : 'int' should be preceded by ')'

c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(26) : error C2059: syntax error : ')'

c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(28) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data

> 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.

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.

Here..this is a function..

int f_i(int x,int y)
   {
      //...
      return (x+y);
   }
   // you meant 
   // z = f_i(...,...)!!

Alright, i appreciate the advice here. I do need to get into the books more, even with reading i've had some trouble understanding this though and trail and error seems to be the only way to figure it out. I dont have a compiler handy since im at work, but does this more like what i was going for?

#include<iostream> 

 

using namespace std; 

 

 int add(int x, int y)


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) 

	{

int z ; 

z = add(x,y); 

cout << "The result is " << z; 

 

return 0 ; 

}

 

 
int add(int x, int y) 

{
 
return (x+y); 

} 

 

}

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.

commented: Whoo! =) +5
commented: Yeah, gotta watch those octal zeros, they're a lot smaller than the decimal equivalents ;) +23

Alright, this will be the last post until i can get to a compiler tonight. I believe i fixed most of what you mentioned, and i do appreciate the feedback. I know the last source post was kind of sloppy looking, at work and was paying attention to something else when i posted it =/

#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 z ; 
z = add(x,y); 
cout << "The result is " << z << endl ;  
 }

}

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.

Will the if statement allow the user to use the program over and over until they enter 0? This was my main objective with the program, that and incorporating functions. lol, and thank you its a rough road for getting the syntax and format perfect but im working on it. My only programming experience thus far besides Python.

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.

Alright, that makes sense. Here is what i have so far, when i get home going to try and compile it and see what happens. Thanks for all the input so far guys, i appreciate your time on this.

#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 z ; 
z = add(x,y); 
cout << "The result is " << z;  
cout << "Would you like to continue? If so enter another two numbers, if not enter 0" << endl ;
cin >> x >> y ; 
}

}

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 [B]z = add(x, y);[/B] 
    cout << "The result is " << z;  
    cout << "[B]\n[/B]Would you like to continue? If so enter another two numbers, if not enter 0" << endl;
    cin >> x >> y ; 
  }
}
commented: Yes! Whoo Indeed! O_O +5

I appreciate all the help, as a final thought do you mind i ask what /n does for the quotations? Ill look it up when i get home, but if noone minds hooking me up with an explanation to it i would appreciate it.

'\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: .

lol, ok i see what your talking about now. Forgot endl after output of z. Well good times, this really helped with my understanding of functions and loops. Took a few tries, but hopefully this will complie correctly.

Alright, the loop/function complied correctly thanks to everyones help. I wanted to see if anyone might have some ideas/feeback on this idea though. The original program was simply a while loop that called a function to add two integers. I tried to modify it so that there was a function for addition/subtraction/multiplication/division and the user could choose with operation they would like to perform by inputing a third operator that would call one of those functions. My question was would a while loop still suffice for this, or should i use other conditionals like 'if'? I couldnt get the loop to call any other function besides addition. Ill go ahead and post the first half the code so you can see what i have so far, any ideas are welcome.

#include<iostream>

using namespace std;

int add(int x, int y)
{
int r ;
r = x+y ;
return (r) ;
}

int sub(int x, int y)
{
int w ;
w = x-y;
return (w) ;
}

int mult(int x, int y)
{
int b;
b = x*y ;
return (b) ;
}

int div(int x, int y)
{
int q;
q = x/y;
return (q);
}


int main()
{
int x,y,z;

cout << "Welcome to the Functions program! To exit at anytime enter 0." << endl ;
cout << "Input two numbers." << endl;
cin >> x >> y ;
cout << "Now enter 1 to add, 2 to subtract, 3 to multipy, 4 to divide" << endl ;
cin >> z ;

Trying to decide where to go from here...

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.

Alright this is what i put together

switch ( z ) 
	 {
      case 1: //add
       sum = add(x,y);
       cout << "Your sum is: " << sum << endl;
       break;
      case 2: // Subtract
       sum = sub(x,y);
       cout << "Your sum is: " << sum << endl;
       break;
      case 3: // Multiply
       sum = mult(x,y);
       cout << "Your sum is: " << sum << endl;
       break;
      case 4: // Divide
       sum = divi(x,y);
       cout << "Your sum is: " << sum << endl;
       break;
	
	 }
      
	  while (x != 0 || y != 0)
	  {
	      cout << "Would you like to continue? If so enter two more numbers." << endl;
              cout << "Otherwise enter 0's." << endl ;
	      cin >> x >> y ;
	      cout << "Which Operation?" << endl ;
	      cin >> z ;
	  }

	 }

It gets hung up on the while loop though, trying to figure out how to loop it but no luck so far.

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 );

Yep, thats what did it right there. Didnt even think about using a 'do-while' loop. Appreciate that, was becoming a headache.

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 );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.