Ok, so my pointer logic is a little flawed, but I'm working on it. My problem is in the main.cpp file below, Inside the getStructData() function. I have the questions listed down there in comments, and what I think seems right, but know it's not. I will now put the question up here, out of the comments.
I have a function getMyStructData(), I can currently print out the elements of a specific struct based on an index number. Instead I'd like to copy the elements of that struct at the given index number (int structureArrayIndex) from the private structure into the structure of the pointer argument.
Inside myStructure.h
struct myStructure
{
int myInteger;
double myDoublesArray[5];
char myCharArray[80];
};
Inside myClass.h
#include "myStructure.h"
class myClass
{
private:
myStructure myStruct[5]
private:
Prog1Class();
~Prog1Class();
void setMyStructData();
void getMyStructData(int structureArrayIndex, struct myStructure *info);
};
Inside main.cpp
#include<iostream>
#include <string>
#include "myClass.h"
#include "myStructure.h"
using namespace std;
void myClass::setMyStructData()
{
for(int i = 0; i < 5 ; i++)
{
cout << "Please enter an integer: " << endl;
cin >> myStruct[i].myInteger;
for(int j = 0; j< 5; j++)
{
cout << "Please enter a double: ";
cin >> myStruct[i].myDoublesArray[j];
}
cout << endl << "Please enter a string: ";
cin.ignore(256, '\n');
cin.getline(myStruct[i].myCharArray, 80, '\n');
}
}
void Prog1Class::getStructData(int structureArrayIndex, struct myStructure *info)
{
//****Below I have what's working, but Instead of just printing out the elements, what I want to do is copy the elements of that struct at the given index number (int structureArrayIndex) from that private structure into the structure of the pointer argument.
//I'm guessing something like this:
// info = &myStructure[structureArrayIndex];
//I know that's wrong, but that's where I'm stuck.
//****Here is how I would print out all of the data using the int structureArrayIndex
cout << myStruct[structureArrayIndex].myInteger << endl;
for (int k = 0; k < 5; k++)
{
cout << myStruct[structureArrayIndex].myDoublesArray[k] << endl;
}
cout << myStruct[structureArrayIndex].myCharArray << endl;
}
int main(void)
{
myClass c;
c.setMyStructData();
c.getStructData(1);
cin.get();
}
In your commented code, you are assigning pointers and not an actual copy of the data.
To do what you ask with the code you provided you may do:
// Check that info isn't null
if (!info) {
return;
}
// Simple copy assignment of private structure to info.
*info = myStruct[structureArrayIndex];
This dereferences the pointer info and does a default copy operation of the myStructure type in myStruct array at the structureArrayIndex.
You have to assign content of myStruct[structureArrayIndex] to content of info.
*info = myStruct[structureArrayIndex];
Related
This question already has answers here:
String 'G' is not showing
(2 answers)
Closed 2 years ago.
As you can see below, my function doesn't return the right value in int main() but it does in the function itself. I'm a newbie to c++, can anyone explain to me why or what's the problem? Thanks!
#include <iostream>
using namespace std;
int a[100],n;
void citire(int n)
{
int a[100];
for(int i = 0 ; i < n ; i ++) { // the for loop
cin >> a[i]; // entering the numbers for each
}
cout << a[5] << endl; // returns the right number
}
int main()
{
cout << "n= "; cin >> n; // how many numbers should the vector have.
citire(n); // me calling the function
cout << a[5]; // returns 0
}
Variable shadowing, where variables in different scopes have the same name. int a[100] inside the function citire is allocated on the stack will not persist when it falls out of scope at the end of the function, it's a different array to the global int a[100].
You have two different variables, a local variable and a global variable, but as you have given both of them the same name, you don't see that difference. Let me show you in a clearer way what you have programmed:
#include <iostream>
using namespace std;
int global_a[100],n;
void citire(int n)
{
int local_a[100];
for(int i = 0 ; i < n ; i ++) { // the for loop
cin >> local_a[i]; // entering the numbers for each
}
cout << local_a[5] << endl; // returns the right number
}
int main()
{
cout << "n= "; cin >> n; // how many numbers should the vector have.
citire(n); // me calling the function
cout << global_a[5]; // returns 0
}
Now, you tell me, where did you store any variable in the array global_a?
In order to avoid this, you might do the following:
#include <iostream>
using namespace std;
int global_a[100],n;
void citire(int n)
{
// int a[100]; don't declare a local variable, so while referring to "a",
// the global variable will be used:
for(int i = 0 ; i < n ; i ++) { // the for loop
cin >> global_a[i]; // entering the numbers for each
}
cout << global_a[5] << endl; // returns the right number
}
int main()
{
cout << "n= "; cin >> n; // how many numbers should the vector have.
citire(n); // me calling the function
cout << global_a[5]; // returns 0
}
For your information, the prefixes "local_" and "global_" are just there for clarification purposes. In the last example, you might just write "a" instead of "global_a", the result will be the same.
I'm collecting names and test scores to populate a vector. Both the function and main method can't recognize the struct's members. How can I get it to see the members? Or is there a better way to populate a vector of structs with user input using a function?
I've searched other similar posts, but it seems like it's just a simple code error I missed.
#include <iostream>
#include <vector>
using namespace std;
const int classSize = 1;
struct StudentType {
string studentFName;
string studentLName;
int testScore;
char grade;
};
vector<StudentType> collectStudentData(vector<StudentType> students[classSize]) {
for (int i = 0; i < classSize; i++) {
cout << "Student " << i << "'s name and test score" << endl;
cin >> students[i].studentFName >> students[i].studentLName >> students[i].testScore;
}
return students[classSize];
};
int main() {
vector<StudentType> students[classSize] = {};
students[classSize] = collectStudentData(students);
cout << students[1].studentFName << students[1].studentLName << students[1].studentFName;
};
'studentFName': is not a member of 'std::vector>'
This line creates an array of vectors:
vector<StudentType> students[classSize] = {};
What you want is this a single vector:
vector<StudentType> students;
Where that gets initialized to a zero-length array.
When it comes to adding data you don't need to return from the other method, you can pass in a reference and add to it:
void collectStudentData(vector<StudentType>& students) {
for (int i = 0; i < classSize; i++) {
// Read in one at a time
StudentType student;
cout << "Student " << i << "'s name and test score" << endl;
cin >> student.studentFName >> student.studentLName >> student.testScore;
// Add to the array
students.push_back(student);
}
}
Ideally classSize is either passed in as an argument, or you just type a blank line to end input. Using a global variable is really messy and should be strongly discouraged.
vector<StudentType> students[classSize]
Is one issue. You are not declaring a function that takes a vector, you are declaring a function that takes an array of vectors.
Secondly, if you only applied that change you would be passing an empty vector, you can initialize vector to be a particular size by passing in the size to the constructor.
Furthermore, it seems that you would benefit from passing the students vector by reference
vector<StudentType>& students
instead, the & creates a reference. Right now your code is copying the vector when it is passed into the function
#include <iostream>
#include <vector>
using namespace std;
const int classSize = 1;
struct StudentType {
string studentFName;
string studentLName;
int testScore;
char grade;
};
void collectStudentData(vector<StudentType>& students) {
for (int i = 0; i < classSize; i++) {
cout << "Student " << i << "'s name and test score" << endl;
cin >> students[i].studentFName >> students[i].studentLName >> students[i].testScore;
}
return students;
};
int main() {
vector<StudentType> students{classSize};
collectStudentData(students);
cout << students[0].studentFName << students[0].studentLName << students[0].studentFName;
};
If you wanted to improve the code further, you would use an iterator in the for loop instead, and preferably you wouldn't need to construct the vector in main, and pass it into a function to mutate it. You could just construct it and return it from the function.
I created a simple program to help me understand how to Dynamically Allocate a structure. I want the program to gets 5 names and 5 accounts from the user, and display the names and the accounts. I know a pointer is like a reference variable, the only differences instead of passing the value, it passes the address of the variable. I set a breaking point for line 23 ("getline(std::cin,clientPtr[count].name);"), line 25 ("std::cin.ignore(std::numeric_limits::max(),'\n');"),
line 27 ("std::cin >>clientPtr[count].accounts;"), line 40 ("std::cout <<"Name:" << clientPtr[count].name;"), line 41 ("std::cout <<"Name:" << clientPtr[count].name;"),line 31( showInfo(&client);). When I debugged it shows that line 41 is not executing. It should display the names and the accounts of each client. In this case it's not. I'm not sure why, just a little background on me, I'm new to C++, as well with using the debugger. I'm using xcode 8.2 and the debugger I am using is lldb. I'm here to learn, so anything will help. Thanks.
#include <iostream>
#include <limits>
struct BankInfo
{
std::string name;
std::string accounts;
};
void showInfo(BankInfo*);
int main()
{
BankInfo client;
BankInfo* clientPtr=nullptr;
clientPtr = new BankInfo[5];
for(int count =0; count < 5; count++)
{
std::cout << "Enter your name:";
getline(std::cin,clientPtr[count].name);
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Enter you account number:";
std::cin >>clientPtr[count].accounts;
}
showInfo(&client);
return 0;
}
void showInfo(BankInfo* clientPtr)
{
for(int count =5; count < 5; count++)
{
std::cout <<"Name:" << clientPtr[count].name;
std::cout <<"Account:" << clientPtr[count].accounts;
}
}
You are handing the wrong thing to showInfo(). You have two variables.. a single BankInfo variable and a dynamic allocated array with size 5.
You want to iterate over the latter and not the former.
Changing showInfo(&client);to showInfo(clientPtr); should do the trick perhaps?
So I fixed the solution I made several mistakes, but thank you for the suggestion. Here's what I did.
#include <iostream>
#include <limits>
struct BankInfo
{
std::string name;
std::string accounts;
};
void showInfo(BankInfo*);
int main()
{
BankInfo client;
BankInfo* clientPtr=nullptr;
clientPtr = new BankInfo[5]; //Allocate an array of BankInfo struct on the heap
for(int count =0; count < 5; count++)
{
std::cout << "Enter your name:";
getline(std::cin,clientPtr[count].name); // stores the value in the name member
std::cout << "Enter you account number:";
std::cin >>clientPtr[count].accounts; // stores the value in accounts member
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
showInfo(clientPtr);
delete [] clientPtr;
clientPtr = nullptr;
return 0;
}
void showInfo(BankInfo* clientPtr)
{
for(int count =0; count < 5; count++)
{
std::cout <<"\nName:" << clientPtr[count].name; // dereference the pointer to the structure
std::cout <<"\nAccount:" << clientPtr[count].accounts; // dereference the pointer to the structure
}
}
for(int count=1 ; count<=5 ; count++)
{
//do your stuff here
}
I have an exercise to write down data to a dynamic table of structures using a function. Here's my code:
#include <iostream>
#include <cstdlib>
using namespace std;
struct student{ char name[15], surname[20]; int age; };
student * createTab(int tsize)
{
student *t = new student[tsize];
return t;
}
void fill(student *t, int tsize)
{
for (int i = 0; i<2; i++)
{
cout << "Enter a name: "; cin >> t[i].name;
cout << "Enter a surname: "; cin >> t[i].surname;
cout << "Enter age: "; cin >> t[i].age;
}
}
int main()
{
student *t = createTab(10);
fill(t, 20);
cout << t[0].surname << endl;
cout << t[1].name << endl;
system("pause");
delete[]t;
return 0;
}
It works, okay. But here, in fill() function I use the index syntax with student[].name. I always worked on tables with pointers like that: *(table+i) in a for loop. *(t+i).name doesn't work. Can I iterate on structure fields using pointers?
P.S - Am I freeing the memory correctly?
And I guess P.S 2 - How is it possible, that when I insert a pointer to a first element of my table to a function, and then I can operate on whole table with indexes?
The standard defines the subscripting as follows:
5.2.1/1 (...) The expression E1[E2] is identical (by definition) to *((E1)+(E2))
This is why, using a pointer t and an index i, *(t+i) and t[i] is the same. The problem with your code in the context of struct fields, is a question of priority: you may write (*(t+i)).name or better (t+i)->name, or much clearer, as you did: t[i].name.
P.S.: If you allocate a table with new[...] you have to free it with delete[]. So yes: it's ok !
I tried the code below to return an array with all string ids, but it didn't work.
The output just returns a number. How can I return an array with ids?
#include <iostream>
#include <string>
using namespace std;
string* getArray()
{
int nanim;
cout << "Enter the number of animals: ";
cin >> nanim;
string *id = new string[nanim];
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
cin >> id[i];
}
for ( size_t i = 0; i < nanim; i++ )
{
cout << id[i] << endl;
}
return id;
}
int main()
{
int n;
cin>>n;
string* anim[n]=getArray();
cout<<anim;
return 0;
}
You are returning a pointer to the first element in the array.
To access array elements just having called string* arr = getArray(); you can use arr[0], arr[1], arr[2] etc. to access the strings.
Don't forget to delete the memory you allocated in the function though; at the moment you have a big memory leak.
Generally this is not good programming though since the function caller doesn't know how many elements there are in the returned array. It would be better to get the number of animals in the caller and pass that into your function.
Better still, rebuild your code to use std::vector as I see you're already using stl. Then you don't need to worry (explicitly) about memory allocation and deallocation.
You do not need to read the number of elements twice, and the type of the anim should be string*, not string* []. Unfortunately, this wouldn't tell you the number of items in the array, so you need to get it from the getArray, for example, like this:
string* getArray(int& nanim) {
// Remove the declaration of nanim, and keep the rest of the code unchanged
...
}
int main()
{
int n;
string* anim = getArray(n);
for (int i=0; i != n; i++) {
cout << anim[i] << endl;
}
delete[] anim;
return 0;
}
This is not an optimal C++ solution, though: you would be much better off using std::vector instead of an array, because the vector grows dynamically, and its size is returned along with the container itself. There would be no need to delete[] the result either, which would significantly simplify your code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> getVector()
{
int nanim;
cout << "Enter the number of animals: ";
cin >> nanim;
vector<string> res;
for ( size_t i=0; i < nanim; i++ )
{
cout<< "\nEnter id anim "<< i+1 << ": ";
string tmp;
cin >> tmp;
res.push_back(tmp);
}
return res;
}
int main()
{
vector<string> anim = getVector();
for ( size_t i = 0; i < anim.size(); i++ )
{
cout << anim[i] << endl;
}
return 0;
}