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

Re: Loop/Function errors

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

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,428
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: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Loop/Function errors

 
1
  #12
Nov 25th, 2008
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.
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
  #13
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,428
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: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Loop/Function errors

 
0
  #14
Nov 25th, 2008
'\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 .
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
  #15
Nov 25th, 2008
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.
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
  #16
Nov 26th, 2008
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,428
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: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Loop/Function errors

 
0
  #17
Nov 26th, 2008
Use a switch statement, it should look along the lines of..
  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.
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
  #18
Nov 26th, 2008
Alright this is what i put together

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
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
  #19
Nov 26th, 2008
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:
  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.
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: 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
  #20
Nov 26th, 2008
Yep, thats what did it right there. Didnt even think about using a 'do-while' loop. Appreciate that, was becoming a headache.


Originally Posted by vmanes View Post
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:
  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 );
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC