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.
Related
I'm new to programming, I was trying this program to copy one string into another string but it's showing error
"error C3861: 'copyString': identifier not found"
Here is the code that I wrote
#include <iostream>
using namespace std;
int main()
{
char a[8], b[8];
cout << "enter the string a";
cin.get(a, 8);
cout << a;
int len = sizeof(a) / sizeof(char);
copyString(a, b);
int i;
cin >> i;
return 0;
}
/*function that copy one string to another*/
void copyString(char* a, char* b)
{
int i = 0;
while (a[i] != '\0') {
b[i] = a[i];
i++;
}
cout << b << " String is this";
}
Please tell me where am I mistaking??
Either provide copyString implementation before main, or provide a prototype for it first:
void copyString(char *a,char *b); // prototype of copyString
int main()
{
...
}
void copyString(char *a,char *b) // implementation of copyString
{
...
}
C++ compiler requires you to provide the declarations before a function or a variable is used. To solve your problem, you can simply place a forward declaration of copyString() before main().
#include <iostream>
using namespace std;
void copyString(char* a, char* b); // this is the forward declaration
int main()
{
char a[8], b[8];
cout << "enter the string a";
cin.get(a, 8);
cout << a;
int len = sizeof(a) / sizeof(char);
copyString(a, b);
int i;
cin >> i;
return 0;
}
/*function that copy one string to another*/
void copyString(char* a, char* b)
{
/* here is the real implementations */
}
However, when your program growth and more and more functions (with inter-dependence) are added, I would recommend you to split those functions into a separated header file and a source for easier maintenance.
main.cpp
#include <iostream>
using namespace std;
#include "my_string_lib.h" // PLEASE notice this line
int main()
{
char a[8], b[8];
cout << "enter the string a";
cin.get(a, 8);
cout << a;
int len = sizeof(a) / sizeof(char);
copyString(a, b); // included in my_string_lib.h
int i;
cin >> i;
return 0;
}
my_string_lib.h
#pragma once // assume you are using msvc
/*!
Copy the string content from a to b,
assume b is allocated large enough to hold a.
*/
void copyString(char* a, char* b);
my_string_lib.cpp
#include "my_string_lib.h"
void copyString(char* a, char* b)
{
/* here is the real implementations for copying a to b*/
}
Please make sure that main.cpp, my_string_lib.cpp and my_string_lib.h are placed inside the same directory.
#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;
}
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 am Getting undefined symbol error for all the integer and char values.Please Help me.
The int x y and z are not working and also the char value of function.
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <string.h>
class Calculator
{
public:
int x;
int y;
int z;
char function;
void Calculate()
{
if(function=='+')
{z=x+y;}
else if(function=='-')
{z=x-y;}
else if(function=='*')
{z=x*y;}
else if(function=='/')
{z=x/y;}
else
{cout<<"Wrong Function!!!";}
}
};
void main()
{
clrscr();
Calculator working;
cout<<"Welcome!"<<endl;
cout<<"Enter your first number:"<<endl;
cin>>x;
cout<<"Enter your function:"<<endl;
cin>>function;
cout<<"Enter your second number:"<<endl;
cin>>y;
working.Calculate();
cout<<"Your Result is:"<<z<<endl;
getch();
}
Either use, std::cin, std::cout, std::endl or include std namespace,
using namespace std;
This code compiles:
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
class Calculator
{
public:
int x,y;
float z;
void add()
{
cin>>x;
cin>>y;
z=x+y;
cout<<"The addition is:"<<z<<endl;
}
void substract()
{
cin>>x;
cin>>y;
z=x-y;
cout<<"The substraction is:"<<z<<endl;
}
void multiply()
{
cin>>x;
cin>>y;
z=x*y;
cout<<"The multiplication is:"<<z<<endl;
}
void divide()
{
cin>>x;
cin>>y;
z=x/y;
cout<<"The division is:"<<z<<endl;
}
};
int main()
{
cout<<"Hello User!"<<endl;
char Name[23];
cout<<"Enter your name:";
cin>>Name;
cout<<"Hy "<<Name<<endl;
cout<<"Calculator:"<<endl;
Calculator maths;
cout<<"Enter two numbers to Add:"<<endl;
maths.add();
cout<<"Enter two numbers to Substract:"<<endl;
maths.substract();
cout<<"Enter two numbers to Multiply:"<<endl;
maths.multiply();
cout<<"Enter two numbers to Divide:"<<endl;
maths.divide();
}
You are missing std:: qualifiers whenever you use cin, cout, or endl. Either use std::cout, std::cin and std::endl or include using namespace std; in the beginning of your file.
You need to pass the values to the function Calculate. Variables x, y, z and function are not accessible outside the class and also u need a return type to the function so that you can get the output from the function Calculate.
class Calculator
{
public:
int x;
int y;
int z;
char function;
int Calculate(int main_x,int main_y,char main_function)
{
x= main_x;
y=main_y;
function = main_function;
if(function=='+')
{z=x+y; return z;}
else if(function=='-')
{z=x-y; return z;}
else if(function=='*')
{z=x*y; return z;}
else if(function=='/')
{z=x/y; return z;}
else
{cout<<"Wrong Function!!!"; return 0;}
}
};
void main()
{
clrscr();
int main_x,main_y,main_z;
char main_function;
Calculator working;
cout<<"Welcome!"<<endl;
cout<<"Enter your first number:"<<endl;
cin>>main_x;
cout<<"Enter your function:"<<endl;
cin>>main_function;
cout<<"Enter your second number:"<<endl;
cin>>main_y;
main_z = working.Calculate(main_x,main_y,main_function);
cout<<"Your Result is:"<<main_z<<endl;
getch();
}
Error is coming because you are trying to access class member variable x, y, z from outside in the main() where x, y, z is not declared.
In order to calculate() work correctly x, y, z should get correct value, in your case these variable has garbage value.
This is successful compiles version of your code.
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
class Calculator
{
public:
int x;
int y;
int z;
char function;
void Calculate()
{
if(function=='+')
{z=x+y;}
else if(function=='-')
{z=x-y;}
else if(function=='*')
{z=x*y;}
else if(function=='/')
{z=x/y;}
else
{cout<<"Wrong Function!!!"<<endl;}
}
};
int main()
{
Calculator working;
cout<<"Welcome!"<<endl;
cout<<"Enter your first number:"<<endl;
cin>>working.x;
cout<<"Enter your function:"<<endl;
cin>>working.function;
cout<<"Enter your second number:"<<endl;
cin>>working.y;
working.Calculate();
cout<<"Your Result is:"<<working.z<<endl;
return 0;
}
I am making my own ADT, the issue I am having is the program won't compile as it crashes with an error C:\Dev-Cpp\Makefile.win [Build Error] [mainpoly.o] Error -1073741819.
I am not sure what that means, but i think it has to do with how i am naming my files (.h,.cpp). Right now I want to get my program to compile so I can test out the bugs in my ADT, i just need help in getting the program to compile at this point, not the errors in the program itself, thank you. I dont know what extension to give to what file (if thats even the core issue) Also, not sure hot to separate the three programs here on the forums, sorry.
Here are the three files
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <string>
#include <vector>
struct v{
int coe;
int deg;
};
class polynomial{
private:
vector<v> poly;
public:
~polynomial();
polynomial(int, int);
int degree();
int coeffecient(int);
void changeCoeffcient(int, int);
void mulpoly(int, int);
int addpoly(int, int);
int getpoly();
};
#endif
#include "polynomial.h"
#include <vector>
polynomial::polynomial(int coeffecient, int degree){
coe=coeffecient;
deg=degree;
v t;
t.co=coe;
t.de=deg;
poly.push_back(t);
}
polynomial::~polynomial(){
delete[];
}
int polynomial::degree(){
return poly;
}
int polynomial::coeffecient(power){
int i=power;
int val;
for(int z=0;z<t.size;z++){
if(i=t.de[z]){
val=t.co[z];
break;
}
else{
return 0;
}
}
return val;
}
void polynomial::changeCoeffcient(int newCoeffcient, int power){
int i=power;
for(int z=0;z<t.size;z++){
if(i=t.de[z]){
t.co[z]=newCoeffcient;
break;
}
else{
return 0;
}
}
}
void polynomial::mulpoly(int coeffecient, int mul){
int i=coeffecient;
for(int z=0;z<t.size;z++){
if(i=t.co[z]){
t.co[z]*=mul;
break;
}
else{
return 0;
}
}
}
#include <stdlib.h>
#include <iostream>
#include <polynomial.h>
using namespace std;
int main(){
int i;
polynomial poly(3,5);
i=poly.degree();
cout<<"The value is: "<<i;
system("PAUSE");
return 0;
}