Code doing something Crazy

Pages: 123
I am creating a journal that saves and loads from a .txt file.
I am having some problems with a simple center alignment of my print month function, my function is printing out values that i have never set it equal to.


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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <iostream>
#include <fstream>
#include <sstream>
#include "CinReader.h"
#include <vector>
#include <string>

using namespace std;

CinReader reader;
struct entry
{
    string day;
    string event;
};
struct month
{
        int year;
        string months;
        int firstDayinmo;
        vector<entry> thismonth;
        int dinmo;
};

void newJournal(month a[], int size);
void viewEdit (month a[], int size);
void ess(month a[], int size);
void printMonth (month a);
void centerstring(string& buff);
void clearScreen ();
//void save (month a[12], int size);
//void getJournal (month a[], int size);
//int monthCheck(int month,int year, month a[12]);



int main()
{
    const int miny = 12;
    month year[miny];
    //int rows = 6;
    //int days = 7;
    //int array [rows][days];
    cout << "Would you like to create a new Journal or work on an existing one?\n"
        << "1. Start new Journal\n"
        << "2. Work on existing one.\n";
    int userchoice = reader.readInt(1,2);
    switch (userchoice)
    {
            case 1:
                newJournal(year, miny);
                ess(year, miny);
            break;
            case 2:
                //getJournal(year);
                //ess();
            break;
    }
    return 0;
}
void ess(month a[], int size)
{
    bool quit = false;
    while (quit == false)
    {
        system ("cls");
        int mainchoice;
        cout << "What would you like to do?\n"
            << "1. View and/or Edit Entry.\n"
            << "2. Search an Entry.\n"
            << "3. Save & Quit.\n"
            << endl;
        mainchoice = reader.readInt(1,3);
        switch (mainchoice)
        {
            case 1:
                viewEdit(a,size);
            break;
            case 2:
                //search(a,size);
            break;
            case 3:
                quit = true;
            break;
        }
    }
}
void viewEdit (month a[], int size)
{
    bool quitEdit = 0;
    while (quitEdit == 0)
    {
        bool quitMonth = 0;
        cout << "Which month would you like?\n";
        for (int i = 0; i < size; i++)
        {
            cout << (i+1) << ". " << a[i].months << endl;
        }
        int usermonth = reader.readInt(1,12);
        int userchoice;
        usermonth -= 1;
        clearScreen();
        printMonth (a[usermonth]);
        while (quitMonth == 0)
        {
            cout << "Which day would you like? 1 - " << a[usermonth].dinmo << endl;
            int userday = reader.readInt(1,a[usermonth].dinmo);
            userday -= 1;
            //in the array a sub usermonth inside vector sub userday get day and event
            cout << a[usermonth].thismonth[userday].day << endl;
            cout << a[usermonth].thismonth[userday].event << endl;

            cout << "\n1. Replace entry?\n"
            << "2. Add to entry?\n"
            << "3. Back\n"
            << endl;
            userchoice = reader.readInt(1,3);
            switch (userchoice)
            {
                case 1:
                    a[usermonth].thismonth[userday].event = reader.readString();
                break;
                case 2:
                    a[usermonth].thismonth[userday].event = a[usermonth].thismonth[userday].event + "\n" + reader.readString();
                break;
                case 3:
                    clearScreen();
                break;
            }
            cout << "\n1. Work on another day.\n"
            << "2. Back\n"
            << endl;
            userchoice = reader.readInt(1,2);
            switch (userchoice)
            {
                case 1:
                clearScreen();
                break;
                case 2:
                quitMonth = 1;
                break;
            }

        }
        cout << "\n\n1. Work on another month.\n"
            << "2. Back\n"
            << endl;
        userchoice = reader.readInt(1,2);
        switch (userchoice)
        {
            case 1:
            clearScreen();
            break;
            case 2:
            quitEdit = 1;
            break;
        }
    }
}
void printMonth (month a)
{
    int dayctr = 7;
    string strbuffer;
    string dayofweek[7] = {"Su  ","M  ","T  ","W  ","Th ","F ","Sa"};

    strbuffer = a.months;
    centerstring (strbuffer);


    //print days of week
    for (int i = 0; i < 7; i++)
    {
        strbuffer += dayofweek [i];
    }
    centerstring (strbuffer);
    cout << "\n";
    //decide where to start printing numbers
    for (int j = 0; j < (7-a.firstDayinmo%7); j++)
    {
        cout << "   ";
        dayctr ++;
    }
    for (int k = 0; k < a.dinmo; k++)
    {
        if (dayctr%7 == 0)
        {
            cout << "\n";
            centerstring(strbuffer);
        }
        if (k < 9)
        cout << " "; //if single digit then allign
            strbuffer += " ";
        strbuffer += (k+1) + " ";
        //cout << (k+1) << " ";
        dayctr ++;
        //cout << strbuffer;
    }
}
void centerstring(string& buff)
{
    int l= buff.length();
    int position=((80-l)/2);
    for(int i = 0; i<position; i++)
    cout<<" ";
    cout<< buff << endl;
    buff.clear();
}





