first of all am very new to C++. I am having some problem with my code. The program should able to get/read the value user entered and output it back. I tried get/set C++ methods, but am having some problem with getting and outputting the value in my main. The following is my code,
#include <iostream>
using namespace std;
class Store {
public:
//get and Set Price
void setPrice(int x){
price = x;
}
int getPrice(){
return price;
}
//Get and set % Marked Up
void setPercentageMarkedUp(int y){
markedUpPrice = y;
}
int getPercentageMarkedUp(){
return markedUpPrice;
}
//Get and set percentage Sales tax
void setPercentageSalesTax(int y){
percSalesTax = y;
}
int getPercentageSalesTax(){
return percSalesTax;
}
private:
int price;
int markedUpPrice;
int percSalesTax;
};
int main(){
int price;
Store obj;
cout << "enter the Original Price of the item: "<<endl;
obj.getPrice();
cout<<"the value is:"<<price<<endl;
return 0;
}
As am very new to both C++ and StackOverflow, please dont downgrade me for asking this simple question. I know its very basic. Will definitely appreciate those who helps. Thanks in advance.
Just need to update the main as follows
int main(){
Store obj;
int n_price;
cout << " Enter Original Price: " << endl;
cin >> n_price;
obj.setPrice(n_price);
cout << "Original Price: " << n_price<< endl;
return 0;
}
Thanks everyone. :)
Related
I am trying to use a variable from the private class and both add and subtract from it at different times, first add 5 to it 5 times then subtract 5 from it 5 times and each time i have to display its value. Currently i have:
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
class car {
private:
int year;
string make;
int speed;
public:
void StoreInfo(int y, string m, int s);
int getSpeed() { return speed; }
int accelerate() { speed += 5; }
int brake() { speed -= 5; }
};
void car::StoreInfo(int y, string m, int s) {
year = y;
make = m;
speed = s;
}
car fillFields() {
car Filler;
int year; // Local variables to hold user input
string make;
int speed = 0;
// Get the data from the user
cout << "Enter year: ";
cin >> year;
cout << "Enter make: ";
cin.get(); // Move past the '\n' left in the
// input buffer by the last input
getline(cin, make);
cout << "The current speed is " << speed << endl;
Filler.StoreInfo(year, make, speed);
return Filler;
}
int main() {
car numbers = fillFields();
car::accelerate();
return 0;
}
This code accepts the input but does not work after that i realize that the car::accelerate() line in the main is incorrect now but how would i use it correctly?
First accelerate and brake do not return any value so you can make them void
void accelerate() { speed += 5; }
void brake() { speed -= 5; }
accelerate is non-static member function and needs an object so you need to call it like:
numbers.accelerate();
And for example change your main function like this to accerelate once and then brake and see the speed:
int main() {
car numbers = fillFields();
numbers.accelerate(); // +5
cout << "The current speed is " << numbers.getSpeed() << endl;
numbers.brake(); // -5
cout << "The current speed is " << numbers.getSpeed() << endl;
return 0;
}
Outputs:
Enter year: Enter make: The current speed is 0
The current speed is 5
The current speed is 0
Demo
You can replace car::StoreInfo with a constructor, because it does what the constructors are meant for. And don't forget to initialize your variables.
numbers.accelerate();
You are not using any static data. You could rename numbers variable if you want.
I'm trying to the program to display the bonus, but the programs renders an answer to 0. I am extremely new to c++ so any guidance would be greatly appreciated.
Here's the code:
#include <iostream>
#include <iomanip>
using namespace std;
//function prototypes
void enterItems(double, double);
void calcAndDisplayBonus(double &salesAmt, double &rate);
int main()
{
//declare variables
double sales = 0.0;
double bonusRate = 0.0;
//enter input values
enterItems(sales, bonusRate);
//calculate and display bonus
cout << fixed << setprecision(2);
calcAndDisplayBonus(sales, bonusRate);
system("pause");
return 0;
} //end of main function
//*****function definitions*****
void enterItems(double salesAmt, double rate)
{
cout << "Enter sales: ";
cin >> salesAmt;
cout << "Enter bonus rate (in decimal form): ";
cin >> rate;
} //end of enterItems function
void calcAndDisplayBonus(double &salesAmt, double &rate)
{
cout << "Bonus: $" << salesAmt * rate << endl;
} //end of calcAndDisplayBonus function
When you call enterItems, you are passing parameters by copy. This means that your changes won't affect the variables available in the scope of the caller.
To solve it, you can either pass a couple of references or pointers, as well as rely on a pair returned from the function as a result, and so on.
As an example, by writing:
void enterItems(double &salesAmt, double &rate)
You'll actually solve the problem above mentioned.
Another valid prototype is:
void enterItems(double *salesAmt, double *rate)
Even though this one asks for a small set of changes all around your code (the example, of course).
There is a plenty of possible solutions, hoping these ones will give you an idea of what's wrong.
Your function
void enterItems(double salesAmt, double rate)
is taking two double-parameters by value, this means, your changes you do inside the function will not be visible from the outside. You could take the doubles by reference:
void enterItems(double &salesAmt, double &rate)
but i'd prefer to return the values, but since you can only return a single value you'd need two functions:
double enterSales()
{
double tmp;
cout << "Enter sales: ";
cin >> tmp;
return tmp;
}
double enterBonus()
{
double tmp;
cout << "Enter bonus rate (in decimal form): ";
cin >> tmp;
return tmp;
}
//in your main:
double sales = enterSales();
double bonusRate = enterBonus();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This is an assignment i am trying to figure out:
Create a new project named Project3 and create a class named Rover
Within the Rover class, specify the following member instance variables:
name(string)
x position on a grid (integer)
y position on a grid (integer)
direction by compass – N, S, E, or W (String)
speed (0 – 5 meters per second, integer)
Within the Rover class, specify the following methods:
Default constructor – set the rover’s position to (0,0), its speed to 0, its direction to
North.
Constructor that receives parameters to initialize all five instance variables described above
Setter methods for each instance variable
Getter methods for each instance variable
getRoverData – returns a string that displays values for each instance variable of the
current rover object, placing each value on a separate line, as follows:
Rover name: A
X-position: 0
Y-position: 0
Direction: E
Speed: 1
Create a class client (main) that creates an array of the a maximum of five rovers and gets the initial
values for all rovers from the user. After the user specifies values for each rover, display a summary
of the rover’s values as shown above.
I have about a billion errors and i dont know why.
using namespace std;
class Rover {
private:
string name;
int x;
int y;
string direction;
int speed;
int position[10][10];
public:
void Rover();
void constructor(string name, int x, int y, string direction, int speed);
void setName(string name);
void setX(int x);
void setY(int y);
void setDirection(string direction);
void setSpeed();
string getName();
int getX();
int getY();
string getDirection();
int getSpeed();
string getRoverData();
};
void Rover::Rover() {
r1.position[0][0];
r1.speed = 0;
r1.direction = "N";
}
string Rover::getRoverData() {
cout << "Rover name: " << r1.getName() << endl;
cout << "X-position: " << r1.getX() << endl;
cout << "Y-position: " << r1.getY() << endl;
cout << "Direction: " << r1.getDirection() << endl;
cout << "Speed: " << r1.getSpeed() << endl;
}
void Rover::constructor(string name1, int x1, int y1, string direction1, int speed1) {
r1.name = name1;
r1.x = x1;
r1.y = y1;
r1.direction = direction1;
r1.speed = speed1;
}
void Rover::setName(string name) {
r1.name = name;
}
void Rover::setX(int x) {
r1.x = x;
}
void Rover::setY(int y) {
r1.y = y;
}
void Rover::setDirection(string direction) {
r1.direction = direction;
}
void Rover::setSpeed(int speed) {
r1.speed = speed;
}
string Rover::getName() {
return name;
}
int Rover::getX() {
return x;
}
int Rover::getY() {
return y;
}
string Rover::getDirection() {
return direction;
}
int Rover::getSpeed() {
return speed;
}
int main(int argc, char** argv) {
string name;
int x;
int y;
string direction;
int speed;
Rover r1;
r1.constructor("Yoda", 3, 3, "N", 3);
cout << "Enter name for Rover: ";
cin >> name;
r1.setName(name);
cout << "Enter its x position: ";
cin >> x;
r1.setX(x);
cout << "Enter its y position: ";
cin >> y;
r1.setY(y);
cout << "Enter direction N,E,S,W: ";
cin >> direction;
r1.setDirection(direction);
cout << "Enter its speed: ";
cin >> speed;
r1.setSpeed(speed);
r1.getRoverData();
return 0;
}
Your example appears incomplete. I'm guessing you just missed including the following lines in your post
#include <string>
#include <iostream>
using namespace std;
First, constructors do not have a return type so void Rover(); makes no sense. Remove void and you're golden there.
Second, what exactly do you think r1 is supposed to be? The compiler should tell you the identifier is undefined because it isn't. remove r1. from your member functions (i.e. anything function starting with Rover::. and you're golden there.
Third, what do you think r1.position[0][0] is going to do? It's just an expression that does nothing. Even position[0][0] is not going to do anything. Perhaps you want to initialize the array somehow but you haven't provided enough information to determine what you're trying to accomplish with it.
Fourth, the member function void Rover::setSpeed(int) has not been declared within the Rover class. Did you forget something? Based on your code it should be
int Rover::getSpeed()
{
return speed;
}
Fifth, void Rover::setSpeed(); doesn't make much sense unless it actually accepts an argument.
So I'm trying to write a basic program in C++ to get the cost of something, the quantity, and calculate the total/subtotal, in three different functions, then display it in main().
Problem is, the variables aren't making it out of the function and I don't know why. I've put output statements inside the functions themselves to check, and the problem only seems to be when I'm trying to pull them out of said functions.
#include <iostream>
using namespace std;
int price(int cost)
{
cout << "What is the cost of the robot?" << endl;
cin >> cost;
if (cost < 1000) //validation
{
cout << "Cost is too low. Setting to $1000." << endl;
cost = 1000;
return cost;
}
return cost;
}
int numRobots(int number)
{
cout << "How many robots are being ordered?" << endl;
cin >> number;
if (number < 50) //validation
{
cout << "We only sell in quantities of 50 or more. Setting quantity to 50." << endl;
number = 50;
return number;
}
return number;
}
void grandTotal(int cost, int number, double &subtotal, double &total)
{
subtotal = (cost * number);
total = (subtotal * .07) + subtotal;
}
int main()
{
int cost = 0;
int number = 0;
double subtotal = 0;
double total = 0;
price(cost);`enter code here`
numRobots(number);
grandTotal(cost, number, subtotal, total);
cout << cost; //testing
cout << number; //outputs
cout << total; //of
cout << subtotal; //variables
system("pause");
return 0;
price(cost);
You are calling a function which returns an int, but you're not storing the int anywhere. You might want to go back to your text book and check the chapter on functions, and how they work. No offense but this is rather basic.
You're doing the same thing with numRobots.
Alternatively, you could pass the parameter by reference and modify it, but imo, that's less easy to understand.
tl;dr;
You should be doing int cost = price(); (there's no reason for the function to take an int as a parameter)
Use returned value or pass parameter by reference or pointer.
1.
int result = numRobots(number);
2.
int numRobots(int& number) {.....}
You need to pass the variables by reference:
int cost = 0;
int number = 0;
price(cost);
numRobots(number);
void price(int& cost)
{
....
}
void numRobots(int& number)
{
....
}
Note the void return type in this case!
Alternatively, you can utilize the return value:
int cost = price(cost);
int number = numRobots(number);
But this method doesn't make much sense because the variable passed as parameter to methods is the same as the one in which the return value is stored!
I'm relative new to C++ and I'm sure there is probably plenty on information on here. Unfortunately, I don't seem to understand it.
I have a class called Account with variables called Number and Balance. The Member Name is given by the User through cin and represent an Account Number. I was able to dynamically create an object through cin. and give their variables a value through cin. However, I'm not able to give the variables a value or ask for the values in the class through cin.
I have been looking for hours and just cannot figure it out. Any help is appreciated. Thanks guys.
Here is my code:
class Account {
public:
int Number;
int Balance;
};
int main() {
int Nmbr;
int Bal;
cin >> Nmbr;
cin >> Bal;
Account Nmbr; // create the object
Nmbr.Number = Nmbr; // add the cin input 'Nmbr' to the variable Number - FALSE
Nmbr.Balance = Bal; // add the cin input 'Bal' to the variable Balance - FALSE
cout << Nmbr.Number << endl; // display Account.Number - FALSE
cout << Nmbr.Balance << endl; //display Account.balance - FALSE
}
Ok I think something like this is what you are after:
account.h:
class Account
{
public:
Account();
Account(int number);
int Number;
int Balance;
};
account.cpp:
#include "account.h"
Account::Account() {}
Account::Account(int number) {
this->Number = number;
}
So then in main you would do something like:
#include <iostream>
#include "account.h"
#include <vector>
const size_t MAXNUM = 5;
int main()
{
std::vector<Account*> allAccounts;
for (int accntNumber = 0; accntNumber < MAXNUM; accntNumber++) {
Account* account = new Account(accntNumber);
allAccounts.push_back(account);
}
// Then later do other stuff:
for (int accntNumber = 0; accntNumber < MAXNUM; accntNumber++) {
Account* checkAccount = allAccounts.at(accntNumber);
std::cout << "Account # = " << checkAccount->Number << std::endl;
}
return 0;
}
Edit: added an account class with a class creator based on the account number.