#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b){
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
} else
{
c=0;
}
return c;
}
int main(){
int c;
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
cout<<endl;
}
error in finding the mod of two numbers and not compiling the program :
error in finding the mod of two numbers and not compiling the program
It compiles for me
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
} else {
c=0;
}
return c;
}
int main(){
int c;
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
fun_div(e,d);
cout<<endl;
}
You should put the error message when asking about compilation errors. However I copied your code exactly and it compiles.
The other thing is that you don't call your function so I added that as well.
And as a side note, you could just do
int fun_div(int a, int b)
{
return (a%b == 0);
}
because (a%b == 0) will evaluate to 1 if a is a multiple of b and 0 otherwise.
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
}
else
{ c=0; }
return c;
}
int main()
{
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
c=fun_div(e,d);
cout<<endl;
}
Try this. I think this is what you expected.
Please explain your question to get a more specific answer.
I have added a function call to the function fun_div.
you should probably add one more check for which is greater.. the greater check will ensure that there is a proper remainder available
Well the main problem in your code is that you are not calling the function which you defined, that is why you are not getting the desired result, And there are some better practices for writing code which you should follow to avoid errors in future.
Don't use global variable and if you are returning the result from function then show on screen from main function.
Recommended Code is given below, i have changed the function so it will only check that 'a' is divisible by 'b' or not and return the value to main so it will show the result on screen.
#include<iostream>
using namespace std;
int fun_div(int a, int b)
{
return (a%b == 0);
}
int main() {
int e, d;
cout << "enter two values : ";
cin >> e >> d;
if (fun_div(e, d))
{
cout << "Solution Exists.";
}
else
{
cout << "No Solution Exists.";
}
cout << endl;
return 0;
}
Related
This is my Unary operator.cpp file
#include <iostream>
using namespace std;
class space{
int x;
int y;
int z;
public:
void getdata(int a, int b, int c){
x=a;
y=b;
z=c;
}
void display(){
cout<<x<<" ";
cout<<y<<" ";
cout<<z<<"\n";
}
void operator-(){
x=-x;
y=-y;
z=-z;
}
};
int main(int argc, char *argv[]){
space S;
S.getdata(10,-20,30);
cout<<"S: ";
S.display();
-S;
S.display();
return 0;
}
This is my code for Overloading Unary Operator. When I compile this code in VS Code this appears in debug console Input first number: =thread-exited,id="2",group-id="i1" sometimes. And sometime code compiles successfully by showing this message The program 'e:\C++\Unary operator.exe' has exited with code 0 (0x00000000)
. But when I run it in Code Blocks It compiles and output appears as expected. I think there is something wrong with VS code. Please help.
#include <bits/stdc++.h>
using namespace std;
struct stu {
int n;
stu(int _n = 0):n(_n) { }
int add(int a, int b = n-1) {
return a + b;
}
};
int main() {
stu obj = stu(5);
cout << obj.add(10) << endl;
}
The compiler shows the message " invalid use of
non-static data member 'stu::n' ".
What is wrong with this code. Any help would be great.
Thanks.
You can't use default arguments this way. Consider writing two separate functions:
struct stu {
int n;
int add(int a, int b) { return a + b; }
int add(int a) { return a + n - 1; }
}
#include<iostream>
using namespace std;
class ulam
{
int num;
double prod;
int cot;
public:
ulam(){cot=0;}
ulam(int x)
{
num=x;
}
void process()
{
for(int i=0;num==1;i++)
{
cout<<num<<endl;
if((num%2) == 0)
{
prod=num/2;
}
else
{
prod=(3*num)+1;
}
num=prod;
cot++;
}
}
void display()
{
cout<<"the number of steps required is: "<<cot;
}
};
int main()
{
int n;
cout<<"enter the number"<<endl;
cin>>n;
ulam obj(n);
obj.process();
obj.display();
}
when i write this code the cot value comes as a garbage value i think. i cant figure out where i went wrong. i used class because i feel it is more discriptive . but the main aim behind this program is to find the number of steps it is required for a number to reach one and to print the whole sequence of numbers. for thos who dont know about the collatz conjecture https://en.wikipedia.org/wiki/Collatz_conjecture
Your condition of the for loop inside process function is wrong. it should be num!=1. You need to initialize cot too. You don't need prod actually.
#include<iostream>
using namespace std;
class ulam
{
int num;
int cot;
public:
ulam()
{
cot=0;
}
ulam(int x)
{
cot=0;
num=x;
}
void process()
{
for(int i=0;num!=1;i++)
{
cout<<num<<endl;
if((num%2) == 0)
{
num=num/2;
}
else
{
num=(3*num)+1;
}
cot++;
}
}
void display()
{
cout<<"the number of steps required is: "<<cot;
}
};
int main()
{
int n;
cout<<"enter the number"<<endl;
cin>>n;
ulam obj(n);
obj.process();
obj.display();
return 0;
}
First Problem
In the constructor where you initialize when an integer is passed, you ALSO have to initialize cot like this:
ulam(int x)
{
cot = 0;
num = x;
}
Better yet, since cot is always going to be 0 at construction, just set cot to 0 as a private variable like this:
class ulam
{
int num;
double prod;
int cot = 0;
public:
//....
};
This means that you could still have a default constructor that will do nothing, and the one that takes an integer won't require cot to be set to 0.
Second Problem
Your second problem is that the loop condition is wrong. It should be num != 1, not num == 1. num == 1 would be the loop would never run unless 1 was passed in cin.
I am new to C++, I have been doing tasks for training. This task was to make a calculation while using class and accessing private integer.
here is my full code.
#include <iostream>
using namespace std;
class Calculatour{
public:
int SumNum(int a, int b){
cin >> a;
cin >> b;
x = a+b;
return x;
}
private:
int x;
};
int main() {
Calculatour ADD;
cout << ADD.SumNum;
return 0;
}
I have been getting an error on this line:
cout << ADD.SumNum;
Where it says
reference to non-static member function must be called ADD calculatour using classes.cpp /ADD calculatour using classes/src line 37 C/C++ Problem.
Also I have been getting this error too:
make: *** [src/ADD calculatour using classes.o] Error 1 ADD calculatour using classes C/C++ Problem
Please consider that i am new to the language. So if you can provide a solution and an explanation this would be really helpful.
Thanks
When invoking a method (or calling a function) with parameters one must supply the parameters even if they are not used. Since in this method you have no intention of using the parameters for anything and have no class hierarchy that forces you to include these parameters, you may as well discard them.
#include <iostream>
using namespace std;
class Calculatour
{
public:
int SumNum()
{
int a;
int b;
cin >> a;
cin >> b;
x = a + b;
return x;
}
private:
int x;
};
int main()
{
Calculatour ADD;
cout << ADD.SumNum();
return 0;
}
The more ideologically correct solution (the Calculator class should do Calculator things, not Data In/Out things) would be to read a and b in in main, and then call SumNum.
#include <iostream>
using namespace std;
class Calculatour
{
public:
int SumNum(int a, int b)
{
x = a + b;
return x;
}
int SumNum(int a) // takes advantage of the stored x value
{
x += a;
return x;
}
private:
int x = 0;
};
int main()
{
Calculatour ADD;
int a;
int b;
cin >> a;
cin >> b;
cout << ADD.SumNum(a, b);
cout << ADD.SumNum(a);
return 0;
}
There is no way for the compiler to tell
int SumNum()
{
int a;
int b;
cin >> a;
cin >> b;
x = a + b;
return x;
}
from
int SumNum()
{
int a;
cin >> a;
x += a;
return x;
}
so you cannot take advantage of overloading and having the same method name perform different tasks with different input.
Say you want
double SumNum()
{
double a;
double b;
cin >> a;
cin >> b;
return a + b;
}
to take floating point input. You can't. You'd have to change the method's name or use templates.
I'm new on programming and I need to learn it for Arduido purposes. I used this code to test some things but I keep getting the "expected primary function before "int"" error, and it also says that the position functions isn't declared.
Am I declaring it wrong? I've tried many different things and kept getting the same message. My objective is to keep typing '1' and get 3, 6, 9, etc on the screen from calling the position function at cout.
#include <iostream>
using namespace std;
int main()
{
int degree=0;
int r=1;
while (r != '0')
{
cin >> r;
// this is where I get the error //
int position()
{
if ( r == 1 )
{
degree=degree+3;
}
return degree;
}
cout << position();
}
return 0;
}
Nesting functions is not allowed in C++. Change your code to be:
#include <iostream>
using namespace std;
int main()
{
// code
}
int position()
{
}
The problem is that your function position is in your function main, which is not possible in c++. Move the position out of the main function.
int position(int r, int degree)
{
if ( r == 1 )
{
degree=degree+3;
}
return degree;
}
int main()
{
int degree=0;
int r=1;
while (r != '0')
{
cin >> r;
cout << position(r, degree);
}
return 0;
}