challenging exercise

Hi guys,

I find myself in no mans land when it comes to C++,I feel like I need to get over "programmer fear" and write something useful,

I have been on this forum 2 years in January and I have not given up,I love programming and love seeing the end result of your work,I am not big on problem solving though but I'm growing to like it,so like I said it's growing on me.

Anyway I want someone to help me out here I want to write an application,a console application that would be useful in every day life,not something like reverse a string,sort an array,factorials etc I want something that will challenge me but focus on the topics I mainly know,


I have quite a solid base in the following

OOP,structs/classes,pointers,pointer to pointers etc,references,polymorphism,c style strings,iteration,have a decent idea of how memory works the stack,heap,EBP,ESP,return address etc,basic I/O,enums,conditions,decent logic I guess(room for improvement here),some data structures such as linked lists,doubly linked lists,binary search trees,etc.

So yeah I pretty much have the basics down now I want to write something that will challenge me(but not leave me pulling my hair out) and give me more confidence

maybe something you wrote when to bring you to the next level,and if you made it this far into my post thank you =)

Thanks

Last edited on
In an operating system of your choice, create a black window and draw a triangle on it. Use whatever libraries you see fit. Low level libraries will help you learn more than high level libraries.
Perhaps something like an address book, using standard containers like std::vector, std::string, std::map, etc. Store the information into a file, formatted in as a Vcard file format: https://en.wikipedia.org/wiki/VCard or some other format of your choice. If you try this make sure you keep your user interface separate from the program logic so if you decide to move to GUI you will only need to change the user interface and not the actual program logic.




Thanks guys I will give them a shot =)

and once finished(fingers crossed) I'l post them here
hey guys

I decided to go with the phone book first it took me about 25 minutes to do,now it works fine because it only contains 21 names but if it were to be a bigger phone book I may need to use records these records will point to the place where the name begins in the file,because a large phone book would take a lot of memory up so it would better practice to keep records in a map or vector these records will be ints I guess( I think) these ints would take a lot less space in memory than a struct

so here is the finished code so far I think it is pretty tidy,I didn't use any globals

any feed back would be great

I may now look at making a bigger book with more names using records

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <stdlib.h>

using namespace std;

struct address{

    string name;
    string firstLine;
    string city;
    string state;

    address(string n,string f,string c,string s){

        name = n;
        firstLine = f;
        city = c;
        state = s;
    }

    address(const address& other){

       name = other.name;
       firstLine = other.firstLine;
       city = other.city;
       state = other.state;
    }

    address& operator=(const address& other){

       name = other.name;
       firstLine = other.firstLine;
       city = other.city;
       state = other.state;

       return *this;
    }
};

void populateBook(ofstream& out){

     for(int i = 0; i < 20;i++){

     string name;
     string first;
     string city;
     string state;

     cout << "enter full name" << endl;
     getline(cin,name);
     cout << "enter first line of address" << endl;
     getline(cin,first);
     cout << "enter city " << endl;
     getline(cin,city);
     cout << "enter state" << endl;
     getline(cin,state);

     out << name << endl;
     out << first << endl;
     out << city << endl;
     out << state << endl;
     }
}

bool readFile(ifstream &in,vector<address> &adr){

      if(in.is_open()){

      while(!in.eof()){

        string name,first,city,state;
        getline(in,name);
        getline(in,first);
        getline(in,city);
        getline(in,state);

        if(!in){

            return true;
        }
        address temp(name,first,city,state);
        adr.push_back(temp);
      }
        return true;
      }
     return false;
}

void printInfo(vector<address> &addr){

    cout << "print all names in phone book..." << endl;
    cout << endl;

  for(int i = 0; i < addr.size(); i++){

        cout <<  "Name : " << addr[i].name << endl;
        cout <<  "First Line : " << addr[i].firstLine << endl;
        cout <<  "city : " << addr[i].city << endl;
        cout <<  "State : " << addr[i].state << endl;
        cout << endl;
    }
}

void printByState(vector<address> &addr){

    string choosenState;
    cout << "print names by state" << endl;
    cout << "enter two digit code of state" << endl;
    cin >> choosenState;
    bool found = false;

    for(int i = 0; i < addr.size(); i++){

       if(choosenState == addr[i].state){

        cout <<  "Name : " << addr[i].name << endl;
        cout <<  "First Line : " << addr[i].firstLine << endl;
        cout <<  "city : " << addr[i].city << endl;
        cout <<  "State : " << addr[i].state << endl;
        cout << endl;
        found = true;
       }
    }
    if(!found){

        cout << "no names in that state" << endl;
    }
}

int main()
{

    ofstream out;
    ifstream in;
    vector<address> addresses;

  //  out.open("phoneBook.txt",ofstream::app);
      in.open("phoneBook.txt");

    if(!in.is_open()){

        cout << "error" << endl;
        return 1;
    }
    if(!readFile(in,addresses)){

        cout << "error" << endl;
        return 1;
    }

    while(true){

        cout << "WELCOME TO THE PHONE BOOK" << endl;
        cout << "--------------------------------" << endl;
        cout << "ENTER : 1 to print all names,2 print by state,3 to quit application" << endl;
        int choice;
        cin >> choice;
        system("cls");

        switch(choice){

    case 1:
        printInfo(addresses);
        break;
    case 2:
        printByState(addresses);
        break;
    case 3:
        return 0;
    default:
        cout << "SORRY : invalid choice" << endl;
        }
    }
}
Okay this is a staring point that shows your current level of ability, now to clean it up a little.

First when passing non-trivial classes (for example std::string) into functions it is usually a good idea to pass them into the functions by reference, const qualifying when appropriate, instead of by value as you are currently doing.

Second you should start using initialization lists with your constructors whenever possible.

Third you should strive to initialize class instances when you define them instead default initializing them and then using some method to alter their values. For example:

The following is a bad example:
1
2
3
4
5
6
7
...
    ifstream in;
    vector<address> addresses;

  //  out.open("phoneBook.txt",ofstream::app);
      in.open("phoneBook.txt");
...


Normally you should prefer something more like:

1
2
3
4
5
6
...
    vector<address> addresses;
...

      ifstream in("phoneBook.txt");  // Prefer using the constructor whenever possible.
...


And note you should be defining your variables closer to first use, not in one big glob at the beginning of scopes. Also note that you really should be testing the stream for all errors, not just checking if the stream is open or not. Something more like if(!in) instead.

Fourth using using eof() to control a read loop will cause problems when not used correctly, such as how you're using it in this program. You should prefer to use the actual read operations to control the read loop.

1
2
3
4
5
...
    while(getline(in,name))
    {
          // Do whatever.
...


So yeah I pretty much have the basics down now I want to write something that will challenge me(but not leave me pulling my hair out) and give me more confidence


Okay since you want a C++ challenge you need to start thinking in C++, ie: use classes instead of structures (private data), using class methods (public accessors) instead of everything being non-class functions, etc.

Also, since you want more challenge your input file should not have every field on it's own line.

There is more but fixing the issues above will be a start.

This is a very very simple start you'll probably want to complicate things as you move along. You need to think about "real people", do these "real people" always have only one type of address where you contact them? Do you always need to go to their address to contact them? Etc., etc., etc.




Topic archived. No new replies allowed.