c++ functions to compare numbers

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

Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ functions to compare numbers

 
0
  #11
Oct 11th, 2007
  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <string>
  5. #include<string>
  6. #include<iostream>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int findbig(int num1, int num2);
  12.  
  13. {
  14. return num1 > num2 ? num1 : num2;
  15. }
  16. void getnumbers ( int & num1, int & num2 )
  17. {
  18. cout<<"Please enter two numbers to compare: ";
  19. cin>> num1 >> num2;
  20. }
  21.  
  22. int main()
  23. {
  24. int num1, num2;
  25.  
  26. getnumbers ( num1, num2 );
  27. cout<< findbig ( num1, num2 ) <<" is the larger of "<< num1<<" and " <<num2<<"\n";
  28. }
  29.  
  30. return 0;
  31. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ functions to compare numbers

 
0
  #12
Oct 11th, 2007
I'd have loved to use your second option, but my professor is very particular about the format of his assignments...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ functions to compare numbers

 
0
  #13
Oct 11th, 2007
A few questions narue...what exactly does the '?' do?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

Re: c++ functions to compare numbers

 
0
  #14
Oct 11th, 2007
? is a statement that following it, there is a choice where can choose the true for the first and the false for the last one else.
Say It Like It Is.
Có sao nói vậy.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ functions to compare numbers

 
0
  #15
Oct 11th, 2007
Ok thx for your input. still having some problems with the compilation.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 147
Reputation: hopalongcassidy is an unknown quantity at this point 
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: c++ functions to compare numbers

 
0
  #16
Oct 11th, 2007
My C is a little rusty, so I might not have gotten the syntax exactly right, but this should work.

#include <fstream>
#include <iostream>

/* Note: I don't think you need all of the #includes you had. I think that #include <stdio> would be all you need
*/

using namespace std;

int main()
{
int num1 = 0;
int num2 = 0;
char again = 'y';
while (again=='y')
{
getnumbers(&num1, &num2);
if (findbig(num1, num2) < 0)
{
cout<< num2 " is the larger of " num1 " and " num2 "\n";
} else if (findbig(num1, num2) > 0) {
cout<< num1 " is the larger of " num1 " and " num2 endl "\n";
} else {
cout<< "the numbers are the same.\n";
}
cout<<"Go again?(y/n): ";
cin>>again;
}
}

/*
Notice that getnumbers receives the addresses of two ints from the caller. Before returning, it fill in the values, pointed to by the addresses provided int the input parameters.
*/
void getnumbers(int *num1, int *num2)
{
cout<<"Please enter two numbers to compare "<<endl;
cin>>*num1>>*num2;
}

int getbig(int num1, int num2)
{
/* returns -1 if num1 < num2, 0 if equal, +1 if num1 > num2 */
if (num1 < num2) {
return -1;
} else if (num1 > num2) {
return 1;
} else {
return 0;
}
}
Last edited by hopalongcassidy; Oct 11th, 2007 at 4:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

Re: c++ functions to compare numbers

 
0
  #17
Oct 11th, 2007
So complexity hi hi ^^. Very complex brain. There are 14 errors and 2 warnings. I only know this.
Say It Like It Is.
Có sao nói vậy.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: c++ functions to compare numbers

 
0
  #18
Oct 11th, 2007
hopalongcassidy.....welcome aboard and thanks for your input. I made made a bit of modifcations, but seem to be having a little problem. We're almost there. I'm posting both my source code and also the errors that I get. Thanks.
  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <string>
  5. #include<string>
  6. #include<iostream>
  7.  
  8. using namespace std;
  9.  
  10. void getnumbers(int *num1, int *num2)
  11. {
  12. cout<<"Please enter two numbers to compare "<<endl;
  13. cin>>*num1>>*num2;
  14. cout<<"The two numbers entered in order were "<<num1<<" and "<<num2<<endl;
  15. }
  16.  
  17. int findbig(int num1, int num2)
  18. {
  19. if (num1 < num2)
  20. {
  21. return -1;
  22. }
  23. else if (num1 > num2)
  24. {
  25. return 1;
  26. }
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32. int num1 = 0;
  33. int num2 = 0;
  34. char again = 'y';
  35. while (again=='y')
  36. {
  37. getnumbers(&num1, &num2)
  38. if (findbig(num1, num2) < 0)
  39. {
  40. cout<< num2<< " is the larger of " <<num1<< " and "<< num2 "\n";
  41. }
  42. else if (findbig(num1, num2) > 0)
  43. {
  44. cout<< num1 <<" is the larger of "<< num1<< " and " <<num2 endl "\n";
  45. }
  46. cout<<"Go again?(y/n): ";
  47. cin>>again;
  48. }
  49. else
  50. {
  51. return 0;
  52. }
  53. }
The errors:
  1. 1>------ Build started: Project: bigone, Configuration: Debug Win32 ------
  2. 1>Compiling...
  3. 1>bigone.cpp
  4. 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(42) : error C2143: syntax error : missing ';' before 'if'
  5. 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(44) : error C2143: syntax error : missing ';' before 'string'
  6. 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(48) : error C2146: syntax error : missing ';' before identifier 'endl'
  7. 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(48) : error C2143: syntax error : missing ';' before 'string'
  8. 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(48) : warning C4551: function call missing argument list
  9. 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(53) : error C2181: illegal else without matching if
  10. 1>Build log was saved at "file://c:\Users\Documents\Visual Studio 2005\Projects\bigone\bigone\Debug\BuildLog.htm"
  11. 1>bigone - 5 error(s), 1 warning(s)
  12. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: c++ functions to compare numbers

 
0
  #19
Oct 11th, 2007
You have a wayward closing brace here:
  1. cout<< findbig ( num1, num2 ) <<" is the larger of "<< num1<<" and " <<num2<<"\n";
  2. }
>I'd have loved to use your second option, but my professor
>is very particular about the format of his assignments...
I didn't expect you to use it anyway. That was in response to another poster who asked how I might solve the problem for any number of values.

>A few questions narue...what exactly does the '?' do?
It's a conditional operator, roughly equivalent to this:
  1. if ( num1 > num2 )
  2. return num1;
  3. else
  4. return num2;
>but seem to be having a little problem.
Your problems always seem to be strictly syntax based. Programming is all about an eye for detail, C++ programming even more so. You need to understand the rules and follow them. Here are your mistakes. Try to learn what they are, and why you keep making them:

>getnumbers(&num1, &num2)
Semicolons are consistently required. Find out where, and don't forget them.

>cout<< num2<< " is the larger of " <<num1<< " and "<< num2 "\n";
Every << single << individual << item << is << separated << by << an << insertion << operator.

>cout<< num1 <<" is the larger of "<< num1<< " and " <<num2 endl "\n";
Ditto.
  1. else
  2. {
  3. return 0;
  4. }
An else statement cannot exist without a matching if statement.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 147
Reputation: hopalongcassidy is an unknown quantity at this point 
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: c++ functions to compare numbers

 
0
  #20
Oct 11th, 2007
I told you that my C is a little rusty. It looks like I should have put a ; after the call to getnumbers() and removed the endl. If you want to make those changes and try it again, it should reduce the number of error messages from the compiler.

I don't have a C compiler on the machine I'm using right now or I'd verify it myself.
Last edited by hopalongcassidy; Oct 11th, 2007 at 5:46 pm.
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