Pointers, dynamic variables, C++ - c++

Errors
I keep receiving errors when running this program. What do these errors mean? A screenshot is attached.
This is the problem:
A class Area that has two private variable members; units of type string and area_value of type float.
1) Input from the keyboard the area_value and its units. Compute one-half and one-quarter of the area and display the results with unit.
2) Destroy the dynamic variable at the end.
#include <iostream
#include <string>
using namespace std;
class Area
{
public:
Area();
void setu(string a);
void seta(float b);
string getu();
float geta();
private:
string unit;
float area_value;
};
int main()
{
Area *p = new Area();
string a;
float f;
cout << "Enter a unit with no space: ";
getline(cin, a);
p->setu(a);
cout << "Enter a value of area: ";
cin >> f;
p->seta(f);
cout << "A half of your input = " << f / 2 << endl;
cout << "A quarter of your input = " << f / 4 << endl;
delete p;
return 0;
}

Looks like
You have not implemented the member functions, or
You have implemented the member functions in a separate file and forgot to include it in building the executable.

It seems you forgot to implement the constructor Area() and the methods of your Area class.

The first include is missing greater than sign (>). The public Area class only defines the functions, not implementing them. Variable p is defined as a pointer.
You can also add half and quarter as functions. I modified it slightly so it compiles with clang++ -Wall -std=c++11 -o area area.cpp.
#include <iostream>
#include <string>
using namespace std;
class Area {
public:
Area() {};
~Area() {};
void setu(string u) { unit = u; }
void seta(float a) { area_value = a; }
string getu() { return unit; }
float geta() { return area_value; }
float getHalf() { return area_value / 2; }
float getQuarter() { return area_value / 4; }
private:
string unit;
float area_value;
};
int main() {
Area p {};
string a;
float f;
cout << "Enter a unit with no space: ";
getline(cin, a);
p.setu(a);
cout << "Enter a value of area: ";
cin >> f;
p.seta(f);
cout << "A half of your input = " << p.getHalf() << endl;
cout << "A quarter of your input = " << p.getQuarter() << endl;
return 0;
}

Related

CPP program displays shadows the perimeter error [duplicate]

I am trying to make a function that returns double the integer number that I will pass to it. I am getting the following error message with my code:
declaration of 'int x' shadows a parameter int x; "
Here is my code:
#include <iostream>
int doublenumber();
using namespace std;
int doublenumber(int x)// <-- this is the function which returns double the value .
{
int x;
return 2 * x;
cout << endl;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin >> a;
doublenumber(a);
return 0;
}
You have x as a parameter and then try to declare it also as a local variable, which is what the complaint about "shadowing" refers to.
I did it because your advice was so helpful, and this is the final result :
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int n= doublenumber(a);
cout << "the double value is : " << n << endl;
return 0;
}
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int d = doublenumber(a);
cout << "Double : " << d << endl;
return 0;
}
There are some problem with your code. Your declaration and definition of function dies not match. So remove declaration as no necessity of it.
You are declaring local x variable inside function which will shadow your function arguments.

Why am I getting this error: class CSE has no member named EnterRN and EnterName?

#include <iostream>
using namespace std;
class CSE
{
private:
char Name;
double Roll;
public:
void getN(char N, double RN)
{
Name = N;
Roll = RN;
}
};
char EnterName()
{
cout << "Enter the name of the student" << ;
}
char EnterRN()
{
cout << "enter the rn" << ;
};
int main()
{
CSE nnn;
nnn.getN(N, RN);
cout << "enter the name" << nnn.EnterName << endl;
cout << "enter the roll" << nnn.EnterRN << endl;
return 0;
}
What should I correct here for the right implementation of my code? I just want to keep the names and roll number private and then public them afterwards. I also want to enter the name and roll number by the user and then display it.
You are defining (free) functions instead declaring and defining a method:
class CSE {
...
public:
char EnterRN();
};
char CSE::EnterRN() {
...
}
If you want to do it inline then the closing } of the class needs to be after those functions.

error access violation writing location 0x00229C20. when trying to input a string in console

