943,791 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1336
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 25th, 2008
0

Loop/Function errors

Expand Post »
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

c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 2008
Nov 25th, 2008
0

Re: Loop/Function errors

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 25th, 2008
0

Re: Loop/Function errors

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 25th, 2008
0

Re: Loop/Function errors

Here..this is a function..
c++ Syntax (Toggle Plain Text)
  1. int f_i(int x,int y)
  2. {
  3. //...
  4. return (x+y);
  5. }
  6. // you meant
  7. // z = f_i(...,...)!!
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 25th, 2008
0

Re: Loop/Function errors

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?

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 2008
Nov 25th, 2008
2

Re: Loop/Function errors

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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Nov 25th, 2008
0

Re: Loop/Function errors

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 =/

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 2008
Nov 25th, 2008
0

Re: Loop/Function errors

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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Nov 25th, 2008
0

Re: Loop/Function errors

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 2008
Nov 25th, 2008
0

Re: Loop/Function errors

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help needed to use multimap
Next Thread in C++ Forum Timeline: Numeric Validation Query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC