Code doing something Crazy

Pages: 123
I am telling you Arrays are by default pass by reference.
everything i am doing to the array in newjournal fills the array created in main. then allows me to pass it from main to any other place in my program. yes it would be ez to declare it as a global array but then i wouldnt be meeting the requirements of the program i am supposed to create.
Last edited on
Really?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;


void determinant(char guy[])
{
	guy="not wrong.\n";
}

int main()
{
	char guy[13]="wrong.\n";
	cout << "Albatross thinks that you're " << guy;
	determinant(guy);
	cout << "After compiling this program, Albatross thinks that you're " << guy;
	cin.get();
	return 0;
}	

Albatross thinks that you're wrong.
After compiling this program, Albatross thinks that you're wrong.


Compiled using GCC 4.2.1 for i686-apple-darwin10.


-Albatross
Last edited on
why do you keep saying that the newjournal function doesnt do anything then? that is not even my problem. i am just having issues printing out something in the center of the screen instead of aligned left or right.
amaac wrote:
I am telling you Arrays are by default pass by reference.

That is absolutely correct.

@Albatross:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string.h>
using namespace std;

void determinant(char chary[])
{
	strcpy(chary,"not wrong.\n");
}

int main()
{
	char guy[13]="wrong.\n";
	cout << "Albatross thinks that you're " << guy;
	determinant(guy);
	cout << "After compiling this program, Albatross thinks that you're " << guy;
	cin.get();
	return 0;
}

Last edited on
All right. I'm just pointing out another problem.

EDIT: @m4ster r0shi: Perhaps by using strcpy, something changed. Either way... something doesn't strike me as right about this. I'll do some reading.

EDIT2: Indeed, I was wrong. However, that goes to show that you can't set values of all elements at once in arrays using =;

When you say center, what do you mean? You want the output to be centered relative to the size of the window that it's being printed in? Tell me, because I haven't been on Windows in ages: are Windows consoles enlargeable in terms of width? If they are not, then figure out how long that value is, subtract the length of your string from it, and halve it to determine how many spaces you need before your string.

-Albatross
Last edited on
yes that is exactly what i am doing in centerstring() but i am getting crazy output. i don't care if window is re sizable or not i am only viewing it on one size. i picked it to be 80 across

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
#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[], 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);
                cout << year[4].year << year[4].months << year[4].dinmo << year[4].firstDayinmo;
                //ess(year, miny);
            break;
            case 2:
                //getJournal(year);
                //ess();
            break;
    }
    return 0;
}

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);
        }
    }
}


this compiles and runs and prints year out except the vector
Last edited on
Albatross wrote:
@m4ster r0shi: Perhaps by using strcpy, something changed. Either way... something doesn't strike me as right about this. I'll do some reading.


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
#include <iostream>
using namespace std;

void determinant(char chary[])
{
	chary[0]='n';
	chary[1]='o';
	chary[2]='t';
	chary[3]=' ';
	chary[4]='w';
	chary[5]='r';
	chary[6]='o';
	chary[7]='n';
	chary[8]='g';
	chary[9]='.';
	chary[10]='\n';

}

int main()
{
	char guy[13]="wrong.\n";
	cout << "Albatross thinks that you're " << guy;
	determinant(guy);
	cout << "After compiling this program, Albatross thinks that you're " << guy;
	cin.get();
	return 0;
}

:P :D
Last edited on
ok now that we all agree i am still getting crazy output for

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
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();
}


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

all centered though :)
@m4ster r0shi: I said I'd do some reading. Experiments only have so much value to me when used in this context.

@OP: Your centerstring appears to be fine, then... what's wrong is printMonth...

Let's go over this. For printMonth, what formatting do you want? It seems you want the days of the week first, and then a list of months, several per line? Is that it?

-Albatross
Last edited on
lets say month may
May centered
underneath is the Su M T W Th F S
and underneath that is the numbers of the calender. with the first day of the month starting where it is supposed to start.

i have posted all of the code minus the commented parts.

if you comment 193 192 188 and add in the comments on line 194 and 196 then it prints what i want just numbers not centered. doh
Last edited on
So, like this?
May
Su M T W Th F S
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
28 29 30 31


-Albatross
yes but the spacing that i have in my code makes it so that all the days of week and numbers line up. and without 0s infront of first 9 #
It might be easier on you if you converted those numbers in your loops to characters to handle in your string. Fortunately, every character has a numerical representation...
http://web.cs.mun.ca/~michael/c/ascii-table.html

I don't know if this will help (I even forgot that arrays were effectively pointers, geez), but it might...

-Albatross
wow that might just be it. working stringstream to make int string i dont think i can use itoa()
ahh when i do

1
2
string strbuffer;
strbuffer += (k+1) + " ";


is that making my k value + 1 into a string or is it still an int? because i pass my strbuffer and i am not sure where to run my itoa() or even if it works because i dont know where to call it at.

1
2
3
4
5
6
7
8
9
string iToa(int i)
{
string a;
stringstream out;
out << i;
a = out.str();
return a;
}
k is in theory still an in int, and therefore compatible with the char type. However, I think that it doesn't make it into the character you think it is (see ASCII table).
http://cplusplus.com/reference/string/string/operator+=/

EDIT: Something is wrong, though, because I saw no random characters in your output... maybe += does the conversion for you? I don't know, ask me again after I get my caffeine supplements.

@Others: If I'm wrong about that, do correct me.

Be careful using itoa(). It is not a standard function, and is unsupported by some compilers. I'd just add 48 to your integer and do an explicit type cast.

-Albatross
Last edited on
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
for (int j = 0; j < (7-a.firstDayinmo%7); j++)
    {
        cout << "   ";
        dayctr ++;
        if (dayctr%7 == 0)
        {
            cout << "\n";

        }
    }
    for (int k = 0; k < a.dinmo; k++)
    {centerstring(strbuffer);
        if (dayctr%7 == 0)
        {
            centerstring(strbuffer);
            cout << "\n";

        }
        if (k < 9)
        strbuffer += " ";//cout << " "; //if single digit then allign

        strbuffer += iToa(k+1) + " ";//cout << (k+1) << " ";
        dayctr ++;
        //cout << strbuffer;
    }
}

string iToa(int i)
{
string a;
stringstream out;
out << i;
a = out.str();
return a;
}


Got it to print the numbers but cant get them to center now HAH just my luck.. thankfully i am almost done with the other parts.


Last edited on
Does stringstream add any \n chars when it outputs a string?
I don't think so... it only outputs what it contains.

-Albatross
I got it!!! Thankyou Albatross and m4ster r0shi. you guyz helped a lot.
Pages: 123