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.
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.
Student here. I'm missing a single rubric item in the following project program and can't figure out where to place it, "it" being a pass-by-reference item. If anyone is feeling generous, please take a look at my program below and give me a heads-up about where I could tweak the code to make pass-by-reference possible. As it stands, I'm stumped and I don't have enough time to come up with an entirely new problem for which to code a solution. Thank you all in advance!
#include <iostream>
#include <cmath>
using namespace std;
//Global Variable
int height = 0;
//Function Prototypes
int getMale();
int getFemale();
int main()
{
//Local Variable
int ideal = 0;
char sex(' ');
//Sequence Structure
cout << "Welcome to the Hamwi Ideal Body Weight Equation Calculator!" << endl;
cout << "Please enter your height in inches (remember, 1 foot = 12 inches): " << endl;
cin >> height;
cout << "Please enter your biological sex (M or F): " << endl;
cin >> sex;
//Repetition Structure
while (toupper(sex) != 'M' && 'F')
{
cout << "Invalid entry. Please enter your biological sex (M or F): " << endl;
cin >> sex;
} //end while
//Selection Structure
if (toupper(sex) == 'M')
{
int ideal = getMale();
cout << "Thank you. Your ideal body weight is approximately: " << ideal << " pounds." << endl;
cout << "Have a nice day!" << endl;
} //end if
else
{
int ideal = getFemale();
cout << "Thank you. Your ideal body weight is approximately: " << ideal << " pounds." << endl;
cout << "Have a nice day!" << endl;
} //end else
return 0;
} //end of main function
//Program-Defined Function #1 (Male)
int getMale()
{
//Local Variable
int male = 0;
if (height >= 60)
{
male = 106 + ((height - 60) * 6);
return male;
} //end if
else
{
male = 106 - ((60 - height) * 6);
return male;
} //end else
} //end of male function
//Program-Defined Function #2 (Female)
int getFemale()
{
//Local Variable
int female = 0;
if (height >= 60)
{
female = 100 + ((height - 60) * 5);
return female;
} //end if
else
{
female = 100 - ((60 - height) * 5);
return female;
} //end else
} //end of female function
Declare your two functions like this
int getMale ( int& height );
int getFemale ( int& height );
And in your main you declare a variable that you can pass to the functions
int main()
{
// Declare here instead of globally
int height = 0;
// Then you can call
int result = getMale(height);
result = getFemale(height);
}
It will behave the same way. It is considered better practice to pass by reference than to use a global variable, so kudos to you for asking this question.
You pass height by reference in one of your gender functions.
For example:
int getMale(int &height) {
/* do stuff */
}
And then simply call it by:
getMale(height);
Also, are you required go use a global variable? If not, then make height a local variable in your main as other commenters have stated. Global variables are considered bad style, as they can cause hard-to-diagnose problems when you're working on a much larger project.
You could move variable height to main() (make it local) and pass it by reference to all your functions. Actually, I'd say that would be a better style than passing data through global variables.
But in a real project it's typically better to think of what behavior you want from your program, not of what awesome language features you want to use.
Is this program not giving you the correct answer and that's why you want to pass by reference? or what is the exact situation?
Anyways in order for you to pass by reference then you remove height from being a global variable and have it local in your main, then for your prototypes include a reference parameter then when you call them in main pass your local variable.
This is example of how pass by reference would work.
#include<iostream>
using namespace std;
//Prototype
void add(int num1, int num2, int &res);
int main()
{
int res;
add(1, 2, res);
cout<<"I got the value by passing my variable by reference"<<res<<endl;
return 0;
}
void add(int num1, int num2, int &res)
{
res = num1 + num2;
}
I am having trouble with a very simple program. I need to call in a method (getNumber) outside of main that takes two numbers from the user and then store those numbers. Those two numbers are then used in a calculation method (math) which is also called into main. I am getting an uninitialized local variable for my two numbers I am calling in from getNumber. I would like the user to enter two numbers have them added together and display the result but by calling in methods.
#include <iostream>
#include <string>
using namespace std;
int getNumber(int x, int y)
{
// here is where the user is prompted to input two numbers
cout << "Please enter two values" << endl;
cin >> x >> y;
return x, y;
}
int math(int x , int y) // here is where the calculations are done
{
int result;
result = x + y;
return result;
}
int main()
{
int x;
int y;
int result;
x = getNumber(x, y); // trying to call in the input method here
result=math(x,y); // calling in claculation method
cout << result;
system("pause");
return 0;
}
void getNumber(int &x, int &y)
{
cout << "Please enter two values" << endl;
cin >> x >> y;
}
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. :)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've seen this all over the site and still nothing has been able to fix my problem. this is the only error I have and I have no idea how to fix this. I've tried commenting out different sections of the code just to see what happens and still nothing but errors. What is going on?
Her is the build error as well
error C2084: function 'HeroProfile::HeroProfile(void)' already has a body
Main.cpp
#include <iostream>
#include <string>
#include "HeroProfile.h"
using namespace std;
int main()
{
string name;
int race;
double weight;
cout << "Enter your Hero's name:";
cin>> name;
cout << "Enter your Hero's Race (1:Elf 2:Human 3 Dwarf)";
cin >> race;
cout << "Enters your Hero's weight ( in pounds):";
cin >> weight;
HeroProfile Hero_1(name, race, weight);
cout << endl << "hero's Name: " << Hero_1.getName() << endl <<
"Race: " << Hero_1.getRace() << endl <<
"Weight:" << Hero_1.getWeight() << endl;
cout << endl;
cout << "Enter your Hero's Name:";
cin >> name;
cout << "Enter your Race (1:Elf 2:Human 3 Dwarf)";
cin >> race;
cout <<"Enter your Hero's weight (in pounds)";
cin >> weight;
HeroProfile Hero_2(name, race, weight);
Hero_2.setName(name);
Hero_2.setRace(race);
Hero_2.setWeight(weight);
cout << endl << "Hero's Name: " << Hero_2.getName() << endl <<
"Race: " << Hero_2.getRace() << endl << "Weight: " << Hero_2.getWeight() << endl;
return 0;
}
The header File
#include <iostream>
#include <string>
using namespace std;
#ifndef HeroProfiel_H
#define HeroProfile_H
class HeroProfile
{
public:
//default constructor
HeroProfile();
//overlaod constructor
HeroProfile(string, int, double);
//destructor
~HeroProfile();
//Accesor functions
string getName() const;
// get name - returns name of patient
int getRace() const;
// getHeight-returns height of patient
double getWeight() const;
// getWeight- returns weight of patient
//Mutator Functions
void setName(string);
//set name of patient
//#param string - name of patient
void setRace(int);
//setHeight-sets heigh tof patient
//#param int- height iof patient
void setWeight(double);
//setWeight-sets weight of patiend
//#param double-weight of patient
private:
//Memeber variables
string newName;
int newRace;
double newWeight;
};
#endif
and the second .cpp file
#include "HeroProfile.h"
eroProfile::HeroProfile() //Default constructor
{
newRace = 0;
newWeight = 0.0;
}
HeroProfile::HeroProfile(string name, int race, double weight) //Overloaded Constructor
{
newName = name;
newRace = race;
newWeight = weight;
}
HeroProfile::HeroProfile()
}
}
//Accessors
string HeroProfile::getName() const
{
return newName;
}
int HeroProfile::getRace() const
{
return newRace;
}
double HeroProfile::getWeight() const
{
return newWeight;
}
//Mutators
void HeroProfile::setName(string name)
{
newName = name;
}
void HeroProfile::setRace(int race)
{
newRace = race;
}
void HeroProfile::setWeight(double weight)
{
newWeight = weight;
}
As others have noted, you have 2 definitions in your second cpp file for the HeroProfile default constructor HeroProfile::HeroProfile():
The first is
HeroProfile::HeroProfile() //Default constructor
{
newRace = 0;
newWeight = 0.0;
}
and the second is
HeroProfile::HeroProfile()
}
}
Based on the fact I don't see one, you probably intended for the second one to be your class destructor (as declared in your header file but not defined in your cpp file), in which case you should replace it with this:
HeroProfile::~HeroProfile()
}
}
I hope you didn't get confused by the fact that HeroProfile::HeroProfile(void) and HeroProfile::HeroProfile() are the same thing, so I thought I should point it out.
The message means exactly what it says. Ignoring typos, the first and third definitions in your second .cpp are for the same function.