#include <iostream>
#include <string>
using namespace std;
struct player
{
string name;
int money = 100;
int wins = 0;
};
int main()
{
string user;
cin >> user;
player user;
user.name = user;
}
What is the proper syntax for this in C++? I'm trying to declare the object name as the one given by the user. How would you do this? Would a class be easier to do this? Any tips/advice is much appreciated!
variables names are meaningless at runtime in compiled langages like c++. If you want to refer to a player using his or her provided username at runtime you need a datastructure that will remember the name as as string, like a hashtable or a map (I would recommend a map, those are easier to use) http://www.cplusplus.com/reference/map/map/
best is classes because of security
as by default ,
classes are private in nature but struct are public in nature
#include <iostream>
#include <string>
using namespace std;
class player
{
public:
string name;
int money;
int wins;
public(string name){
this->name = name;
this->money = 100;
this->wins = 0;
}
};
int main()
{
string user;
cin >> user;
player *user = new player(user);
}
Related
This question already has answers here:
How do I write setters and getters for an array? (c++)
(3 answers)
Closed 3 years ago.
I'm trying to represent a Course with Students. The Students have information about their first and last names, age... And the Courses have a name and an array of 3 Students.
I'm getting an error when I try to define the getters and setters for the array.
Error (active) E0415 no suitable constructor exists to convert from "Student [3]" to "Student"
Error (active) E0137 expression must be a modifiable lvalue
Course.h
#pragma once
#include "Student.h"
#include "Teacher.h"
class Course
{
private:
string name;
Student students[3];
Teacher teacher;
public:
Course();
~Course();
void setName(string name);
string getName();
void setStudents(Student students[3]);
[3] Student getStudents();
};
Course.cpp
#include <iostream>
#include "Course.h"
#include "Student.h"
#include "Teacher.h"
using namespace std;
Course::Course() {}
Course::~Course()
{
}
void Course::setName(string name)
{
this->name = name;
}
string Course::getName()
{
return this->name;
}
void Course::setStudents(Student students[3])
{
/*for (int i = 0; i < 3; i++) {
this->students[i] = students[i];
}*/
//This way the set works
this->students = students;
}
[3]Student Course::getStudents()
{
return this->students;
}
I expect the output of the get to be the array of students.
A C style array cannot be copied, cannot be automatically assigned, and cannot be returned from a function.
Thankfully, the C++ standard library provides a thin wrapper class over C style arrays which implement all these operations. It’s called std::array and it can be used exactly like you’re trying to use C-style arrays.
#pragma once
#include "Student.h"
#include "Teacher.h"
#include <array>
class Course
{
private:
string name;
std::array<Student, 3> students;
Teacher teacher;
public:
Course();
~Course();
void setName(string name);
string getName();
void setStudents(std::array<Student, 3> students);
std::array<Student, 3> getStudents();
};
I'm trying to learn about inheritance. I've tried to make a class called ProductionWorker that is derived from Employee. I'm trying to follow the model given by this website. However, it seems like the inheritance isn't working because I get errors in the main function saying that name, number, and date are not set in this scope. What is wrong with the code?
Program.cpp:
#include <iostream>
#include <string>
#include "employee.h"
using namespace std;
int main() {
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
worker.setName(name);
cout<<"What is the employee number?"<<endl;
cin>>number;
worker.setNumber(number);
cout<<"What is the employee hire date?"<<endl;
cin>>date;
worker.setDate(date);
cout<<"Employee Information:"<<endl;
cout<<worker.getName()<<endl;
cout<<worker.getNumber()<<endl;
cout<<worker.getDate()<<endl;
cout<<worker.getShift()<<endl;
cout<<worker.getPayRate()<<endl;
return 0;
}
employee.h:
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include <string>
#include <iostream>
using namespace std;
class Employee{
protected:
string name;
int number;
string date;
public:
Employee(string a="", int b=0, string c=""){
name=a;
number=b;
date=c;
}
void setName(string);
void setNumber(int);
void setDate(string);
string getName();
int getNumber();
string getDate();
};
class ProductionWorker: public Employee{
private:
int shift;
double pay;
public:
ProductionWorker(int d=1, double e=10.0, string a="", int b=0, string c=""):Employee(a, b, c){
shift=d;
pay=e;
}
int getShift();
double getPayRate();
};
employee.cpp:
#include <string>
#include <iostream>
#include "employee.h"
using namespace std;
//EMPLOYEE
void Employee::setName(string a){
name=a;
}
void Employee::setNumber(int b){
number=b;
}
void Employee::setDate(string c){
date=c;
}
string Employee::getName(){
return name;
}
int Employee::getNumber(){
return number;
}
string Employee::getDate(){
return date;
}
//PRODUCTION WORKER
int ProductionWorker::getShift(){
return shift;
}
double ProductionWorker::getPayRate(){
return pay;
}
I think that you should declare name, number and date before you use it, in Program.cpp. You try to get input into symbol that compiler doesn't know yet.
Your error is nothing to do with inheritance:
int main() {
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
You're reading into a variable called name but no such variable is in scope (i.e. declared in the main() function itself or globally). Though name is a member of the Employee class, you cannot use it as such in this way. (Consider: how would the compiler know which instance of Employee that you were wanting to set the name of? And, of course, if you could read directly into the instance variable, there would be no need to call worker.setName(...) afterwards).
You should just declare name as a local variable, by specifying its type:
int main() {
string name;
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
Now cin>>name; reads into the local variable name that is declared within the main() function. (I took the liberty of fixing your indentation). In a similar way, you need declarations for date and number.
You haven't declared the variables that cin needs to read into.
Just add
string name;
int number;
string date;
At the top of function main.
I recently started learning and working on multiple classes but i can't understand why my code doesn't work.
I get these errors:
errors photo
This is my code separated in these classes
main.cpp:
#include <iostream>
#include <string>
#include "dalykai.h"
using namespace std;
int main(){
string input;
dalykai dalykaiObj;
cout << "Type some name: "; cin >> input;
dalykaiObj.setName(input);
cout << "Jusu ivestas vardas yra: " << dalykaiObj.getName() << endl;
return 0;
}
dalykai.h
#ifndef DALYKAI_H
#define DALYKAI_H
using namespace std;
class dalykai{
public:
dalykai();
void setName(string x);
string getName();
protected:
private:
string name;
};
#endif // DALYKAI_H
dalykai.cpp
#include <iostream>
#include <string>
#include "dalykai.h"
using namespace std;
string name;
dalykai::dalykai(){
cout << "Object was created successfully!\n";
}
dalykai::void setName(string x){
name = x;
}
dalykai::string getName(){
return name;
}
This is wrong:
dalykai::void setName(string x){
name = x;
}
It should be:
void dalykai::setName(string x){
name = x;
}
In addition. Avoid using using namespace std in headers. Your get function should end with const since it doesn't change class member variables.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Admin {
static void editUser() {
vector<User> usr = FileManager::createVector(); //errors are here
...
}
};
class FileManager {
public:
static vector<User> createVector() {
string name;
string surname;
string code;
float miles;
float balance;
vector<User> users;
ifstream getUsers("users.txt");
while (getUsers >> name >> surname >> code >> miles >> balance) {
User temp(name, surname, code, miles, balance);
users.push_back(temp);
}
return users;
}
};
This is a piece of code I'm writing and I get these 2 errors:
error C2653: 'FileManager' : is not a class or namespace name
error C3861: 'createVector': identifier not found
The thing is I've looked all over the internet and I really can't see what is wrong, my head hurts a lot, and time is limited. I really didn't want to ask here because you probably have more important questions to answer. Any help is appreciated.
You should either define FileManager before Admin class or use forward declaration to make it visible for compiler.
I wanna know how should I assign a string to a string member of a struct.
for example :
#include<iostream>
#include<string>
using namespace std;
string c="salam";
struct man{
string name;
}*mary;
int main(){
string b ="HI";
(*mary).name=b;
return 0;
}
It doesn't work but I need this kind of assignment. I have string that I do some operations on it and wanna assign it to one of the members of my struct...
Thanks for your help :)
The problem is not with the string; it is with your attempted use of structures. You created a pointer-to-man, not a man.
Here's how to define a type called man, then create an instance of that type called mary, then assign a value to the member of that type called name:
#include <iostream>
#include <string>
using namespace std;
struct man
{
string name;
};
int main()
{
man mary;
mary.name = "HI";
}