[help] new, delete, struct

01/20/2013 12:11 Hikarim#1
Well, i have a structure, input and output functions, to get some data, now i would like to make the following:
-the user inputs a number (for which i need a dynamic memory reservation)
-and that number declares how many times the input should loop)

so for example the use puts in 4; it will run the input function 4 times he can tpye in stuff for 4 different companyes or w/e ... same with the output

Here is waht i ahev for now:

Code:
#include <iostream>



using namespace std;



struct comp{
       string name;
       int obr;
       int bill;
       int days;
       
};


void input(comp &x){
     cout << "Name: ";
     cin >> x.name;
     cout << endl;
     cout << "Obr: ";
     cin >> x.obr;
     cout << endl;
     cout << "Bill: ";
     cin >> x.bill;
     cout << endl;
     cout << "Days: ";
     cin >> x.days;
     cout << endl; 
}

void output(comp x){
     cout << "Name: " << x.name << endl;
     cout << "Obr: " << x.obr << endl;
     cout << "Bill: " << x.bill << endl;
     cout << "Days: " << x.days << endl;
     
}

int main(){


cout << "Input data: " << endl;

comp x;

input(x);
output(x);



system("PAUSE");
return 0;   
}
01/20/2013 12:33 beefm4ker#2
you could for example use an array.... Here's the code:
Code:
#include <iostream>



using namespace std;



struct comp{
       string name;
       int obr;
       int bill;
       int days;
       
};


void input(comp &x){
     cout << "Name: ";
     cin >> x.name;
     cout << endl;
     cout << "Obr: ";
     cin >> x.obr;
     cout << endl;
     cout << "Bill: ";
     cin >> x.bill;
     cout << endl;
     cout << "Days: ";
     cin >> x.days;
     cout << endl; 
}

void output(comp x){
     cout << "Name: " << x.name << endl;
     cout << "Obr: " << x.obr << endl;
     cout << "Bill: " << x.bill << endl;
     cout << "Days: " << x.days << endl;
     
}

int main(){

comp *x;
int count;

cout << "Input data: " << endl;
cout <<"How many inputs?" << endl;
cin >> count;

x = new comp[count];
for (int i=0; i < count; i++)
{
input(x[i]);
output(x[i]);
}



delete [] x;
x = NULL;

system("PAUSE");
return 0;   
}
(untested)
01/20/2013 12:40 Hikarim#3
Oh, works great, thank you :)
01/20/2013 12:41 Nightblizard#4
What exactly is your problem? It seems to me that you already know what to do, why just don't do it?

Oh, and by the way. You should not use new and delete for this kind of stuff. Use the class "vector" instead. It is much safer and in general less error-prone!

So for example:
[Only registered and activated users can see links. Click Here To Register...]
01/20/2013 13:12 Hikarim#5
I knew what to do, but i didnt know 100% how to code it, and when i tryed to loop it on my own, it printed out the same result for all, not as it was entered, but its solved now .)... and i had to use new and delete, the exercise requested it :)
But thanks for the info anyway :)
01/20/2013 13:18 TIMΣ™#6
Code:
#include <string>
Add this
01/20/2013 23:35 Hikarim#7
Ok, one more thing, how can i write to a .txt file?
i want to write the output of this to a .txt file:
Code:
for (int i=0; i < count; i++)
{
input(x[i]);
output(x[i]);
}
i checkedout the writing samples on cpp.com, but didnt get anyhting to work for me... there are only examples when you have just cout, but i have a function call...
01/22/2013 20:58 Delinquenz#8
You can include <fstream> to use the i/o file stream class.

Initialize with std::fstream fileTxt; a fstream and open it with fileTxt.open("data.txt"); .

After that you can write in it with fileTxt << "lol"; . If you want to set a new line after that informations use \n.
01/22/2013 23:45 +Yazzn#9
Quote:
Originally Posted by Delinquenz View Post
At the end you have to close it with fileTxt.close(); .
Why? Just let the dtor do its job.
01/23/2013 23:00 Delinquenz#10
Quote:
Originally Posted by Yazzn (: View Post
Why? Just let the dtor do its job.
I'm quiet new to C++, thanks for the tip.