943,617 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 13677
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 11th, 2007
0

Re: c++ functions to compare numbers

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

I'd have loved to use your second option, but my professor is very particular about the format of his assignments...
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

A few questions narue...what exactly does the '?' do?
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

? 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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vincent551987vn is offline Offline
14 posts
since Oct 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

Ok thx for your input. still having some problems with the compilation.
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

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.
Reputation Points: 53
Solved Threads: 13
Junior Poster
hopalongcassidy is offline Offline
148 posts
since Oct 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

So complexity hi hi ^^. Very complex brain. There are 14 errors and 2 warnings. I only know this.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vincent551987vn is offline Offline
14 posts
since Oct 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

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.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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 ==========
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 11th, 2007
0

Re: c++ functions to compare numbers

You have a wayward closing brace here:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. else
  2. {
  3. return 0;
  4. }
An else statement cannot exist without a matching if statement.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 11th, 2007
0

Re: c++ functions to compare numbers

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.
Reputation Points: 53
Solved Threads: 13
Junior Poster
hopalongcassidy is offline Offline
148 posts
since Oct 2007

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: Solution for Big Integer used Linked - List.
Next Thread in C++ Forum Timeline: Participating in projects





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


Follow us on Twitter


© 2011 DaniWeb® LLC