i am trying to input a string in my code in c++ and when i run i always get the following error: Exception thrown at 0x0F5023F5 (msvcp140d.dll) in assignment-1.exe: 0xC0000005: Access violation writing location 0x00229C20. i will post my code below if anyone could help me that would be great.Please note that i already know that its a problem with me trying to access the memory location on which you dont have access to, i just dont know how to fix it.
HEADER FILE:
#ifndef item_H
#define item_h
class item
{
private:
//attributes
int itemID;
char itemName[20];
float itemcost;
float itemprice;
//utility function
float calcPrice();
public:
//constructor
item(int = 000, char[] = "itemUnknown", float = 0,float = 0);
//destructor
~item();
//set functions
void setAll(int, char[], float, float);
void setID(int);
void setName(char[]);
void setCost(float);
//get function
int getID();
float getcost();
float getprice();
void getname();
//print function
void print();
};
#endif
CPP:
#include "Dariush.h"
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
//constructor will set attributes
item::item(int ID, char n[] , float c,float p)
{
setID(ID);
setName(n);
setCost(c);
setAll(ID, n, c, p);
}
//destructor will print destroing two objects
item::~item()
{
cout << "destroing two objects : " << " " << itemName << " "
<< " & " << itemName << endl;
}
//set functions :
void item::setID(int ID)
{
cout << "please enter the item's ID : " << endl;
cin >> ID;
}
void item::setName(char n[])
{
cout << "please enter the item's name" << endl;
cin.ignore();
cin.getline(n, 20);
}
void item::setCost(float c)
{
cout << "please enter the item's cost : " << endl;
cin >> c;
}
void item::setAll(int ID, char n[], float c, float p)
{
itemID = (ID > 0 && ID < 999) ? ID : 0;
strcpy_s(itemName, n);
itemcost = (c > 0) ? c : 0;
calcPrice();
}
//get functions :
int item::getID()
{
return itemID;
}
float item::getcost()
{
return itemcost;
}
float item::getprice()
{
return itemprice;
}
void item::getname()
{
cout << itemName << endl;
}
//print function :
void item::print()
{
cout << "ID : " << itemID << endl
<< "Name : " << itemName << endl
<< "cost : " << itemcost << endl
<< "price : " << itemprice << endl;
}
// utility function for price callculation :
float item::calcPrice()
{
if (itemcost < 1000)
{
itemprice = itemcost + (itemcost*0.1);
}
else
itemprice = itemcost + (itemcost*0.2);
return itemprice;
}
MAIN.CPP:
#include "Dariush.h"
#include <iostream>
#include<string>
using namespace std ;
void main()
{
item i1;
item i2;
i1.print();
i2.print();
}
thanks for the assistance.
Lets take a closer look at these three function declarations:
item(int = 000, char[] = "itemUnknown", float = 0,float = 0);
void setAll(int, char[], float, float);
void setName(char[]);
The thing here is that the character "array" arguments you declare are not really arrays at all. Instead they are pointers. When declaring arguments, e.g. char n[] is actually translated by the compiler as char *n.
The constructor declaration makes the pointer point to the constant string literal "". And the important thing about constant string literals is that they are indeed constant. Trying to modify a string literal leads to undefined behavior. And change this literal is what you are trying to do with the cin.getline(n, 20) call in the setName function. Not only that, but you are also telling the cin.getline function to read more than fits in the string literal.
The simple solution is to have setName read into the member variable itemName instead.
There are many problems with this code, but the one that is causing the access violation is:
void item::setName(char n[])
{
cout << "please enter the item's name" << endl;
cin.ignore();
cin.getline(n, 20); //here
}
You should use cin.getline(itemName, 20); instead.
Also, to prevent such error in the future, declare arguments as char const n[] instead of char n[] - good compiler should display a warning when you use string literals with non-const pointer as argument.

C2597 illegal reference to non static member

