c++ functions to compare numbers
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include<string>
#include<iostream>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 0;
char again = 'y';
while (again=='y')
{
cout<<"Please enter two numbers to compare "<<endl;
cin>>num1>>num2;
cout<<"The two numbers entered in order were "<<num1<<" and "<<num2<<endl;
if (num1<num2)
{
cout<<num2<<" is the larger of "<<num1<< " and "<<num2<<endl;
}
else if (num1>num2)
{
cout<<num1<<" is the larger of "<<num1<< " and "<<num2<<endl;
}
cout<<"\n Go again?(y/n): ";
cin>>again;
}
return 0;
}
Good day. My assignment is as follows: Write a c++ program using functions, that will accept 2 numbers from the keyboard and then determine which is
the larger and smaller of the two. There should be two functions:
1. getnumbers
Output within the 1st function should display
The two numbers entered in order were XXX and YYY
2. findbig
Output within the 2nd function should display
XXX is the larger of XXX and YYY
Put the main function within an "again" loop that will continue until the user no longer wants to.(skip a line between each sets output).
Could you please explain to me how i'd go about creating two such functions? I had a bit of problem grasping the concept of functions....thanks. The above program does work, but it doesn't have the two distinct functions as is requested.
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
If getnumber() doen't ask for values but just shows them, it would look like this
int findbig(int a, int b)
{
// will return -1, if a is greater
// 0 if both are equal
// 1 if b is greater
}
void getnumbers(int a, int b)
{
// your output
int x = findbig(a, b);
// check the values of x
// decide which is greater
}
vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
HHHmmmmm. I didn't quite get it. I still got problems. Below is my revised code:
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include<string>
#include<iostream>
using namespace std;
int num1, num2;
int findbig(int num1, int num2);
int main()
{
char again = 'y';
while (again=='y')
{
cout<<"Please enter two numbers to compare "<<endl;
cin>>num1>>num2;
cout<<"The two numbers entered in order were "<<num1<<" and "<<num2<<endl;
}
void getnumbers(int num1, int num2);
if (num1<num2)
int x = findbig(num1, num2);
cout<<num2<<" is the larger of "<<num1<< " and "<<num2<<endl;
else if (num1>num2)
{
cout<<num1<<" is the larger of "<<num1<< " and "<<num2<<endl;
}
cout<<"\n Go again?(y/n): ";
cin>>again;
return 0;
}
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
You're overcomplicating things, I think. Here's a sample without the "again" crap that should give you an idea of what's expected when functions get involved:
#include <iostream>
int findbig ( int num1, int num2 )
{
return num1 > num2 ? num1 : num2;
}
void getnumbers ( int& num1, int& num2 )
{
std::cout<<"Please enter two numbers to compare: ";
std::cin>> num1 >> num2;
}
int main()
{
int num1, num2;
getnumbers ( num1, num2 );
std::cout<< findbig ( num1, num2 ) <<" is larger\n";
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Thx for your prompt reply Narue, we're almost there, however, if I compile using your suggested format...i get the following error:
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(16) : error C2447: '{' : missing function header (old-style formal list?)
1>Build log was saved at "file://c:\Users\Documents\Visual Studio 2005\Projects\bigone\bigone\Debug\BuildLog.htm"
1>bigone - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
I'm thinking perhaps because we're using different compilers. Also, just a side-question. I'm aware that C++ is used to create programs like these and other tools that would be very repetitive for a human to do, such as certain calcualtions, can we create c++ programs to do other internal stuff? By that i mean, can you create a c++ program to shutdown your computer or to lauch internet explorer?
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
>if I compile using your suggested format...i get the following error:
You made a change that broke the code. Post what you're compiling so I can see where the error is.
>widenning for many cases that is there are many nums to compare to each others, how can you solve?
Load the numbers into a suitable data structure (I'd suggest a set), and then search for the smallest. For example:
#include <iostream>
#include <set>
int findbig ( std::set<int>& numbers )
{
return *numbers.rbegin();
}
void getnumbers ( std::set<int>& numbers )
{
int num;
std::cout<<"Please enter a list of numbers to compare: ";
while ( std::cin>> num )
numbers.insert ( num );
}
int main()
{
std::set<int> numbers;
getnumbers ( numbers );
std::cout<< findbig ( numbers ) <<" is larger\n";
}
>I'm thinking perhaps because we're using different compilers.
Nope, it looks like we're using the same one.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
#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;
}
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
I'd have loved to use your second option, but my professor is very particular about the format of his assignments...
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
A few questions narue...what exactly does the '?' do?
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
Ok thx for your input. still having some problems with the compilation.
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
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.
#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;
}
}
The errors:
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 ==========
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
You have a wayward closing brace here:
cout<< findbig ( num1, num2 ) <<" is the larger of "<< num1<<" and " <<num2<<"\n";
}
>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:
if ( num1 > num2 )
return num1;
else
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 " <cout<< num1 <<" is the larger of "<< num1<< " and " <else
{
return 0;
}
An else statement cannot exist without a matching if statement.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401