void newJournal(month a[], int size)
{
    int firstDaysinmo[12] = {12,8,8,11,13,9,11,7,10,12,8,10};
    int daysInmonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    string monthset[12] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
    string dayofweek[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    cout << "Please tell me what year it is.\n";
    int useryear = reader.readInt(2010,2020);
    for (int i = 0; i < size; i++)
    {
        a[i].year = useryear;
        a[i].months = monthset[i];
        a[i].dinmo = daysInmonth[i];
        a[i].firstDayinmo = firstDaysinmo[i];

        for (int j = 0; j < daysInmonth[i]; j++)
        {
            entry e;
            e.day = dayofweek [((firstDaysinmo [i] + (j))%7)];
            e.event = "empty";
            a[i].thismonth.push_back(e);
        }
    }
}

void clearScreen ()
{
#ifdef WIN32
    system("cls");
#else
    system("clear");
#endif
}




Output
May
su m t w th f sa
January anuary nuary uary
ary ry yFebuaryebuarybuary
Blah blah blah to April

i could take out the center string and strbuffer and add in the lines in comments. it would make my output include the days but they would be aligned to the left. Any help?






Last edited on
nobody had any ideas??
anything is helpful get me thinking differently.
Could we see main?

And when you've decided to use C or C++ strings, please stick to one. Here's a few functions to help you out:
http://cplusplus.com/reference/string/string/

-Albatross
1. What's the point of str in centerstring()?
2. Why is it never deleted?
3. Why does centerstring() clear buff when all it really needs to do is send it to std::cout?
4. What exactly are you doing on line 60? C++ is strongly typed.
5. Why does dayofweek need to be an array of 7 std::strings, rather than a single std::string?
Last edited on
i posted main. my strings are hidden in the struct month. i call strings from the array of 12 months that i have created in main.
1. What's the point of str in centerstring()? str is array of chars i kept getting error saying it couldnt convert string to chars something wouldnt work so i put done into make it work
the function works fine when i pass it the month in line 34 35
I am new to strings all i wanted to do was center my month on the console
2. Why is it never deleted?
forgot to delete hey maybe the problem

3. Why does centerstring() clear buff when all it really needs to do is send it to std::cout?
i passed buff & so i could always delete it and use again. or that was the idea.

4. What exactly are you doing on line 60. C++ is strongly typed.
60 is printing k value which is the # day it should print 1-31 or whatever the days in month is.

5. Why does dayofweek need to be an array of 7 std::strings, rather than a single std::string?
because there are 7 days in a week and it allows me to print them out separately later when i ask what day they want to work on. << not done yet.


my issue is obviously in centerstring. i am just not familiar with the string class enough to make it work

i wanted a string buffer that added my strings together untill it reached the /n then output them on the line in the middle of colsole. i am perhaps doing it all wrong.
Last edited on
Use the string class, not <string.h>. Trust me.
http://cplusplus.com/reference/string/string/

-Albatross
i did i just changed the code to
1
2
3
4
5
6
7
8
9
void centerstring(string& buff)
{
    int l= buff.length();
    int position=((80-l)/2);
    for(int i = 0; i<position; i++)
    cout<<" ";
    cout<< buff << endl;
    buff.clear();
}


but same results as the other way no change at all
to be honest i was not aware of the difference but now i am after seeing your link.
Last edited on
WAIT.

Why are you clearing buff?

Note: Your newJournal is a function that doesn't output anything. So you know.

-Albatross
Last edited on
first it outputs then clears so i can reuse buff without clearing it in the function every time i want to use it.
unless i am mistaken in the functionality of clear. in my code it makes sense to me pass a string allow function to change it. center your string and output it. then reset the string given.

new journal is not supposed to output anything. just creates and fills the array and vector

btw ess is just menu
Last edited on
But...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
centerstring (strbuffer);
    cout << "\n";
    //decide where to start printing numbers
    for (int j = 0; j < (7-a.firstDayinmo%7); j++)
    {
        cout << "   ";
        dayctr ++;
    }
    for (int k = 0; k < a.dinmo; k++)
    {
        if (dayctr%7 == 0)
        {
            cout << "\n";
            centerstring (strbuffer);
        }


strbuffer isn't set to anything between the two centerstrings here, is it?

newJournal creates a and fills local array and vector, however they're strictly local. There's no reference anywhere and no global variables...

-Albatross
newjournal fills array and vector.. the array is actually declared in main. and passed to newjournal to fill. all the vectors and everything is filled in the newjournal. when it is passed. so is everything else.

i see the issue with strbuffer not set to anything but i am not sure where else to put it. that if statement is checking to see if you are at the end of the week and should start a new line. that is also when i want to print out my string when there will be no more strings being added.
Last edited on
Never mind, that bit that was here won't work. Although, what amaac had wouldn't work either.

It's simple: Don't clear the buffer. :)

-Albatross
Last edited on
umm it was my understanding that arrays are always pass by reference. therefore i didnt think i needed to add the &. i may be wrong but everything else in the code works this way without &

I took the buffer out and got the following output May MaySu M T W Th F Sa \n MaySu M T W Th F Sa January anuary.... for 7-8 lines or so it gets to Aprilprilrilil

so wierd the only place that i have all those words together is in newJournal and it is a local array. i dont understand why it decreases the value of the string and prints it out again.
Last edited on
Actually, they're passed as pointers. In parameter declarations, T a[] and T *a are synonyms. Shut up, Albatross, and learn the syntax.
Last edited on
i have posted all of the code minus the commented parts. i have more to write i get annoyed at the little things i guess.

if you comment 193 192 188 and add in the comments on line 194 and 196 then it prints what i want just not centered. doh

and i am pretty sure that arrays get changed and passed around just fine in my code if you want to check run it yourself the other functions to edit and view are the same. they work fine. besides print month called within.
Last edited on
Then how would you modify a non-globally declared array declared like so type array[]; from an outside function? Please stop flaming, helios (irony).

-Albatross
Last edited on
Do you want to modify the array, or the pointer (if there is one)?
hmm in print journal i should not be modifying anything or i could pass it a pointer that points to array. but that is stupid because i only passed printmonth(month a) which is a month with a vector in it. not an array
i still dont understand my output though or why it is receiving all the months names when i only passed it one month out of the array.
In newJournal, I meant, that function only modifies its own internal variables. You can't use month &a[], as I originally thought, however that function does almost nothing as far as your larger program is concerned.

-Albatross
Pages: 123