| | |
c++ functions to compare numbers
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
C++ Syntax (Toggle Plain Text)
#include <iomanip> #include <cmath> #include <fstream> #include <string> #include<string> #include<iostream> using namespace std; int findbig(int num1, int num2); { return num1 > num2 ? num1 : num2; } void getnumbers ( int & num1, int & num2 ) { cout<<"Please enter two numbers to compare: "; cin>> num1 >> num2; } int main() { int num1, num2; getnumbers ( num1, num2 ); cout<< findbig ( num1, num2 ) <<" is the larger of "<< num1<<" and " <<num2<<"\n"; } return 0; }
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;
}
}
#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.
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.
The errors:
C++ Syntax (Toggle Plain Text)
#include <iomanip> #include <cmath> #include <fstream> #include <string> #include<string> #include<iostream> using namespace std; void getnumbers(int *num1, int *num2) { cout<<"Please enter two numbers to compare "<<endl; cin>>*num1>>*num2; cout<<"The two numbers entered in order were "<<num1<<" and "<<num2<<endl; } int findbig(int num1, int num2) { if (num1 < num2) { return -1; } else if (num1 > num2) { return 1; } } 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"; } cout<<"Go again?(y/n): "; cin>>again; } else { return 0; } }
C++ Syntax (Toggle Plain Text)
1>------ Build started: Project: bigone, Configuration: Debug Win32 ------ 1>Compiling... 1>bigone.cpp 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(42) : error C2143: syntax error : missing ';' before 'if' 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(44) : error C2143: syntax error : missing ';' before 'string' 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(48) : error C2146: syntax error : missing ';' before identifier 'endl' 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(48) : error C2143: syntax error : missing ';' before 'string' 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(48) : warning C4551: function call missing argument list 1>c:\users\documents\visual studio 2005\projects\bigone\bigone\bigone.cpp(53) : error C2181: illegal else without matching if 1>Build log was saved at "file://c:\Users\Documents\Visual Studio 2005\Projects\bigone\bigone\Debug\BuildLog.htm" 1>bigone - 5 error(s), 1 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You have a wayward closing brace here:
>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:
>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.
An else statement cannot exist without a matching if statement.
C++ Syntax (Toggle Plain Text)
cout<< findbig ( num1, num2 ) <<" is the larger of "<< num1<<" and " <<num2<<"\n"; }
>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)
if ( num1 > num2 ) return num1; else return num2;
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)
else { return 0; }
New members chased away this month: 3
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.
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.
![]() |
Similar Threads
- VB 2005 Trigonometric Functions (VB.NET)
- perfect numbers in two function (C++)
- c project (C++)
- calculate the execution time of a program (C)
- comparing two integers (Java)
- Base converter (C++)
- Converting Hexadecimal characters to integers. (C++)
- Applet Help (Java)
Other Threads in the C++ Forum
- Previous Thread: Solution for Big Integer used Linked - List.
- Next Thread: Participating in projects
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






