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;
}
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 <iostream>
#include <string>
#ifndef FOOD_H
#define FOOD_H
using namespace std;
this class for insertion sorting for food's price or food's calorie
class Food
{
private:
string Name;
float Calorie;
float Price;
public:
void setName(string n);
void setCalorie(float c);
void setPrice(float p);
string getName();
float getCalorie();
float getPrice();
Food();
Food(string n,float c,float p);
void Print(Food FoodArray[],int);
void Sort(Food FoodArray[], float);
void Sort2(Food FoodArray[], float);
};
#endif
#include <iostream>
#include <string>
#include "Food.h"
#ifndef IMPLEMENTER_CPP
#define IMPLEMENTER_CPP
using namespace std;
void Food::setName(string n){Name=n;}
void Food::setPrice(float p){Price=p;}
void Food::setCalorie(float c){Calorie=c;}
string Food::getName(){return Name;}
float Food::getPrice(){ return Price;}
float Food::getCalorie(){ return Calorie;}
Food::Food(string n,float c,float p)
{
Food::Name=n;
Food::Calorie=c;
Food::Price=p;
}
Food::Food()
{
Food::Name="";
Food::Calorie=0.00;
Food::Price=0.00;
}
void Food::Print(Food FoodArray[],int num)
{
for (int i=0;i>num;i++)
{
cout<<"-------------------------------------------------"<<endl;
cout<<"NAME: "<<FoodArray[i].getName()<<" CALORIE: "<<FoodArray[i].getCalorie()<<" PRICE: "<<FoodArray[i].getPrice()<<endl;
cout<<"-------------------------------------------------"<<endl;
}
}
void Food::Sort(Food FoodArray[], float pri)
{
int i,j;
float tmp;
string t;
for(i=1;i<=pri-1;i++)
{
tmp=FoodArray[i].getPrice();
t=FoodArray[i].getName();
j=i-1;
while((tmp<FoodArray[j].getPrice())&&(j>=0))
{
FoodArray[j+1]=FoodArray[j];
j--;
}
FoodArray[j+1].setName(t);
FoodArray[j+1].setPrice(tmp);
}
cout<<endl<<"Sorted list is shown below: "<<endl;
}
void Food::Sort2(Food FoodArray[], float cal)
{
int i, j;
float tmp;
string t;
for(i=1;i<=cal-1;i++)
{
tmp=FoodArray[i].getCalorie();
t=FoodArray[i].getName();
j=i-1;
while((tmp<FoodArray[j].getCalorie())&&(j>=0))
{
FoodArray[j+1]=FoodArray[j];
j=j-1;
}
FoodArray[j+1].setCalorie(tmp);
FoodArray[j+1].setName(t);
}
cout<<endl<<"Sorted list is shown below: "<<endl;
}
#endif
#include <iostream>
#include <string>
#include "Food.h"
#include "Implementer.cpp"
using namespace std;
contains the main method. From this class an array of food will be created and the sorting function
will be called to sort the array.
class client
{
public:
Food *foodArray[];
void info()
{int num;
string name,answer;
float calorie,price;
loop2:
cout<<"how many do you want ? ";
cin>>num;
if (num>0)
{
for (int i=0; i<num; i++)
{
cout<<i+1<<"- ";
cout<<"Enter Food name: ";
cin>>name;
cout<<"Enter Calories: ";
cin>>calorie;
cout<<"Enter Price: ";
cin>>price;
Food food=Food(name,calorie,price);
*foodArray[i]=food;
}
foodArray[num]->Print(*foodArray,num);
loop:
cout<<"Would you like to sort the foods based on Price or Calories? P/C"<<endl;
cin>>answer;
if (answer=="P")
{
foodArray[num]->Sort(*foodArray,price);
foodArray[num]->Print(*foodArray,num);
}
else if (answer=="C")
{
foodArray[num]->Sort2(*foodArray,calorie);
foodArray[num]->Print(*foodArray,num);
}
else
{
cout<<"please choose P or C."<<endl;
goto loop;
}
}
else
{
goto loop2;
}
}
};
int main()
{
client C;
C.info();
return 0;
}
I am working on a project for a class and I am having some troubles, that gave me 2 errors which i don't understand what they mean...
It gave the error: c4716 "medie" must return a value.
Here is the code:
#include <iostream>
#include <stdlib.h>
#include<math.h>
using namespace std;
float medie(float a, float b, float c)
{
float MG,MA;
MG= sqrt(a*b*c);
cout<< "MG="<< MG<<endl;
MA=(2*a*b*c)/(a+b+c);
cout<< "MA="<< MA<<endl;
}
float medie(float a,float b,float c,float d)
{
float MG,MA;
MG= sqrt(a*b*c*d);
cout<< "MG="<< MG<<endl;
MA=(2*a*b*c*d)/(a+b+c+d);
cout<< "MA="<< MA<<endl;
}
int main()
{
float a,b,c,d;
cout<<"a="<<endl;
cin>>a;
cout<<"b="<<endl;
cin>>b;
cout<<"c="<<endl;
cin>>c;
cout<<"d="<<endl;
cin>>d;
medie(a,b,c);
medie(a,b,c,d);
}
Your medie function is declared to return a float value, but you don't have any return statement in it. If you declare them to return void the error should go away.
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
void medie(float a, float b, float c)
{
float MG,MA;
MG = sqrt(a*b*c);
cout<< "MG="<< MG<<endl;
MA = (2*a*b*c)/(a+b+c);
cout<< "MA="<< MA<<endl;
}
void medie(float a,float b,float c,float d)
{
float MG,MA;
MG = sqrt(a*b*c*d);
cout<< "MG="<< MG<<endl;
MA = (2*a*b*c*d)/(a+b+c+d);
cout<< "MA="<< MA<<endl;
}
int main()
{
float a,b,c,d;
cout<<"a="<<endl;
cin>>a;
cout<<"b="<<endl;
cin>>b;
cout<<"c="<<endl;
cin>>c;
cout<<"d="<<endl;
cin>>d;
medie(a,b,c);
medie(a,b,c,d);
}
I think there is something simple wrong with my header file where I have the nested class declared, if not that, I must had missed a step in setting up the nested class. I only get an error in main towards the bottom when I try to declare an instance of it (printpersonal).
error in main isn't till bottom. Everything above is good.
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <stdio.h>
#include "Stocks.h"
using namespace std;
int main(){
cout<<"Create account username: "<<endl;
string name;
cin>>name;
cout<<"Create password: "<<endl;
string password;
cin>>password;
accountinfo personal;
personal.name=name;
personal.password=password;
ifstream data;
int counter=0;
dividends div;
data.open("Netflix.csv");
string word;
Stocks annualdata;//class
//Date, Open, High, Low, Close, Volume, Adjusted Close
ticker info;//struct
int lineindex=0;
while(getline(data, word, '\n')){
stringstream ss;
int wordindex=0;
if(lineindex>0){
ss<<word;
while(getline(ss,word,',')){
if(wordindex==0){//10 lines of data total
info.date=word;
}
if(wordindex==1){//10 lines of data total
info.open=addDouble(word);
}
if(wordindex==2){//10 lines of data total
info.high=addDouble(word);
}
if(wordindex==3){//10 lines of data total
info.low=addDouble(word);
}
if(wordindex==4){//10 lines of data total
info.close=addDouble(word);
}
if(wordindex==5){//10 lines of data total
info.volume=addInt(word);
}
if(wordindex==6){//10 lines of data total
info.adjustedclose=addDouble(word);
}
wordindex++;
}
}
annualdata.addDay(info);
lineindex++;
}
annualdata.maxprofit();
annualdata.oneyear();
annualdata.sixmonths();
annualdata.maxloss();
double money;
double divs=div.annualdiv;
if(data=="Apple.csv"){
money=div.addDiv(divs);
div.oneyear();
}
Stocks::Account printpersonal;
printpersonal.printaccountinfo(personal);
return 0;
}
Header File
#ifndef STOCKS_H
#define STOCKS_H
#include <string>
using namespace std;
//Date, Open, High, Low, Close, Volume, Adjusted Close
struct ticker{
std::string date;
double open;
double high;
double low;
double close;
int volume;
double adjustedclose;
};
struct accountinfo{
std::string name;
std::string password;
};
class Stocks//base class
{
public:
Stocks();//Constructor1 method7
Stocks(std::string, double, double, double, double, int, double);
void addDay(ticker);//method1
void maxprofit();//method2
void oneyear();//method3
void sixmonths();//method4
void maxloss();//method5
int index=253;
double money=1000;
int shares;
ticker annual[254];
class Account {//nested class
public:
Account();//method11
void printaccountinfo(accountinfo info){//method12
}
};
protected:
private:
};
class dividends:public Stocks{//derived class
public:
dividends(){};//constructor2 method8
double annualdiv=(.57+.57+.52+.52);
double addDiv(double annualdiv);//method6
void oneyear();//overloaded method method9
};
#endif // STOCKS_H
Source File
#include "Stocks.h"
#include <iostream>
using namespace std;
Stocks::Stocks(string d, double o, double h, double l, double c, int v, double ac)
{
string date=d;
double open=o;
double high=h;
double low=l;
int volume=v;
double adjustedclose=ac;//Date, Open, High, Low, Close, Volume, Adjusted Close
}
Stocks::Stocks()
{
//dtor
}
void Stocks::addDay(ticker data){
annual[index].date=data.date;
annual[index].open=data.open;
annual[index].high=data.high;
annual[index].low=data.low;
annual[index].close=data.close;
annual[index].volume=data.volume;
annual[index].adjustedclose=data.adjustedclose;
index--;//most recent data is inserted at bottom (Chronologically)
}
void Stocks::maxprofit(){
int index=0;
int x=1;
while(index<254){
if(annual[index].open<annual[index].close){
shares=money/annual[index].open;
money+=shares*(annual[index].close-annual[index].open);
while(x<254){//when you decide to sell
if(annual[index].close>annual[index+x].open){
index=x;
x=254;
}
else{x++;}
}
index++;
}
else{index++;}
}
cout<<"Portfolio value using the optimal trading strategy after 1 year: $"<<money<<endl;
}
void Stocks::maxloss(){
int index=0;
int x=1;
while(index<254){
if(annual[index].open>annual[index].close){
shares=money/annual[index].open;
money+=shares*(annual[index].close-annual[index].open);
while(x<254){//when you decide to sell
if(annual[index].close>annual[index+x].open){
index=x;
x=254;
}
else{x++;}
}
index++;
}
else{index++;}
}
cout<<"Portfolio value using the worst trading strategy after 1 year: $"<<money<<endl;
}
void Stocks::oneyear(){
money=1000;
shares=money/annual[0].open;
money+=(shares*annual[0].open-shares*annual[254].close);
//Color(12,"\N Hey! I'm in color!")
cout<<"portfolio value after 1 year: $"<<money<<endl;
}
void Stocks::sixmonths(){
money=1000;
shares=money/annual[0].open;
money+=(shares*annual[0].open-shares*annual[127].close);
//Color(12,"\N Hey! I'm in color!")
cout<<"portfolio value after 6 months: $"<<money<<endl;
}
double dividends::addDiv(double annualdiv){
money=1000;
money=money+annualdiv;
return money;
}
void dividends::oneyear(){
money=1000;
money+=(shares*annual[0].open-shares*annual[254].close);
shares=money/annual[0].open;
cout<<"Apple portfolio amount including dividends: $" <<money+annualdiv<<endl;
}
I noticed you're missing a ; after your Account class. Not sure if that's the cause of the error but I think that needs to be fixed.
I want float variable 'avg' to be returned and then in the main pass it to 'void batsman::display(float a)' using a parameter. then display the average marks in display method. this method gives me 2 errors. any other way?
#include<iostream.h>
class batsman
{
int marks[5];
char name[15],country[15];
public:
void input();
float cal();
void display();
};
void batsman::input()
{
int i;
cout<<"Enter player name: ";
cin>>name;
cout<<"Enter player country: ";
cin>>country;
cout<<"Enter player marks"<<"\n";
for(i=0;i<5;i++)
{
cout<<"Mark "<<i+1<<": ";
cin>>marks[i];
}
}
float batsman::cal()
{
int i;
int tot=0;
float avg;
for(i=0;i<5;i++)
{
tot=tot+marks[i];
}
avg=(float)tot/5;
return avg;
}
void batsman::display(float a)
{
float avg1;
avg1=a;
cout<<"Player name: "<<name<<"\n";
cout<<"Player country: "<<country<<"\n";
cout<<"Average: "<<avg1<<"\n";
}
int main()
{
batsman b1;
b1.input();
b1.cal();
b1.display(b1.batsman::cal());
//cout<<"Average: "<<b1.batsman::cal()<<"\n";
}
The code has several errors.
iostream.h should be iostream
using namespace std; // add this at top so that cout is found
display() should be display(float a) in the class declaration.
After those changes the code ran as expected.
Change display's definition in your class to display(float a), and prepend std:: to cout and cin. As a recommendation, don't use iostream.h, use iostream.