#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.

Recommended Answers

All 22 Replies

Member Avatar for GreenDay2001

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
}

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;
}

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";
}
commented: whoops..thanx for figuring out my folly, didn't noticed then, but my code was really crap +4

widenning for many cases that is there are many nums to compare to each others, how can you solve? we use this whether when the comparisors are and maybe more than two a little but this algorithm is not brief. you can use another algorithm. Use an indirect varia.

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 ==========

there is an mistake, this is, function Void not using return to back due to the recurs. U must change it into INT type.

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?

>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.

I have no imagination bout ur speech. but there is a thing i want to say, i have had both of compilers. :).

#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;
}

I'd have loved to use your second option, but my professor is very particular about the format of his assignments...

A few questions narue...what exactly does the '?' do?

? 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.

Ok thx for your input. still having some problems with the compilation.

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;
}
}

So complexity hi hi ^^. Very complex brain. There are 14 errors and 2 warnings. I only know this.

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 ==========

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 " <<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.

else 
{
  return 0;
}

An else statement cannot exist without a matching if statement.

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.

Member Avatar for iamthwee

You mean c++

Yeah, you're right. But all of the code except for the cout and cin stuff is just plain C. And whether it's C or C++ sort of misses the heart of the matter. The thing that the original poster needs to understand is how to code a function and how to pass variables by reference. Don't you agree?

Hoppy.

Ok thanks much again guys. Problem solved, i made a bit of modifications and i encountered a few syntax errors, but were taken care of. The program works like a charm. see you guys again soon, time to move on to my new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.