Bad syntax habit prevention

Hi, I'm just beginning to learn about structures, pointers, and references. I'm trying to practice what I've learned. I understand the idea of these new concepts, but when I'm having trouble knowing when and how to use them. In order to practice I've started a simple contact list program. I am hoping that you guys could help point out some instances where the use of structures, pointers(non-existent), and references are used correctly or incorrectly, and where they could be used correctly. This would really help me understand what I'm doing before my program turns into a big ball of confusion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct personInfo
{
    string first_name;
    string middle_name;
    string last_name;
    string birthdate;
    string phone_number;
    string email_address;
};

//Store file in string variable and return it.
string getFile()
{
    string in_file;
    string line;
    ifstream reader( "C:/Users/Ryan/Desktop/data.txt" );
    if ( ! reader )
    {
        cout << "Error." << endl;
    }
    for (int i = 0;! reader.eof(); i++)
    {
        getline( reader, line );
        in_file.append( line );
        in_file.append( "\n" );
    }
    reader.close();
    return in_file;
}

//Write old and new information in file.
void writeFile( string& in_file, personInfo& contact )
{
    ofstream writer( "C:/Users/Ryan/Desktop/data.txt", ios::out );
    if ( ! writer )
    {
        cout << "Error." << endl;
    }
    writer << in_file << endl;
    writer << "First name: " << contact.first_name << endl;
    writer << "Middle name: " << contact.middle_name << endl;
    writer << "Last name: " << contact.last_name << endl;
    writer << "Date of birth: " << contact.birthdate << endl;
    writer << "Phone number: " << contact.phone_number << endl;
    writer << "Email address: " << contact.email_address << endl << endl;
    writer.close();
}

//Specify new person details.
personInfo newPerson( personInfo& contact )
{
    string input;
    cout << "First name: ";
    cin >> input;
    contact.first_name = input;
    cout << "Middle name: ";
    cin >> input;
    contact.middle_name = input;
    cout << "Last name: ";
    cin >> input;
    contact.last_name = input;
    cout << "Date of birth: ";
    cin >> input;
    contact.birthdate = input;
    cout << "Phone number: ";
    cin >> input;
    contact.phone_number = input;
    cout << "Email address: ";
    cin >> input;
    contact.email_address = input;
    return contact;
}

void displayAll( string& in_file )
{
    cout << in_file;
}

int main()
{
    vector<personInfo> contactList;
    string in_file;
    for ( int i = 0; i <= contactList.size(); i++ )
    {
        personInfo contact;
        cout << "People information\n\n";
        newPerson( contact );
        contactList.push_back( contact );
        string in_file = getFile();
        writeFile( in_file, contactList[i] );
        displayAll( in_file );
    }
}


The code is still very under cooked. Again, this topic is more concerning the syntax of the program.
closed account (Dy7SLyTq)
how come you arent using methods and constructors?
I didn't know I could use those with structures. I thought that was only for use of classes. Would I just place the functions within the structure as if it were an entirely public class, then call on them using methods?
closed account (Dy7SLyTq)
in c you would be correct. structs are similar to classes in c++. methods are are functions either defined or declared in a class. you can have them public or private
Would it be beneficial to to define and declare the functions inside of the struct? If so, how would it be beneficial? Also, are constructors used the same way they would be in a class?
closed account (Dy7SLyTq)
to your second question: yes
to the first: it depends. a method is usually a function that pertains to the struct or class. like if you made a function that printed everything in a vector, regardless of type, you wouldnt put that in your class, but if you made one that just did a vector of your structs, you would want to put that in it
Okay, I am understanding this as how to organize the functions. If the function has only to do with the struct, then to make that function a method of the struct would enhance readability.

How about references and pointers: Am I referencing correctly in this code? Are there any places that a pointer might better the efficiency?
closed account (Dy7SLyTq)
in yours? not really. in something like making a class to generate sdl windows? absolutely
So should I not worry about allocation of memory until my programs begin to slow?
closed account (Dy7SLyTq)
yeah. in a program this small dont worry about it. when you get into s*l library or low level stuff, i would start getting into that stuff
Sounds good to me. I suppose I'll just read on through until I come across something more applicable.

Thanks!
Topic archived. No new replies allowed.