Enter two integer and display the largest number using predefined function - c++

I already type the function but I can't get the output. Can anyone help me. It must use predefined function. My program is running but it have some errors so please help me.
#include <iostream>
using namespace std;
float largeNum (float a, float b)
{
float largeNum;
if(a>b)
cout<<"a is larger";
if(b>a)
cout<<"b is larger";
return (largeNum);
}
int main()
{
float num1, num2;
cout<<"Enter number";
cin>>num1;
cout<<"Enter number";
cin>>num2;
cout<<largeNum<< "is larger"<<endl;
return 0;
}

#include <iostream>
using namespace std;
float largeNum (float a, float b)
{
if(a>b)
return a;
return b;
}
int main()
{
float num1, num2;
cout<<"Enter number";
cin>>num1;
cout<<"Enter number";
cin>>num2;
cout<<largeNum(num1, num2)<< "is larger"<<endl;
return 0;
}

Try to change your code to below. You have not given any thing to largeNum, so you cannot return it.
#include <iostream>
using namespace std;
float largeNum(float a, float b)
{
if (a > b)
cout << "a is larger";
if (b > a)
cout << "b is larger";
return 0;
}
int main()
{
float num1, num2;
cout << "Enter number";
cin >> num1;
cout << "Enter number";
cin >> num2;
largeNum(num1, num2);
getchar();
getchar();
return 0;
}

#include <iostream>
using namespace std;
void largeNum(float a, float b)
{
if (a > b)
{
cout << a << " larger";
}
if (b > a)
{
cout << b << " larger";
}
}
int main()
{
float num1, num2;
cout << "Enter number";
cin >> num1;
cout << "Enter number";
cin >> num2;
largeNum(num1,num2);
return 0;
}
in your code you just define the function but you are not calling this function main so thats why u get error

Related

How to add 2 user input values using OOP in C++ and print with showDetails()

#include <iostream>
using namespace std;
class Arithmetic{
private:
int x;
int y;
public:
Arithmetic(int num1, int num2){
x = num1;
y = num2;
}
Arithmetic(){
}
~Arithmetic(){
}
int getNum1(){
return x;
}
int getNum2(){
return y;
}
int add(){
return getNum1() + getNum2();
}
void showDetails(){
cout << add();
}
};
int main(){
Arithmetic a;
int num1, num2;
cout << "Enter Num1: ";
cin >> num1;
cout << "Enter Num2: ";
cin >> num2;
a.showDetails();
return 0;
}
I've been tasked to add 2 user input values using object-oriented programming in C++. I've been trying to use cin inside the main and outside the main but for some reason it won't work. Plus I've been told to not use setters and only getters. And for some reason, I still don't understand how to print showDetails().
P.S. I have no experience in C++ only in Java.
First, a mini-code review:
#include <iostream>
using namespace std; // Bad practice
class Arithmetic{
private:
int x; // Prefer default member initialization
int y;
public:
Arithmetic(int num1, int num2){
// Prefer using the initialization section
x = num1;
y = num2;
}
Arithmetic(){
// Should be defaulted and paired with default member initialization
}
~Arithmetic(){ // Unnecessary; Rule of Zero
}
int getNum1(){ // Should be const
return x;
}
int getNum2(){ // Should be const
return y;
}
int add(){
// No need to invoke getters, and should be const
return getNum1() + getNum2();
}
void showDetails(){
cout << add();
}
};
int main(){
Arithmetic a;
int num1, num2;
cout << "Enter Num1: ";
cin >> num1; // Poor formatting
cout << "Enter Num2: ";
cin >> num2;
// This is where your issue occurs. You never put the data into the object.
// Declare 'a' here, and use your parameterized constructor.
a.showDetails();
return 0;
}
Your issue is order of operations for the most part.
#include <iostream>
class Arithmetic {
private:
int x = 0;
int y = 0;
public:
Arithmetic(int num1, int num2) : x(num1), y(num2) {}
Arithmetic() = default;
int getNum1() const { return x; }
int getNum2() const { return y; }
int add() const { return x + y; }
void showDetails() const { std::cout << add(); }
};
int main() {
int num1;
int num2;
std::cout << "Enter Num1: ";
cin >> num1;
std::cout << "Enter Num2: ";
cin >> num2;
Arithmetic a(num1, num2);
a.showDetails();
return 0;
}
Output:
❯ ./a.out
Enter Num1: 5
Enter Num2: 7
12
I left the getters in, but for the question as asked, they could be deleted.

