Question:
C++ How to use an array with custom data structure?
James
2012-05-06 21:17:19 UTC
I have a struct called "myStruct" and a vector of myStructs called "myVector".

How do I use the the struct within the vector? Specifically, I need input to be stored into text.

/************************************************************************/
Looks basically like this:
struct myStruct
{
string text;
.....(<- other parts of the struct that aren't important)
.....
};

int main()
{
string input;
vector myVector;
cin >> input;
myVector[0].text = input (<- this was my guess, but causes "error: segmentation fault (core dumped)" )
return 0;
}
/************************************************************************/

I have tried many other guesses at how to do this, but no dice. Help?
Five answers:
anonymous
2012-05-07 01:31:43 UTC
Try this...

#include

#include

#include

using namespace std;

struct myStruct

{

string text;

};



int main()

{

string input;

vector myVector;

myStruct *Test = new myStruct;

cin >> input;

Test->text = input;

myVector.push_back(*Test);

cout << myVector[0].text << endl;

return 0;

}

I've tested it, and it compiles fine in g++
anonymous
2016-10-16 18:06:43 UTC
Your questions are extremely no longer consumer-friendly to respond to and that's clearly a homework situation. So I won't supply each and everything away. first of all, working with data from c++ would be somewhat troublesome for the hot programmer. i'd first write some small try classes to verify which you recognize situation-loose report io. IO in c++ is working equipment autonomous. So it extremely is not consumer-friendly to tell you precisely what to seek for via fact i do no longer understand what working equipment you're working with. yet usually it style of feels that fopen and fscanf and fprintf are the purposes i'd use to resolve the topics listed right here. there are assorted sources that coach you the thank you to apply them. Secondly, programmers attempt to "see there purposes" in any different case. to respond to the question what purposes do you want ask your self what would I do if i replaced into doing this and not utilising a working laptop or computing gadget. perhaps you will examine the enter, then calculate the output and then checklist the output. so your purposes would be examine, Calculate, and checklist. information structures are complicated and it extremely is not consumer-friendly to tell you what to do via fact i do no longer understand what you need to use. yet i'd use a vector from the widely used template library. this may well be seen cheating for a homework project. finally, injury your situation down into small chucks. resolve each and every of them and them composite the end result. do no longer attempt to do each and everything at as quickly as. Create and take a verify out each and every section independently. think of of it as 5 small initiatives and not one vast one. solid success and bear in ideas this could be sort of relaxing, if it extremely is not perhaps programming isn't for you because it extremely is going to be plenty extra of an identical :)
Fadi
2012-05-06 21:29:21 UTC
are you coming from the .NET framework?

all you do to store a variable into a vector is,

myVector[0] = input;



also, I'd recommend you use Visual Studio 2011 ultimate. it underlines all the errors to you and weed em out in an error-list. you won't have to wait till you compile the code to be able to see if your code works as it's intended to
?
2012-05-06 21:37:30 UTC
myVector.push_back(*(new myStruct(input));

or



myVector.push_front(*(new myStruct(input));



Like the above you can add items to vector in C++
john h
2012-05-07 01:12:11 UTC
in C++ you can output text to a file by

declaring the processes of outputting file and the file name.

ofstream outputFile("filename.txt");

if the file doesn't already exist it will be created.



study the following 2 programs i wrote to learn how to use file output in this way.



http://pastebin.com/Nf8nDcf3



http://pastebin.com/xAjxwJxD


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...