943,884 Members | Top Members by Rank

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

Re: Loop/Function errors

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.

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;
  28. cout << "Would you like to continue? If so enter another two numbers, if not enter 0" << endl ;
  29. cin >> x >> y ;
  30. }
  31.  
  32. }
Last edited by cout<<"alias"; Nov 25th, 2008 at 6:05 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 2008
Nov 25th, 2008
1

Re: Loop/Function errors

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 z = add(x, y); 
    cout << "The result is " << z;  
    cout << "\nWould you like to continue? If so enter another two numbers, if not enter 0" << endl;
    cin >> x >> y ; 
  }
}
Last edited by William Hemsworth; Nov 25th, 2008 at 6:42 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

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

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

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

Re: Loop/Function errors

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.

c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int add(int x, int y)
  6. {
  7. int r ;
  8. r = x+y ;
  9. return (r) ;
  10. }
  11.  
  12. int sub(int x, int y)
  13. {
  14. int w ;
  15. w = x-y;
  16. return (w) ;
  17. }
  18.  
  19. int mult(int x, int y)
  20. {
  21. int b;
  22. b = x*y ;
  23. return (b) ;
  24. }
  25.  
  26. int div(int x, int y)
  27. {
  28. int q;
  29. q = x/y;
  30. return (q);
  31. }
  32.  
  33.  
  34. int main()
  35. {
  36. int x,y,z;
  37.  
  38. cout << "Welcome to the Functions program! To exit at anytime enter 0." << endl ;
  39. cout << "Input two numbers." << endl;
  40. cin >> x >> y ;
  41. cout << "Now enter 1 to add, 2 to subtract, 3 to multipy, 4 to divide" << endl ;
  42. cin >> z ;

Trying to decide where to go from here...
Last edited by cout<<"alias"; Nov 26th, 2008 at 12:05 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 2008
Nov 26th, 2008
0

Re: Loop/Function errors

Use a switch statement, it should look along the lines of..
C++ Syntax (Toggle Plain Text)
  1. switch ( z ) {
  2. case 1: // Add
  3. {
  4. }
  5. break;
  6. case 2: // Subtract
  7. {
  8. }
  9. break;
  10. case 3: // Multiply
  11. {
  12. }
  13. break;
  14. case 4: // Divide
  15. {
  16. }
  17. break;
  18. }
You will have to understand and add most of the functionality yourself
For more help on this topic.
Last edited by William Hemsworth; Nov 26th, 2008 at 12:23 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Nov 26th, 2008
0

Re: Loop/Function errors

Alright this is what i put together

c++ Syntax (Toggle Plain Text)
  1. switch ( z )
  2. {
  3. case 1: //add
  4. sum = add(x,y);
  5. cout << "Your sum is: " << sum << endl;
  6. break;
  7. case 2: // Subtract
  8. sum = sub(x,y);
  9. cout << "Your sum is: " << sum << endl;
  10. break;
  11. case 3: // Multiply
  12. sum = mult(x,y);
  13. cout << "Your sum is: " << sum << endl;
  14. break;
  15. case 4: // Divide
  16. sum = divi(x,y);
  17. cout << "Your sum is: " << sum << endl;
  18. break;
  19.  
  20. }
  21.  
  22. while (x != 0 || y != 0)
  23. {
  24. cout << "Would you like to continue? If so enter two more numbers." << endl;
  25. cout << "Otherwise enter 0's." << endl ;
  26. cin >> x >> y ;
  27. cout << "Which Operation?" << endl ;
  28. cin >> z ;
  29. }
  30.  
  31. }

It gets hung up on the while loop though, trying to figure out how to loop it but no luck so far.
Last edited by cout<<"alias"; Nov 26th, 2008 at 4:09 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 2008
Nov 26th, 2008
0

Re: Loop/Function errors

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:
C++ Syntax (Toggle Plain Text)
  1. do
  2. {
  3. get option ( 1,2,3,4 or 0 to quit)
  4. if( option not 0 )
  5. {
  6. get inputs (x and y)
  7.  
  8. switch( option )
  9. {
  10. //cases 1-4 as you've done
  11. default: //bad input message
  12. }
  13. }
  14. }while ( option not 0 );
Last edited by vmanes; Nov 26th, 2008 at 4:56 pm.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 26th, 2008
0

Re: Loop/Function errors

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


Click to Expand / Collapse  Quote originally posted by vmanes ...
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:
C++ Syntax (Toggle Plain Text)
  1. do
  2. {
  3. get option ( 1,2,3,4 or 0 to quit)
  4. if( option not 0 )
  5. {
  6. get inputs (x and y)
  7.  
  8. switch( option )
  9. {
  10. //cases 1-4 as you've done
  11. default: //bad input message
  12. }
  13. }
  14. }while ( option not 0 );
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cout<<"alias" is offline Offline
16 posts
since Nov 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