Loop/Function errors

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 16
Reputation: cout<<"alias" is an unknown quantity at this point 
Solved Threads: 1
cout<<"alias" cout<<"alias" is offline Offline
Newbie Poster

Loop/Function errors

 
0
  #1
Nov 25th, 2008
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

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main ()
  7. {
  8.  
  9. double x, y ;
  10.  
  11. cout << "Welcome to the Functions Program! To quit enter 00." << endl ;
  12. cout << "Input two numbers." << endl ;
  13. cin >> x, y ;
  14.  
  15.  
  16. while (x || y != 00) ;
  17. {
  18. int z ;
  19. z = (x,y);
  20. cout << "The result is " << z;
  21.  
  22. return 0 ;
  23. }
  24.  
  25.  
  26. int (int x, int y) ;
  27. {
  28. int r; r=x+y;
  29. return (r);
  30. }
  31.  
  32. }

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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Loop/Function errors

 
0
  #2
Nov 25th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Loop/Function errors

 
0
  #3
Nov 25th, 2008
  1. 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
  1. while ( x != 0 && y != 0 )
  2. {
  3. //do stuff here
  4. }
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Loop/Function errors

 
0
  #4
Nov 25th, 2008
Here..this is a function..
  1. int f_i(int x,int y)
  2. {
  3. //...
  4. return (x+y);
  5. }
  6. // you meant
  7. // z = f_i(...,...)!!
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: cout<<"alias" is an unknown quantity at this point 
Solved Threads: 1
cout<<"alias" cout<<"alias" is offline Offline
Newbie Poster

Re: Loop/Function errors

 
0
  #5
Nov 25th, 2008
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?

  1. #include<iostream>
  2.  
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. int add(int x, int y)
  10.  
  11.  
  12. int main ()
  13.  
  14. {
  15.  
  16.  
  17.  
  18. int x, y ;
  19.  
  20.  
  21.  
  22. cout << "Welcome to the Functions Program! To quit enter 00." << endl ;
  23.  
  24. cout << "Input two numbers." << endl ;
  25.  
  26. cin >> x, y ;
  27.  
  28.  
  29.  
  30.  
  31.  
  32. while (x != 00 || y != 00)
  33.  
  34. {
  35.  
  36. int z ;
  37.  
  38. z = add(x,y);
  39.  
  40. cout << "The result is " << z;
  41.  
  42.  
  43.  
  44. return 0 ;
  45.  
  46. }
  47.  
  48.  
  49.  
  50.  
  51. int add(int x, int y)
  52.  
  53. {
  54.  
  55. return (x+y);
  56.  
  57. }
  58.  
  59.  
  60.  
  61. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,486
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Loop/Function errors

 
2
  #6
Nov 25th, 2008
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.
Last edited by William Hemsworth; Nov 25th, 2008 at 2:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: cout<<"alias" is an unknown quantity at this point 
Solved Threads: 1
cout<<"alias" cout<<"alias" is offline Offline
Newbie Poster

Re: Loop/Function errors

 
0
  #7
Nov 25th, 2008
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 =/

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int add(int x, int y)
  7. {
  8. int r;
  9. r = x+y;
  10. return (r);
  11. }
  12.  
  13.  
  14. int main ()
  15. {
  16. int x, y ;
  17.  
  18. cout << "Welcome to the Functions Program! To quit enter 0." << endl ;
  19. cout << "Input two numbers." << endl ;
  20. cin >> x >> y ;
  21.  
  22.  
  23. while (x != 0 || y != 0)
  24. {
  25. int z ;
  26. z = add(x,y);
  27. cout << "The result is " << z << endl ;
  28. }
  29.  
  30. }
Last edited by cout<<"alias"; Nov 25th, 2008 at 3:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,486
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Loop/Function errors

 
0
  #8
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: cout<<"alias" is an unknown quantity at this point 
Solved Threads: 1
cout<<"alias" cout<<"alias" is offline Offline
Newbie Poster

Re: Loop/Function errors

 
0
  #9
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 631
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 102
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Loop/Function errors

 
0
  #10
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC