Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 04:27

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[help] new, delete, struct

Discussion on [help] new, delete, struct within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
[help] new, delete, struct

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;   
}
Hikarim is offline  
Old 01/20/2013, 12:33   #2
 
beefm4ker's Avatar
 
elite*gold: 0
Join Date: Dec 2009
Posts: 572
Received Thanks: 90
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)
beefm4ker is offline  
Thanks
1 User
Old 01/20/2013, 12:40   #3
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
Oh, works great, thank you
Hikarim is offline  
Old 01/20/2013, 12:41   #4
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
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:
Nightblizard is offline  
Thanks
1 User
Old 01/20/2013, 13:12   #5
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
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
Hikarim is offline  
Old 01/20/2013, 13:18   #6
 
TIMΣ™'s Avatar
 
elite*gold: 23
Join Date: Oct 2010
Posts: 2,986
Received Thanks: 357
Code:
#include <string>
Add this
TIMΣ™ is offline  
Old 01/20/2013, 23:35   #7
 
elite*gold: 0
Join Date: Apr 2012
Posts: 372
Received Thanks: 21
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...
Hikarim is offline  
Old 01/22/2013, 20:58   #8

 
Delinquenz's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 1,160
Received Thanks: 232
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.
Delinquenz is offline  
Old 01/22/2013, 23:45   #9



 
+Yazzn's Avatar
 
elite*gold: 420
Join Date: Jan 2012
Posts: 1,082
Received Thanks: 1,000
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.
+Yazzn is offline  
Thanks
2 Users
Old 01/23/2013, 23:00   #10

 
Delinquenz's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 1,160
Received Thanks: 232
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.
Delinquenz is offline  
Reply


Similar Threads Similar Threads
Struct Allokieren ?
12/08/2012 - C/C++ - 11 Replies
Hallo, Ich arbeite mich gerade durch das Buch C++ A - Z und bin gerade bei den Structuren, so jetzt habe ich mir als beispiel ein kleines (Telefon Buch) gemacht ganz simpel (Vorname, Nachname, Geschlecht, Nummer) Code #include "stdafx.h" using namespace std;
[S] Warrock Struct Logger
11/17/2012 - WarRock - 1 Replies
Topic. Skype: epvp.pinkbeatz
struct and classes
08/03/2012 - CO2 Programming - 6 Replies
took about 50 pages reading about difference between structs and classes , so i thought it's good idea to sum up this for whoever want to get it with a spoon , feel free to correct or add information at struct cannot have instance field initializers in structs struct time { private int x = 5; // compile-time error } Structs cannot contain explicit parameterless constructors struct time {
[c++] struct
08/25/2011 - C/C++ - 2 Replies
Kann closed werden. sry
Packet Struct's
05/02/2009 - Kal Online - 30 Replies
Mir war langweilig hier nen beispiel wie ihr wie ich finde alles bischen besser aussehn lassen könnt :P struct PacketsSend { DWORD VersionCheck; DWORD Login; DWORD Ping; DWORD Skill; DWORD Chat;



All times are GMT +1. The time now is 04:27.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.