GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Jan-2004, 13:21
fehyd fehyd is offline
New Member
 
Join Date: Jan 2004
Posts: 6
fehyd is on a distinguished road

accumulator position issues


ok, I have the standard calendar program that's assigned in the beginning stages of C++.

right now, I'm just running it to display january, but I am screwing up where my acumulators go... This is mostly a logic problem that has my brain running loops. The more I look at it, the more my head starts spinning.



CPP / C++ / C Code:
void printJan(double dow)//I know I will eventually have to pass this by reference to get the next months

{
int day=1;
cout<<" ************ "<<endl;
cout<<" * January * "<<endl;
cout<<" ************ "<<endl;
cout<<" S M T W R F S"<<endl;
cout<<"*********************"<<endl;

while(day<=31)
if(dow<6)
{
cout<<setw((dow * 3) + 3)<<day;
dow++;
day++;
}
else
{
cout<<setw((dow * 3) + 3)<<day<<endl;
dow++;
day++;
}
}




"dow" is a double variable set earlier in the code

I have the right formulas (as far as I know) but it's the order and position of the accumulators that has me messed up.
it's running correctly as far as the numbers go, but I cant seem to get my numbers to line up correctly. Please help?

Joshua O'Campo
  #2  
Old 31-Jan-2004, 14:09
fehyd fehyd is offline
New Member
 
Join Date: Jan 2004
Posts: 6
fehyd is on a distinguished road
from what I can tell... the formula I am using should only be used for the first day...

CPP / C++ / C Code:
cout<<setw((dow * 3) + 3)<<day;

I'm using this because there are 3 spaces per day... so, if the day of the week is thursday(dow = 4), it should look like this:

position 0 1 2 3 4 5 6
day s m t w r f s
first day(15 spaces) 1

my spacing isnt coming out right in the post, but you get the idea...


second day should only go up by 3 spaces, but it's going up by 15.. I see my mistake (position of the formula for calculating the first day) but I dont know how to re-arrange it so it will work.. I'm stumped.

again, with this type of problem, the longer I look at it and mess with it, the farther away the end result gets... it's pretty frustrating because I am pretty sure it is a simple solution.
  #3  
Old 31-Jan-2004, 15:08
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hey fehyd. Welcome to the forums.

First of all, I gave a calander example in this thread . It uses the tm structure and also uses more standard C syntax, but it may give you some help.

First of all, I have a question for you. Why is dow a double? I can not think of any reason that this should be a double, wouldn't an integer work just as well here? Your code for this is obviously working, but I wouldn't use a double unless I needed to.

Second, I am not 100% sure why your output is like it is. Have you tried something like:
CPP / C++ / C Code:
while(day<=31){
    cout << day;
    if(dow<6)
       cout<<"  ";
    else
         cout<<endl;
    dow++;
    day++;
}

It is several less lines this way and also, I don't know what the purpose of the spacing call is. When you write to the console, your text will go to where the last write left off, not from the first of the line.

One other thing that you will need to watch out for is double digits though. You could write another if statement to check for double digits and adjust your spacing accordingly.

Hope this helps.
  #4  
Old 31-Jan-2004, 15:44
fehyd fehyd is offline
New Member
 
Join Date: Jan 2004
Posts: 6
fehyd is on a distinguished road
I looked at the calander you had posted in that thread, and to be honest, it was way over my head. I'm still in the first week of my data structures class, and I left the intro to programming class a bit fuzzy on functions.

the spacing is to take into affect the 2 digit numbers. I am using a setwidth of 3 so that it will print out like this... 10_11_12 (read the "_" as a space)


basically, I'm still dealing with the very beginning stages of the language.
What I am desperately trying to figure out is how to use the loop structure to align the days of the month... I have a previous function within the program that sets a value for the dow (day of the week) which the first day of the month falls on... for instance, using january 2004 as an example.. my previous function asigns the value of "4" to the dow. so, when running the above function, I know that the first day of january falls under the 4th day (sunday being zero, saturday being 6)

now, acount for 3 spaces per day ... the first should fall under the 15th space under my formula
CPP / C++ / C Code:
cout<<setw((dow * 3) + 3)<<day
this comes out fine, and the first day is always in the right position. no problems there...

the headache starts when I want to place the second day of the month... I used the accumulator "day++" to advance the date to the second, no problems there...

now, to place the second day, I need it to fall under the 18th position (which would be friday, or dow = 5) I've already updated the dow by using the accumulator dow++, then I am trying to place the second day in the right spot using (dow*3)+3 or 5 * 3 + 3 = 18... setting the width for that number to 18. since I have not used an "endl" yet, that would be counting 18 spaces from the beginning of the line, right?

now, second part of my problem... once the dow = 6, I want to place the date under the 6th day (or 21st position) and endl it so that the next day falls back under the sunday (or 3rd spot). I know I have to reset the dow to zero for this to happen...

I am just haveing a bunch of syntax and order issues... I have tried a million combinations, but none of them are putting the days in the right spot...
  #5  
Old 31-Jan-2004, 15:52
fehyd fehyd is offline
New Member
 
Join Date: Jan 2004
Posts: 6
fehyd is on a distinguished road
my latest attempt at this code looks like this:

CPP / C++ / C Code:
void printJan(double dow)

{
	int day=1;
cout<<"    ************     "<<endl;
cout<<"    *  January *     "<<endl;
cout<<"    ************     "<<endl;
cout<<"  S  M  T  W  R  F  S"<<endl;
cout<<"*********************"<<endl;

while(day<=31)
{
	if(day==1)
	{
	cout<<setw((dow * 3) + 3)<<day;
	dow++;
	day++;
	}

	else
	{	
		if(dow<6)
		{
		cout<<setw(dow*3)<<day;
		dow++;
		day++;
		}
		else
		{
		cout<<setw(dow*3)<<day<<endl;
		day++;
		dow=0;
		}

	}
}
}


it is printing it out to look like this...

CPP / C++ / C Code:

    ************
    *  January *
    ************
  S  M  T  W  R  F  S
*********************
                   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
(the spacing isnt exact, but you can see the basic idea...)

so, I know I have the endl in the right place... all the numbers are correct and in the right order... it's just my spacing... I cant figure out if I am using the setw wrong, the accumulators too much, or just using the wrong syntax all together...
  #6  
Old 31-Jan-2004, 16:07
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Okay, now it is your turn to help me, since I am so terribly unfamiliar with the C++ I/O functions. Is setw a padding call? So if I call it with 3, I will get this output on all 3 of these lines:
Code:
__1 _10 100

If so, I think you are on the right track. Put a call in to check if it is the first day only and use your formula for this just like you have it. After that, you only need to call setw with a number of 3, because it is measured from your last output. Maybe this diagram will help:
Code:
_____1__2__3 _____6__9__12 +6 +3 +3
Does this make sense?

Also, you can use/study the code in this forum to help you with your programming class. The last thing that you should do is copy it. So I am glad that you are writing this yourself. However, there are some leap year issues that you may run in to as you get this done. You may still want to look at that other sample for things like this.
  #7  
Old 31-Jan-2004, 16:15
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Ahh, you are posting too fast for me to keep up! Anyway, you fixed your code somewhat like I mentioned before even seeing it. With a small change, I think this should get you somewhere:

CPP / C++ / C Code:
while(day<=31)
{
	if(day==1)
	   cout<<setw((dow * 3) + 3)<<day;
	else
		if(dow<6)
		   cout<<setw(3)<<day;
		else
                {
		   cout<<setw(3)<<day<<endl;
                   dow = 0;
                 }
	day++;
	dow++;
}

Do you understand, why I only need to call the day++ and dow++ in one location? Because no matter what the day and day of the week need to increase.

Anyway, I think you about have it.
  #8  
Old 31-Jan-2004, 16:58
fehyd fehyd is offline
New Member
 
