| | |
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 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; }
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.
If getnumber() doen't ask for values but just shows them, it would look like this
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
#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"; }
I'm here to prove you wrong.
Thx for your prompt reply Narue, we're almost there, however, if I compile using your suggested format...i get the following error:
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(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 ==========
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:
>I'm thinking perhaps because we're using different compilers.
Nope, it looks like we're using the same one.
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:
C++ Syntax (Toggle Plain Text)
#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"; }
Nope, it looks like we're using the same one.
I'm here to prove you wrong.
![]() |
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 |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






. 