Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to get three variable from the user by using in my own header ...
here's the piece of the code i have written ...
<class T>
Sparse<T>::Sparse(){
std::cout << "Please Enter The Following Information" << std::endl << "Row:" ;
std::cin >> this->rows;
std::cout << "Column:" << std::endl;
std::cin >> this->column;
std::cout << "Please Enter The Number of TermS:" << std::endl;
std::cin >> this->term;
}
i used forward decelaration for in my own header :
class cin;
the problem i've encountered is as you can see there is no loop for the function of the class But ...
when i run the code, the compiler runs this block multi_Times ...
Like the cin just can't initialize the variable ...
Like this
"Please Enter The Following Information"
"Rows:"
"Columns:"
"Please Enter The Number of TermS:"
"Please Enter The Following Information"
"Rows:"
"Columns:"
"Please Enter The Number of TermS:"
Please Help .....
Extra Detail ....
As Mr.Coffin Said I Want to use "Forward declaration" Please Help ME ...
How do i must use ...
cause including a header in another header is such a bad style to use ...
And yes i have two constructor one gets the argument for the
the other works this way ....
and it is because i dont want to have cin and cout and lots of equalation (=) in my main ...
Thanks Mr.Coffin ....
The constructor of a class is there for the initial creation of the object. In this example, this is the default constructor. In general you probably don't want to write user-facing questions in the constructor of a class, let alone the default constructor.
Writing it your way means that any time you create an uninitialized Sparse, the user will be asked these questions.
Sparse<int> a, b;
This calls the constructor twice, asking the questions twice, forcing the user to answer the questions without knowing which 'Sparse' they are being questioned over.
Sparse<int> a, b;
if (day == "Monday") {
a.something();
} else {
b = a;
}
Here the user is prompted with questions that only make sense one day of the week.
But you also run into problems like:
std::vector<Sparse<int>> manySparses;
manySparses.resize(1000);
The user is now going to be asked the questions ... 1000 times.
Choose one of two approaches: Ask the questions BEFORE you construct the object; or choose sensible default values - e.g. values that indicate "I don't have values yet" and then populate the values after construction,
You could have a static member for asking the question or you could have a member that asks them after the object is constructed:
Sparse<int> a, b;
a.GetConstraints();
if (day == "Monday")
b.GetConstraints();
else
b = a;
But don't write a default constructor that asks the user questions like this.
---- Edit, regarding 'cin' ----
'cin' is not a class. It's an object. For portability, and just down right good practice, you shouldn't go injecting your own attempt to declare standard library types/structs/functions like this, especially not in headers.
Instead, accept it as a dependency and
#include <iostream>
in your header file before you try to use it.
---- Edit: final notes ----
When you think of "Construction" in the programming sense try to think on the scale of "deciding I'm going to build a skyscraper" rather than "undertaking the work of building the empire state building, selling space on the 101st floor and hiring a management team".
It's not that you can't/shouldn't write complex constructors, but your goal should simply be to get the object into a reliable state for the program.
class string {
public:
string() {
std::cout << "Enter the string you want: ";
std::cin >> m_string;
m_length = strlen(m_string);
}
...
};
vs
class string {
public:
string() : m_string(NULL), m_length(0) {}
...
};
Secondly, having a constructor which talks to standard input/output fundamentally cripples its usefulness. There's no problem with using that logic to obtain the values from the user, but if you put it into the class's constructor then the class can only be used in an application that has standard input/output (neither gui nor headless apps need apply).
Thirdly, how will you handle errors? What if cin gets closed after the first question? Your code doesn't check for it, and if it did, it would have to pass the burden to whomever is creating the object. Nobody expects to have to write
try {
Object myObject; // may fail if user types 'wibble' instead of '13'.
} catch (wibble_input_as_columns_exception& e) {
std::cerr << "Damnit, Dave, 'wibble' is not a valid number of columns." << std::endl;
}
Related
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 1 year ago.
Improve this question
I'm having some issue with returning vectors of objects of a class in functions because everytime my destructor erases the data twice and all the code just dies when the functions ends
here a simple code I wrote just to show my problem:
#include <iostream>
#include <vector>
using namespace std;
class identity{
public:
string name;
identity(string Name);
~identity();
};
vector<identity> function();
int main(){
function();
cout << "Hello world!";
}
identity::identity(string Name)
: name{Name}{
cout << "Object created!" << endl;
}
identity::~identity(){
cout << "Object " << name << " destroyed!" << endl;
}
vector<identity> function(){
identity me("Isaias");
}
in this case the cout "Hello world" doesn't work and the program always ends with "Object" without displaying the name like this:
Object created!
Object Isaias destroyed!
Object
and then the program just stops. I kind of fixed the problem by seting the type of the function to "void" or anything else instead of "vector" but I'd like to know why this problem occurs. Also I'm new to programming in general and in this community so I'm sorry if I'm not doing this in the right way.
I'd like to thank you all for your attention before anything and sorry again if i am messing everything up here.
vector<identity> function(){
identity me("Isaias");
}
The behaviour of the program is undefined because you don't return anything from the function even though you've declared that the function returns a vector<identity>.
To fix the bug, return a value. Example:
using namespace std::string_literals;
return {"Isaias"s};
Another bug is that you've chosen to use using namespace std, but you've declared identity which is an identifier that is already in the std namespace. This makes the program ambiguous and ill-formed.
To fix this, choose another name for the class, such that the name doesn't conflict with any of the current or future names in the std namespace. This is very difficult because there are many names in std and you cannot predict the future. There is a simpler solution though! Simply don't use using namespace std.
Third bug is that you've used std::string without including the header that defines it. The consequence is that the program may not necessarily compile in any current or future language implementation.
Solution: Include the header <string>.
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 4 years ago.
Improve this question
I am building a project that is composed of Vehicle, Showroom, and Dealership. I've built the classes, and I am testing out my method GetAveragePrice()
float Dealership::GetAveragePrice()
This method was working perfectly:
Dealership dealership("COP3503 Vehicle Emporium", 3);
dealership.AddShowroom(&showroom);
dealership.AddShowroom(&secondary);
dealership.AddShowroom(&third);
cout << "Using just the GetAveragePrice() function\n\n";
cout << "Average price of the cars in the dealership: $" << std::fixed << std::setprecision(2);
cout << dealership.GetAveragePrice();
The output would be
Using just the GetAveragePrice() function
Average price of the cars in the dealership: $27793.60
This is the expected output I wanted, but I was told I have memory leaks and must include a destructor to deallocate my *Showroom showroomList pointer (which I initialized as the following in the Dealership constructor):
this->showroomList = new Showroom[maxNumOfShowrooms];
So I write my destructor as the following:
Dealership::~Dealership()
{
delete [] showroomList;
}
Now, there aren't any memory leaks, but I don't get the expected output and an exit code 11:
Using just the GetAveragePrice() function
Process finished with exit code 11
Does anyone know why this destructor is messing up my output?
This version would delete only once by the last instance standing, in its destructor.
std::unique_ptr<ShowRoom> Dealership::showroomList;
Dealership::Dealership(size_t maxNumOfShowrooms)
:showroomList(std::unique_ptr<ShowRoom>(new Showroom[maxNumOfShowrooms]))
{
}
Dealership::~Dealership()
{
// auto deleted here, with reverse order of initialization
}
but you have a new and delete pair so you should check for deletion only once. This would need some global counter outside of class (or its static variable) and this may not be as readable as smart pointer.
If you are using multiple threads with this, then you could be better with shared_ptr and a custom deleter ([](T * ptr){delete [] ptr;}) as its second constructor parameter.
At least this way you can know if error is about new and delete.
I'm working on a simple game engine just for the experience of it. I've realized, though, that I have no idea how to export the user's custom game as its own standalone executable. For example (this is not my actual game engine, it just provides an easy reference for discussion), suppose we had the following very simple code:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void RunGame(string question, string answer)
{
string submission;
cout << question << endl;
getline(cin, submission);
if (submission == answer)
cout << "Correct!";
else
cout << "Wrong!";
}
int main()
{
string question;
string answer;
cout << "Enter Question:" << endl;
getline(cin, question);
cout << "Enter Answer:" << endl;
getline(cin, answer);
RunGame(question, answer);
}
In this example the user gets to create their own customized bit of trivia, and then can test it immediately afterwards when RunGame is called. Now I want to be able to save their game with the trivia information they provided as its own .exe (basically it will perform from the call to RunGame onwards). How would I go about doing that?
To be clear, this isn't a question about what is the easiest/fastest way to make a game. It is looking for how to build a standalone, executable file from within code.
If you really want to store data inside the .exe itself:
An executable has a header that defines its size, boundaries and other useful stuff to the Operating System, so, essentially, the operating system knows where the code and data sections start and end, and it finally uses this information to load up the .exe to memory when it is asked to run.
Since the Operating System knows (besides the .exe's file size) where the executable actually ends, this also means that any data pasted after the .exe's "calculated" end (by headers) won't negatively effect the binary. It will still load and execute just fine.
You can abuse this property to concatenate data after the end of the executable.
I'll leave you with this test, using Windows' bundled WordPad application as a 'host' for some other data:
Go to C:\Windows and copy write.exe (WordPad) to another folder, so we can experiment without damaging anything.
Bring to that folder another file, any file will do. In my example, the data file will be a PDF called "myfancyfile.pdf"
Now, open a command prompt and use the COPY command to stitch both files together, making sure the .exe comes first:
copy /B write.exe+myfancyfile.pdf mynewprogram.exe
copy's /B flag means "binary copy", so essentially both files were pasted together without any kind of text or data conversion.
Try to run "mynewprogram.exe". Realize it runs just fine :-)
Self-modifying your .exe with data is not only feasible, it won't negatively effect functionality. Having that said, it is still a ugly way to persist data.
Have fun coding your solution.
You don't want to do this. A better way is to save the trivia in some custom format (for example, .txt, .dat, ..).
Then the game just handles this data.
So first think about the format inside of the .txt for example.
Lets say at first theres a number, indicating which entry this is. Second follows the question and after that the answer follows. This, you must decide for yourself.
Example trivia-data.txt
1
How old is actor X from show Y?
32 years
2
...
...
#include <iostream> // std::cout, std::endl
#include <fstream> // std::ifstream, std::ofstream
using namespace std;
int main()
{
// create file
ofstream ofile("trivia-data.txt");
// define your data
int num_of_question = 1;
string question, answer;
getline(cin, question);
getline(cin, answer);
// write your data to the file
ofile << num_of_question << '\n';
ofile << question << '\n';
ofile << answer << '\n';
// close the file
ofile.close();
return 0;
}
Now that you created your data you just have to build your program in a way you would like to present this. Instead of writing to a file, you should read from a file and print the questions out and compare answers and what not. Look up std::ifstream for reading your file.
At the start you could ask your user if he would like to create a quiz or play one that already exists.
Edit:
Since this sounds a lot like homework I just provide some pseudo code.
I'd go for an approach like this(pseudo code):
print "Would you like to create(c) or play(p) a quiz? Answer(c/p): "
input = get_input() // 'c' or 'p'
if input == 'c'
// now do what I posted with some loops to create a couple of questions
else
print "Please provide an URL to the quiz-data you would like to play: "
url = get_input() // C:/test.txt
// read in data, print out questions, do comparisons and print answers etc
This is infinitely easier than your approach and this also makes it possible for others to create quizzes not just you.
Building an executable is non-trivial. You will first need to comply with the target operating systems' ABI so that it can find your program's entry point. The next step will be deciding how your program is going to be able to access system resources: probably you'll want your executable to implement dynamic linking so it can access shared libraries, and you'll need to load the various .dll or .so files you're going to need. All the instructions you'll need to write for this will vary from OS to OS, you may need to introduce logic to detect the exact platform and make informed decisions, and you will need to vary for 32 vs 64 bit.
At this point you're about ready to start emitting the machine instructions for your game.
A reasonable alternative here is (as done by Unity) to provide a "blank" executable with your engine. Your engine itself would be a shared library (.dll or .so) and the blank executable would simply be a wrapper that loads the shared library and invokes a function in it with a pointer to something in it's data section.
Generating your user's executable would comprise loading the appropriate blank, making platform-specific modifications to it to tell it the size of the data section you're intended to provide it with and writing your data in the appropriate format. Or, you could simply have a blank that has an embedded copy of the raw structure into which you write values, just like populating a struct in memory:
struct GameDefinition {
constexpr size_t AuthorNameLen = 80;
char author_[AutherNameLen+1];
constexpr size_t PublisherNameLen = 80;
char publisher_[PublisherNameLen+1];
constexpr size_t GameNameLen = 80;
char name_[GameNameLen+1];
constexpr size_t QuestionLen = 80;
constexpr size_t AnswerLen = 80;
char question_[QuestionLen+1];
char answer_[AnswerLen+1];
};
static GameDefinition gameDef;
#include "engine_library.h" // for run_engine
int main() {
run_engine(&gameDef);
}
You'd compile this againsst the shared-library stub for your engine, and emit it as an executable, then you'd look up the platform-specific details of the executable format, locate the position of "gameDef" in it. The you'd read the blank into memory, and write it out with the definition of "gameDef" replaced with the one based on user input.
But what many engines do is simply ship or require the user to install a compiler (Unity relies on C#). So instead of having to tweak executables and do all this crazy platform-specific stuff, they simply output a C/C++ program and compile it.
// game-generator
bool make_game(std::string filename, std::string q, std::string a) {
std::ostream cpp(filename + ".cpp");
if (!cpp.is_open()) {
std::cerr << "open failed\n";
return false;
}
cpp << "#include <engine.h>\n";
cpp << "Gamedef gd(\"" << gameName << "\", \"" << authorName << \");\n";
cpp << "int main() {\n";
cpp << " gd.q = \"" << q << \"\n";
cpp << " gd.a = \"" << a << \"\n";
cpp << " RunGame(gd);\n";
cpp << "}\n";
cpp.close();
if (!invoke_compiler(filename, ".cpp")) {
std::cerr << "compile failed\n";
return false;
}
if (!invoke_linker(filename)) {
std::cerr << "link failed\n";
return false;
}
}
If "RunGame" is not part of your engine but user-supplied, then you could emit that as part of the cpp code. Otherwise, the intent here is that it's making a call into your library.
Under Linux you might compile this with
g++ -Wall -O3 -o ${filename}.o ${filename}.cpp
and then
g++ -Wall -O3 -o ${filename} ${filename}.o -lengine_library
to link it against your engine's library.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
coming from java i would like to not have to deal with de-allocation when creating new custom or other library's objects.
today i was trying to create an instance of my entity object like:
entity cube = new entity("entityName")
because this is how entity's constructor is formatted
but i get the following error:
cannot convert from |entity *| to |entity|
i noticed there are no errors if i just remove the new keyword, and i was wondering two things.
what does the error while using new mean ? (i'm pretty confident with how pointers work but not completely as i started with java.)
is it ok for me to create objects like that without the new keyword or is an object even created? (because there are no errors.)
new entity("entityName")
means "create an instance of entity in the free store and return a pointer to that instance".
Since a pointer to an entity is not the same as an entity, you cannot initialise an entity with that value unless you have yet another constructor.
The way to do what you want is
entity cube("entityname");
And you need a good book on C++.
First, I suggest you to read a C++ tutorial. It has much more complexity than Java.
This is a very partial Java to C++ "how to convert" guide that I can give you:
Java code:
void sayHello(String name) {
system.out.println("Hello, " + name);
}
public static void main(String args[]) {
String name = "James"; // <-- This string is of course created in the dynamic memory
sayHello(name); // <-- "name" will be passed by reference to the "sayHello()" method
}
Equivalent in C++ - option 1:
void sayHello(const std::string &name) {
std::cout << "Hello, " << name << std::endl;
}
int main() {
std::string name("James"); // <-- Variable is created on the stack
sayHello(name); // <-- Although "name" is not a pointer, it will be passed by reference to "sayHello", as "name" is defiend there with "&", which means that it is a reference
}
A reference is a very "weird" type - it behaves like a local variable, although it actually points to an instance that does not have to be on the stack of the current function or on the stack at all.
C++ - option 2:
void sayHello(const std::string *name) {
std::cout << "Hello, " << *name << std::endl; // <-- dereferenceing "name" using a preceding star, as "cout" needs the variable itself and not its address
}
int main() {
std::string *name = new std::string("James"); // <-- Instance is created in the dynamic memory
sayHello(name); // <-- Sending the pointer "name" to the "sayHello" function
// You will need to free "name" somewhere in the code unless you don't care about memory leaks
}
There are more options, like passing the instance by value (not recommended in such case), or like creating it in the dynamic memory and deref
what does the error while using new mean ? (i'm pretty confident with how pointers work but not completely as i started with java.)
No. C++ is different about this, you don't use an allocation (new) to initialize cube:
entity cube("entityName");
is it ok for me to create objects like that without the new keyword or is an object even created? (because there are no errors.)
No. See above. ("because there are no errors." I doubt this, there should at least be compiler warnings if you assign entity from a pointer.)
I am trying to make a simple comparison to find if istream is a std::cin or std::ifstream.
My pseudocode is something like that:
class myclass
{
public:
void write(istream& is)
{
if(is == cin) // this does not work
{
//do something
}
else
{
//do something else
}
}
};
How can I proceed?
Thank you!
Since std::cin is an instance of std::istream, you could compare the addresses of the two objects to see if they are equal:
if (&is == &std::cin)
(Demo)
However I would consider investigating if you can achieve your goal without doing this; switching logic based on the identity of the stream argument is not very clean and may inhibit future development or maintenance of this project.
I'm not sure why you need to check if you have cin or not, but I had the same problem and my reason was: if I have cin, it means I'm in interactive mode, so I should give some useful information to cout. While checking the address of cin with is will probably hopefully always work, I wouldn't really trust it...
Anyways, my solution was to slightly generalize and add an extra parameter with a reasonable default
void f(istream &i, ostream *o = nullptr) {
if (o) {
*o << "useful info...";
}
i >> important_variable;
}
This gives a slightly more general function, using the "pointer to parameter means optional parameter" idiom. If you need cin for some other reason, then you may also look into specifically which properties of cin you need, and how to determine if your input stream possesses them.