Join Date: Jan 2004
Posts: 6
fehyd is on a distinguished road
ok, that makes total sense!! basically, I was adding way too much stuff, and making it more complicated than it needed to be... I was unclear if the setwidth (setw) started where the last entry left off, or back at the beginning of the line. Thank you very much for clearing that up.

and yes, you are correct.. in C++, the setw command sets it aligned right (by default) in the appointed number of spaces.


there is only one issue left so far...
CPP / C++ / C Code:
while(day<=31)
{
  if(day==1)
     cout<<setw((dow * 3) + 3)<<day;
  else
    if(dow<6)
       cout<<setw(3)<<day;
    else
                {
       cout<<setw(3)<<day<<endl;
                   dow = 0;
                 }
  day++;
  dow++;

}
}

ok, in the original form, it ended the first week on saturday (as it is supposed to) but every week after that ended on friday...
it looks like this...
CPP / C++ / C Code:
  s  m  t  w  r  f  s
            1  2  3  4
5  6  7  8  9 10
11 12 13 14 15 16
...etc.

I worked this a bit and changed it to read
CPP / C++ / C Code:
if(dow<=6)

now, it ends the first week in a position AFTER saturday, but every week after that ends on saturday as it is supposed to...
with the "=" it looks like this:
CPP / C++ / C Code:
   s  m  t  w  r  f  s
               1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21
...etc.
(these are a rough estimate, I cant get them to line up exactly in my post)

this makes no logical sense to me... why would the first week differ from the following weeks? I would think that if the week shorted out or was longer in the first line, it should repeat it in the following lines....

now, for the rest of the calender, I will need to carry over the value of "dow" so that the next month will begin on the correct day.. I am pretty sure I have that down (passing by reference) I also have the leap year already configured, so no problems there...

I'm not worried about copying or anything... If I have a problem with any of this stuff, this is exactly the way I will approach it. I will kill myself for hours and hours writing and fidling with the code, until finally, as an alternative for mass murder or computers thrown through windows, I'll ask for help.

most of the time, and the source of most of my frustration, my problem will also be like this one... I've just written way too much code and burried myself with it. I really do appreciate your help on this one. If there is any better way to present this type of problem, please let me know. Hey, the easier I can get my point acros, the easier it will be for someone to help me!

thanks again... any ideas about this last glitch?

joshua
  #9  
Old 31-Jan-2004, 17:46
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Josh.

I've got a way to fix your glitch. Don't take bad advice from me

The code sets dow=0 at the first of each week. Then we increment it, so that it equals 1. My advice is to keep your first compare statement and just set this value to -1.

CPP / C++ / C Code:
while(day<=31)
{
  if(day==1)
     cout<<setw((dow * 3) + 3)<<day;
  else
    if(dow<6)
       cout<<setw(3)<<day;
    else
                {
       cout<<setw(3)<<day<<endl;
                   dow = -1;
                 }
  day++;
  dow++;

}
}

Sorry about that.

Anyway, I think your use of this forum is perfect. It is a learning tool and a place where people can learn from each other. People that post there homework assignments to be completed aren't going to get much help from me, but people like you that have worked hard on the code and post it with questions will always get my full attention.

Hopefully, you keep using this forum and start looking to see if you can help other people as well. Like me with c++ syntax
  #10  
Old 31-Jan-2004, 18:28
fehyd fehyd is offline
New Member
 
Join Date: Jan 2004
Posts: 6
fehyd is on a distinguished road
that works perfectly! thanks a million. funny thing is... I actually understand WHY it works! lol

really man, thank you. As I get better and more comfortable with this language (my first with programming) I'll be sure to help out anyone in any way I can.
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
some Great site for programming issues priyanka Web Design Forum 8 19-Dec-2005 08:55
[vbulletin 3] Usergroup Promotion Issues JdS Web Design Forum 1 23-Jan-2004 15:47
Downloadable document compatibilty issues? rhino1616 Web Design Forum 3 07-May-2003 03:05
[Linux] Mozilla Issues JdS Computer Software Forum - Linux 3 01-May-2003 08:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.