Program works but when input is invalid it keeps looping c++

#include "stdafx.h"
#include <iostream>
#include <stdexcept>
using namespace std;
inline double multplication(double num1, double num2);
inline double division(double num1, double num2);
inline double addition(double num1, double num2);
inline double substraction(double num1, double num2);
int main()
{
cout << "Welcome to 'The calculator' just enter a number the operator '*, /, + and -' then the second number enjoy" << endl;
char redo = 'r'; double result = 0; int cnt = 0;
while (redo == 'r' || redo == 'R')
{
try
{
if (cnt > 0)
cout << "Ok same rules as before" << endl;
int num1 = 0, num2 = 0; char inputedOperator = '*';
cin >> num1 >> inputedOperator >> num2;
switch (inputedOperator)
{
case('*') :
result = multplication(num1, num2);
break;
case('/') :
result = division(num1, num2);
break;
case('+') :
result = addition(num1, num2);
break;
case('-') :
result = substraction(num1, num2);
break;
default:
throw runtime_error("Invalid operator");
}
}
catch (runtime_error err)
{
cout << err.what() << endl;
cout << "Enter y to redo or anykey to exit" << endl;
cin >> redo;
++cnt;
continue;
}
cout << "The answer is " << result << "\nPlease enter y to redo or anykey to exit" << endl;
cin >> redo;
++cnt;
}
cout << "Thanks for using" << endl;
return 0;
}
inline double multplication(double num1, double num2)
{
return num1 * num2;
}
inline double division(double num1, double num2)
{
return num1 / num2;
}
inline double addition(double num1, double num2)
{
return num1 + num2;
}
inline double substraction(double num1, double num2)
{
return num1 - num2;
}
Whenever I run '3 * 2' as the input it works fine but for '3 * d' it keeps looping at the end of my code also '3 d 3' is fine since it just sais invalid operator. So my question is why does it keep looping and how do I fix it?
added
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<int>::max(), '\n');
}
To clear the invalid input and buffer. Found info on cplusplus.com/forum/beginner/2957/

How to start from beginning of the program

I am a very beginner in c++. i am just learning abc of this language.. i created this small program that would add:
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
system("pause");
}
this above code is my very first usable application of c++
now i wanted that when some one wants to again add then this program starts again... i thoughts of using loops, i but cannot think how to use in such a way. i mean what conditions i should use.
please tell me
thanks.
Here is how we do :
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
while(true)
{
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\nSecond number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
char ch = 'n';
cout << "Start Again, [y/n] ? ";
cin >> ch;
if (ch == 'Y' || ch == 'y')
continue;
else
break;
}
return 0;
}
If we were to take "start from the beginning" literally, we can call main() again when we get to the end!
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
system("pause");
return main(); // <---- starts again from beginning of main()!!
}
(This will eventually crash when the program runs out of stack space, but the user will almost certainly get tired of adding numbers long before then. Of course, a clever compiler would realize this is tail recursion, and use a goto instead of a function call.)
You can try putting the main part of your 'adding' in an endless loop.
I suggest use a post condition loop, meaning one that will execute it's body at least once (then it will check the condition and so on), because you'll be wanting to add some numbers at least once.
Example:
do {
// do stuff here
} while (true) // always true condition -> makes the loop infinite
So I guess you'll ask how do you stop this. You can ask the user if he wants to continue.
Add this to the loop's body:
int lock = 0;
cout << "Do you want to continue? (0 = no, 1 = yes)" << endl;
cin << lock;
if (lock == 0) break; // stops the loop immeadiately
You can do the same with lock being char with values 'y' or 'n'.
Since you are starting, I am going to suggest changing your code a little bit:
#include <iostream>
using namespace std;
float add(float a, float b)
{
return a+b;
}
// Function that does the core work.
void read_input_print_sum()
{
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
}
int main()
{
read_input_print_sum();
system("pause");
}
Now, you can add various methods to call the core function repeatedly. One has been suggested in the answer by Rakibul Hassan.
That can be implemented with:
int main()
{
while (true)
{
read_input_print_sum();
}
system("pause");
}
Another way: Ask the use whether they want do the work again.
bool getRepeat()
{
cout << "Do you want to repeat? (Y/N): ";
int yesno = cin.getc();
return ( yesno == 'Y' || yesno == 'y' );
}
int main()
{
bool repeat = true;
while (repeat)
{
read_input_print_sum();
repeat = getRepeat();
}
system("pause");
}
Another way: Ask the number of times they wish to repeat the computation before you start.
int main()
{
int N = 0;
cout << "How may times do you want to add numbers: ";
cin >> N;
for ( int i = 0; i <= N; ++i )
{
read_input_print_sum();
}
system("pause");
}
The following code will do that:
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
while( true ){
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
}
system("pause");
}
above code will run forever. If you want to give user a choice, then apply a loop break condition.
char repeat = 'y';
while( repeat == 'y'){
// do as previous
//.....
//finally give user a choice
cout<< "Do you want to repeat?(y/n):";
cin>> repeat;
}
system("pause");
}
Just call main(); again..
in your code will be like this:
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
main();
}