Trying to get this homework figured out and I just keep hitting one wall after another. What I am getting now is the error message:
Error 1 error C2597: illegal reference to non-static member 'circleType::radius'
I have 2 header files, circleType.h and cylinderType.h and I need to out put results for shipping and painting costs that a user would enter. A little help before I go completely out of my mind ... Thank you.
circle.h
class circleType
{
public:
static void setRadius(double r);
double getRadius();
double area();
double circumference();
circleType(double r = 0);
private:
double radius;
};
void circleType::setRadius(double r)
{
if (r >= 0)
{
radius = r;
}
else
{
radius = 0;
}
}
double circleType::getRadius()
{
return radius;
}
double circleType::area()
{
return 3.1416 * radius * radius;
}
double circleType::circumference()
{
return 2 * 3.1416 * radius;
}
circleType::circleType(double r)
{
setRadius(r);
}
cylinderTyper.h
#include "circleType.h"
class cylinderType: public circleType
{
public:
static void setRadius(double r);
static double getRadius();
static double area();
static double circumference();
cylinderType(double r = 0);
private:
double radius;
};
main.cpp
#include "stdafx.h"
#include "cylinderType.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void enterData(int& cylinderBase,int& cylinerHeight, double& shipCost, double& paintCost);
int main()
{
cout << fixed << showpoint << setprecision(2);
int cylinderBase, cylinderHeight;
double sCost, pCost, shipCost, paintCost, volume, area = 0, circumference = 0;
enterData(cylinderBase, cylinderHeight, shipCost, paintCost);
cylinderType::setRadius(cylinderBase + cylinderHeight);
cylinderType::getRadius();
cylinderType::area();
cylinderType::circumference();
cout << "Cost of shipping: $" << circumference * shipCost << endl;
cout << "Cost of painting: $" << area * paintCost << endl;
system("pause");
return 0;
}
void enterData(int& cylinderBase, int& cylinderHeight, double& shipCost, double& paintCost)
{
cout << "Enter the base size of cylinder: ";
cin >> cylinderBase;
cout << endl;
cout << "Enter the hight size of cylinder: ";
cin >> cylinderHeight;
cout << endl;
cout << "Enter shipping cost per liter: ";
cin >> shipCost;
cout << endl;
cout << "Enter cost of painting per square foot: ";
cin >> paintCost;
cout << endl;
}
It's a very simple rule: static member functions can only access member variables that are static as well. That's because a static function isn't called against a specific object, so object members don't make sense in that context.
In your case, the static function setRadius is trying to modify the member variable radius which is not static. I suspect that you really don't want setRadius to be a static function.

cout/cin does not name a type error

Whats wrong with this code to keep on receiving this error?
The error only happened when instead of putting "distanceFormula" in main, I made it it's own class.
#include <iostream>
#include <string>
using namespace std;
class distanceFormula {
public:
int speed;
int time;
int distance;
cout << "What is the speed?" << endl;
cin >> speed;
cout << "How long did the action last?" << endl;
cin >> time;
distance = speed * time;
cout << "The distance traveled was " << distance << endl;
};
int main()
{
distanceFormula ao
ao.distanceFormula;
return 0;
};
The body of the class declaration can only contain members, which can either be data or function declarations, and optionally access specifiers.
Wrap your code inside a function and then call that in main by an object
class distanceFormula {
public:
int speed;
int time;
int distance;
void init()
{
cout << "What is the speed?" << endl;
cin >> speed;
cout << "How long did the action last?" << endl;
cin >> time;
distance = speed * time;
cout << "The distance traveled was " << distance << endl;
}
};
int main()
{
distanceFormula ao;
ao.init();
return 0;
};
If you still have to use a class. Here is how you do it:
#include <iostream>
class distanceFormula
{
private:
int speed; // private
int time; // private
public:
distanceFormula(); // constructor
int getSpeed(); // to get
int getTime(); // to get
int getDistance(); // to get
void setSpeed(int); // to set
void setTime(int); // to set
};
distanceFormula::distanceFormula()
{
this->time = 0;
this->speed = 0;
}
int distanceFormula::getSpeed()
{
return this->speed;
}
int distanceFormula::getTime()
{
return this->time;
}
int distanceFormula::getDistance()
{
return this->time * this->speed;
}
void distanceFormula::setSpeed(int speedVal)
{
this->speed = speedVal;
}
void distanceFormula::setTime(int timeVal)
{
this->time = timeVal;
}
int main()
{
distanceFormula YourObject; // create obj
int SpeedValue;
int TimeValue;
std::cout << "Enter the Speed:";
std::cin >> SpeedValue; // take speed
std::cout << "Enter the Time:";
std::cin >> TimeValue; // take time
YourObject.setSpeed(SpeedValue); // set
YourObject.setTime(TimeValue); // set
std::cout << "This is the distance: " << YourObject.getDistance(); // retrieve result
getchar(); // wait
return 0;
}