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);
}
Related
#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'm trying to find out the GCD in C++. I'm using __gcd code as mentioned on this website : https://www.geeksforgeeks.org/stdgcd-c-inbuilt-function-finding-gcd/
Appended is my code. Can someone please guide as to what is wrong
#include <iostream>
#include <string>
#include <numeric>
#include <algorithm>
using namespace std;
class Rational {
private:
int num;
int denom;
public:
Rational(int a, int b){
num = a;
denom = b;
}
int add(){
return num + denom;
}
int sub(){
return num - denom;
}
int mul(){
return num * denom;
}
void gcd(){
cout <<__gcd(num,denom);
}
int simplify(){
int gcd1 = gcd(num,denom);
return (num/gcd1,denom/gcd1);
}
};
int main(){
Rational r(2,6);
cout<<r.add()<<endl;
cout<<r.sub()<<endl;
cout<<r.mul()<<endl;
cout<<r.gcd()<<endl;
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
This is my BankAccount.h file
#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_
#include <string>
#include <iostream>
using namespace std;
class BankAccount{
public:
BankAccount();
BankAccount(string n, double b);
string getName();
double getBalance();
virtual void deposit(double a);
virtual bool withdraw(double a);
void toString();
bool transfer(BankAccount *a, double b);
protected:
string name;
double balance;
};
#endif
this is my BankAccount.cpp file
#include <iostream>
#include "BankAccount.h"
#include <string>
using namespace std;
BankAccount::BankAccount(string n, double b){
name = n;
balance = b;
}
string BankAccount::getName(){
return name;
}
double BankAccount::getBalance(){
return balance;
}
void BankAccount::deposit(double a){
balance = balance + a;
}
bool BankAccount::withdraw(double a){
if (balance - a < 0){
return false;
}
else {
balance = balance - a;
}
}
void BankAccount::toString(){
cout << "Name: " << this->getName() << " Balance:" << this->getBalance();
}
bool BankAccount::transfer(BankAccount *a, double b){
if (this->getBalance() - b < 0){
return false;
}
else {
this->withdraw(b);
a->deposit(b);
}
}
this is my SavingsAccount.h file
#ifndef SAVINGSACCOUNT_H_
#define SAVINGSACCOUNT_H_
#include <iostream>
#include <string>
#include "BankAccount.h"
using namespace std;
class SavingsAccount : public BankAccount {
public:
SavingsAccount(string n, double b, double i);
void addInterest();
private:
double interest;
};
#endif
This is my SavingsAccount.cpp file
#include <iostream>
#include "SavingsAccount.h"
#include <string>
using namespace std;
SavingsAccount::SavingsAccount(string n, double b, double i){
name = n;
balance = b;
interest = i;
}
void SavingsAccount::addInterest(){
balance = balance + (interest * balance * .01);
}
I keep getting an undefined reference to BankAccount error. I'm not really sure why, any help would be greatly appreciated. It keeps opening up a makeFile.win with $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
highlighted.
You have declared a default constructor (the one with no parameters) for BankAccount, and you are using it in SavingsAccount, but you haven't actually implemented it.
I'm using visual basic 2010. I have tried everything I can think of to eliminate these errors. It's a simple idea behind the program but I'm curious if the issue has something to do with how I'm referencing or calling my variables. I an new to C++, any help or suggestions would be greatly received. Thanks in advance.
Box.cpp
#include "stdafx.h"
#include "Box.h"
#include <iostream>
#include <iomanip>
using namespace std;
double getVolume(int A, int B, int C){
double totalVolume;
totalVolume = A * B * C;
return totalVolume;
}
double getSurfaceArea(int A, int B, int C){
double totalSurface;
totalSurface = (A*B*2) + (B*C*2) + (A*C*2);
return totalSurface;
}
bool perfectBox(int A, int B, int C){
bool perfect;
if (A = B)
if (B = C)
perfect = true;
else
perfect = false;
else
perfect = false;
return perfect;
}
//Box.h
class Box
{
public:
int A, B, C;
Box(int A, int B, int C);
double getVolume(int A, int B, int C);
// get volume of entered sides
double getSurfaceArea(int A, int B, int C);
// calculate surgace are based on sides
bool perfectBox(int A, int B, int);
// compare all 3 sides to determine if box is perfect
};
//Main.cpp
#include "stdafx.h"
#include "Box.h"
#include <iostream>
using namespace std;
Box::Box(int a, int b, int c){
}
int main()
{
int a, b, c;
cout << "Enter 3 side lengths of a box to determine volume, surface area and if it's perfect...
\n";
cout << "length of Side 1: ";
cin >> a;
cout << endl << "Side 2: ";
cin >> b;
cout << endl << "Side 3: ";
cin >> c;
Box test(a, b, c);
cout << "Total Area: " << test.getVolume(a, b, c) << endl;
cout << "Total Surface: " << test.getSurfaceArea(a, b, c) << endl;
cout << "Is it a perfect box: " << test.perfectBox(a, b, c) << endl;
system ("Pause");
return 0;
}
You miss your namespace declaration. When you refer to getVolume, it's not just getVolume, but Box::getVolume.
double Box::getVolume(int A, int B, int C){
double totalVolume;
totalVolume = A * B * C;
return totalVolume;
}
double Box::getSurfaceArea(int A, int B, int C){
double totalSurface;
totalSurface = (A*B*2) + (B*C*2) + (A*C*2);
return totalSurface;
}
bool Box::perfectBox(int A, int B, int C){
bool perfect;
if (A = B)
if (B = C)
perfect = true;
else
perfect = false;
else
perfect = false;
return perfect;
}
Can you please show the compiler output?
In your constructor you haven't assigned A, B, C so perhaps there is an issue there. Also you are using assignment operators i.e. =, not comparison operator i.e. == which are two different commands.
Simple rule: Every Function of class you want to use, must be declared [float functioName(int param)] in the .h file of the class. This includes the constructor classname(int a);
There is a missing constructor declaration in box.h
Box(int a, int b, int c);
should be move in box.cpp
Box::Box(int a, int b, int c)
{
}
Tip from a researcher of our computer science deparment:
first include system header then local headers:
#include <iostream>
#include <iomanip>
#include "stdafx.h"
#include "Box.h"
instead of (different order depending on <> or ""):
#include "stdafx.h"
#include "Box.h"
#include <iostream>
#include <iomanip>
There also missing functions in the .h File. Every function which is defined in box.cpp must be declared in box.h
Declare:
float getVolume();
Define:
float Box::getVolume()
{
float localVariableForVolume = A*B*C;
//this works too float localVariableForVolume = this->A*this->B*this->C;
return(localVariableForVolume);
}
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;
}