function overloading for displaying different number types

I'm trying to make a program with an overloaded function "display". So far I've been ok, but I seem to be stuck. I can't seem to figure out what I am doing wrong with my code here.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double small_num, large_num;
double display;//I think the problem is here!!!
cout <<"Please enter two number starting with the smallest, then enter the largest"<<endl;
cin >> small_num;
cin >> large_num;
display(small_num, large_num);
system("pause");
return 0;
}
void display(int num1, int num2)
{
cout << num1 <<" "<< num2 << endl;
return;
}
void display(double num1, int num2)
{
cout << num1 <<" "<< num2 << endl;
}
void display(int num1, double num2)
{
cout << num1 <<" "<< num2 << endl;
}
void display(double num1, double num2)
{
cout << num1 <<" "<< num2 << endl;
}
Remove double display;. This creates a new local variable named display, which conflicts with your function names.
And also either:
Put the display methods above main.
Leave their definitions where they are and declare them above main as follows:
void display(int num1, int num2);
void display(double num1, int num2);
void display(int num1, double num2);
void display(double num1, double num2);

Input equation in bisection method, C++

I have this code:
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
double f(double x);
double biseccion(double a, double b, double tolerancia, int maxiter);
int main()
{
double a, b, raiz;
double tolerancia=0.00000;
int maxiter=25;
cout << "Input begin of interval: ";
cin >> a;
cout << "Input end of interval: ";
cin >> b;
cout << "\n";
cout << " # de"<<"\n"<<"Iteration"<<"\t"<<" A"<<"\t"<<" B"<<"\t"<<" C"<<"\t"<<" f(c)"<<endl;
raiz=biseccion(a,b,tolerancia,maxiter);
cout << "\n";
cout << "The root is: "<< raiz <<endl;
return 0;
}
double f(double x)
{
return x*x*x-x-2;
}
double biseccion(double a, double b, double tolerancia, int maxiter)
{
double c;
int numiter=1;
do
{
c=(a+b)/2;
if(f(a)*f(c)<0)
{
b=c;
}
else
{
a=c;
}
cout<<" "<<numiter<<"\t"<<"\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<f(c)<<endl;
numiter++;
}
while((abs(f(c))>tolerancia)&&(numiter<maxiter));
return c;
}
Instead of writing "x*x*x-x-2" in my code, I want user to input it before asking for begin of interval. How can I do that?
I try using a variable to store the "x*x*x-x-2", but none work.
You need to parse the input, its probably not as easy as you are thinking but there are some libraries that can help you.
muparser.sourceforge.net/
code.google.com/p/expressionparser/
partow.net/programming/exprtk/index.html
here is also a solution in c# that might help you too.
Is there a string math evaluator in .NET?