7 Replies - 2701 Views - Last Post: 05 May 2010 - 12:41 PM Rate Topic: -----

#1 Topaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-May 10

new to C++ array problem lab due 5/9/2010

Posted 05 May 2010 - 09:50 AM

I am new to C++ and am doing an assignment due 5/9. Struggling with array concept. The due is display calendar for month/days, allow user to select a date for purpose of entering/editing a journal entry, allow the user to search through journal for a given string. Objectives are use a vector to allow for scaleable data, use at least one pointer to access data.

Am at wits end,following code so far but am not sure how to fix it for the arrays & what to do next? Have some of vector information written but left out of this post since it compiles, need to fix rest of code for arrays. Reslly not getting how arrays work here, help? explain what to do to fix?

#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include "CinReader.h"
using namespace std;
CinReader reader;

struct entry
{
int date;
string dayOfWeek;
string text;
string timestamp;
};

//functions for use by program
int calcDaysSoFar(int year, int month);
void printCalendar(int month, int year, int firstDay);
void repeatChar(char ch, int numChar);
void printTop(int innerWidth);
void drawDivide(int innerWidth);
void printBottom(int innerWidth);

//function allowing your program to start, prompts user for input and
//outputs information to user, uses structs, calls functions, cleans up arrays when done
int main()
{
int year, month;
int days;

cout << " Please enter the number of the month and year to access the calendar\n" << endl;
cout << " Please leave a space in between the month and year when you enter them: \n";
cin >> month >> year;

do
{
days = calcDaysSoFar(year, month);
int firstDayOfWeek = days % 7;

printCalendar(month, year, firstDayOfWeek);
month++;
}
while(month < 13);

system("pause");

return 0;
}

//declares the array for the names of a month for a year
void monthName(string monthNameArray, int sizeOfMonthArray = 12)
{
cout << "Enter the month name" << monthName << "." "\n";

string monthName[12] = {"January","February","March","April","May","June",
"July","August","September","October","November","December"};
for(int i = 0; i< sizeOfMonthArray; i++)
{
cout << "Please enter the name of the month " << monthName << " " << i+1 << "\n";
monthNameArray[i] = reader.readInt();
}
}

//declares the array for the # days per month per year
void daysPast(int daysPastArray[],int sizeOfDaysPastArray)
{
int daysPast[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
for(int i = 0; i < sizeOfDaysPastArray; i++)
{
cout << "Please enter the number of days in this " << daysPast << " month" << i+1 << ": \n";
daysPastArray[i] = reader.readInt();
}
}

//declares the array for the # days in a month for a year
void daysInMonth(int monthArray[], int sizeOfMonthArray = 12)
{
int monthArray[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(int i = 0; i < sizeOfMonthArray; i++)
{
cout << "Please enter the number of days in this " << daysInMonth << " month " << i+1 << ": \n";
month[i] = reader.readInt();
}
}

//function calculates the number of days passed from the start of May 2010
int calcDaysSoFar(int daysPassed[12] = ( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
,int year, int month)
{

const int firstYear = 2010;
int dayOffset = 3;
int days;


days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month-1];

return days;
}

//Prints Month & Year followed by the day headings followed
//by the numbers
void printCalendar(int month, int year, int firstDay)
{
string dayHeader = string("Sun") + char(186) + "Mon" + char(186) + "Tues" +
char(186) + "Wed" + char(186) + "Thu" + char(186) + "Fri" + char(186) + "Sat";

int innerWidth = dayHeader.length();

//converts year to a string
stringstream sstream;
string tempStr;

cout << '\n';
printTop(innerWidth);

//print the month and year line
sstream << year;
tempStr = string(" ") + monthName[month-1] + string(" ") + sstream.str();
cout << char(186);
cout << tempStr;
repeatChar(' ', innerWidth - tempStr.length());
cout << char(186);

// draws a dividing line
cout << '\n';
cout << char(204);
for (int count = 0; count < 7; count ++)
{
repeatChar(char(205),4);
if(count != 6)
{
cout << char(203);
}
}
cout << char(185);
cout << '\n';

//print the day header line
cout << char(186);
cout << dayHeader;
cout << char(186) << '\n';

//draws a horizontal line
drawDivide(innerWidth);
int count.offset;
offset = 1 - firstDay;

//get the number of days in this month
count = daysInMonth[month-1]

//loop to fill in rows
int dayNum = 0;
for (int x = offset; x <= count; x +=7)
{
cout << char(186);
for dayNum = x; (dayNum < x+7); dayNum++)
{
if (dayNum <= 0 || dayNum > count)
{
cout << setw(4) << left << setprecision(3) << " " << char(186);;
}
else
{
cout << setw(4) << left << setprecision(3) << setfill(' ') << dayNum << char(186);
}
}
cout << '\n';
if(dayNum <= count)
{
//draws a divide line after each row except last
drawDivide(innerWidth);
}
}
printBottom(innerWidth);
}

//function to set up top of calendar grid
void printTop(int innerWidth)
{
cout << char (201);
repeatChar((char)205, innerWidth);
cout << char (187);
cout << '\n';

}

//repeats a char a number of times
void repeatChar(char ch, int numChar)
{
string aString (numChar, ch);
cout << aString;
}

void drawDivide(int innerWidth)
{
cout << char(204);
for (int count = 0; count < 7; count ++)
{
repeatChar(char(205), 4);
if( count !=6)
{
cout << char(206);
}
}
cout << char(185);
cout << '\n';
}

//draws bottom of grid
void printBottom(int innerWidth)
{
cout << char(200);
for (int count = 0; count <7; count ++)
{
repeatChar(char(205), 4);
if( count !=6)
{
cout << char(202);
}
}
cout << char(188);
cout << '\n';
}

This post has been edited by JackOfAllTrades: 05 May 2010 - 10:25 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: new to C++ array problem lab due 5/9/2010

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: new to C++ array problem lab due 5/9/2010

Posted 05 May 2010 - 09:53 AM

You already asked for help.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: new to C++ array problem lab due 5/9/2010

Posted 05 May 2010 - 10:28 AM

You keep asking for a fix, but you haven't told us what NEEDS to be fixed.

And please look into indentation. What did you use to edit this monstrosity?
Was This Post Helpful? 1
  • +
  • -

#4 Guest_Topaze*


Reputation:

Re: new to C++ array problem lab due 5/9/2010

Posted 05 May 2010 - 12:04 PM

I am getting coding errors when I compile and don't get what I'm doing wrong with the arrays, this has been a real struggle for me to get. This is a calendar that will later allow journal entries to be made, the journal entry code is commented out to concentrate on fixing the other errors.

using codeblocks on a windows vista machine for C++ problem
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include "CinReader.h"

using namespace std;

CinReader reader;

struct entry
{
    int date;
    string dayOfWeek;
    string text;
    string timestamp;
};

//functions for use by program
void greeting();
void clear();
int calcDaysSoFar(int year, int month);
void printCalendar(int month, int year, int firstDay);
void repeatChar(char ch, int numChar);
void printTop(int innerWidth);
void drawDivide(int innerWidth);
void printBottom(int innerWidth);
//void initializeEntry (entry& e);
//void save (entry e);
//void retrieve (entry& e);



//function allowing your program to start, prompts user for input and
//outputs information to user, uses structs, calls functions, cleans up arrays when done
int main()
{


    int year, month;
    int days;

    cout << " Please enter the number of the month and year to access the calendar\n" << endl;
    cout << " Please leave a space in between the month and year when you enter them:  \n";
    cin >> month >> year;

    do
    {
        days = calcDaysSoFar(year, month);
        int firstDayOfWeek = days % 7;

        printCalendar(month, year, firstDayOfWeek);
        month++;
    }
    while(month < 13);

    system("pause");

    return 0;
}


    /*// e1 is an instance of entry
    entry e1;
    initializeEntry(e1);
    save(e1);
    retrieve(e1);

    cout << "Date: " << e1.date << endl;
    cout << "Day of week: " << e1.dayOfWeek << endl;
    cout << "Entry: " << e1.text << endl;
    cout << "Time: " << e1.timestamp << endl;

    return 0;
}*/

//this is a function that sets up the output screen
void clear()
{
    for(int i = 0; i < 26; i++)
    {
        cout << endl;
    }
}

//function greeting the user
void greeting ()
{
    cout << "You will be able to enter appts or to Do List entries. \n";

    cout << "Press enter to continue\n";
    reader.readString();
}


//declares the array for the names of a month for a year
void monthName(string monthNameArray, int sizeOfMonthArray = 12)
{
    cout << "Enter the month name" << monthName << "." "\n";

    string monthName[12] = {"January","February","March","April","May","June",
                "July","August","September","October","November","December"};
    for(int i = 0; i< sizeOfMonthArray; i++)
    {
        cout << "Please enter the name of the month " << monthName << " " << i+1 << "\n";
        monthNameArray[i] = reader.readInt();
    }
}

//declares the array for the # days per month per year
void daysPast(int daysPastArray[],int sizeOfDaysPastArray)
{
int daysPast[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
    for(int i = 0; i < sizeOfDaysPastArray; i++)
    {
        cout << "Please enter the number of days in this " << daysPast << " month" << i+1 << ": \n";
        daysPastArray[i] = reader.readInt();
    }
}

//declares the array for the # days in a month for a year
void daysInMonth(int monthArray[], int sizeOfMonthArray = 12)
{
    int monthArray[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    for(int i = 0; i < sizeOfMonthArray; i++)
    {
        cout << "Please enter the number of days in this " << daysInMonth << " month " << i+1 << ": \n";
        month[i] = reader.readInt();
    }
}

//function calculates the number of days passed from the start of May 2010
int calcDaysSoFar(int daysPassed[12] = ( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
                ,int year, int month)
{

    const int firstYear = 2010;
    int dayOffset = 3;
    int days;


    days = (year - firstYear) * 365;
    days += dayOffset;
    days += daysPassed[month-1];

    return days;
}

//Prints Month & Year followed by the day headings followed
//by the numbers
void printCalendar(int month, int year, int firstDay)
{
    string dayHeader = string("Sun") + char(186) + "Mon" + char(186) + "Tues" +
    char(186) + "Wed" + char(186) + "Thu" + char(186) + "Fri" + char(186) + "Sat";

    int innerWidth = dayHeader.length();

    //converts year to a string
    stringstream sstream;
    string tempStr;

    cout << '\n';
    printTop(innerWidth);

    //print the month and year line
    sstream << year;
    tempStr = string(" ") + monthName[month-1] + string("     ") + sstream.str();
    cout << char(186);
    cout << tempStr;
    repeatChar(' ', innerWidth - tempStr.length());
    cout << char(186);

    // draws a dividing line
    cout << '\n';
    cout << char(204);
    for (int count = 0; count < 7; count ++)
    {
        repeatChar(char(205),4);
        if(count != 6)
        {
            cout << char(203);
        }
    }
    cout << char(185);
    cout <<  '\n';

    //print the day header line
    cout << char(186);
    cout << dayHeader;
    cout << char(186) << '\n';

    //draws a horizontal line
    drawDivide(innerWidth);
    int count.offset;
    offset = 1 - firstDay;

    //get the number of days in this month
    count = daysInMonth[month-1]

    //loop to fill in rows
    int dayNum = 0;
    for (int x = offset; x <= count; x +=7)
    {
        cout << char(186);
        for dayNum = x; (dayNum < x+7); dayNum++)
        {
            if (dayNum <= 0 || dayNum > count)
            {
                cout << setw(4) << left << setprecision(3) << "   " << char(186);;
            }
            else
            {
                cout << setw(4) << left << setprecision(3) << setfill(' ') << dayNum << char(186);
            }
        }
        cout << '\n';
        if(dayNum <= count)
        {
            //draws a divide line after each row except last
            drawDivide(innerWidth);
        }
    }
    printBottom(innerWidth);
}



//function to set up top of calendar grid
void printTop(int innerWidth)
{
    cout <<  char (201);
    repeatChar((char)205, innerWidth);
    cout << char (187);
    cout << '\n';

}

//repeats a char a number of times
void repeatChar(char ch, int numChar)
{
    string aString (numChar, ch);
    cout << aString;
}

void drawDivide(int innerWidth)
{
    cout << char(204);
    for (int count = 0; count < 7; count ++)
    {
        repeatChar(char(205), 4);
        if( count !=6)
        {
            cout << char(206);
        }
    }
    cout << char(185);
    cout << '\n';
}

//draws bottom of grid
void printBottom(int innerWidth)
{
    cout << char(200);
    for (int count = 0; count <7; count ++)
    {
        repeatChar(char(205), 4);
        if( count !=6)
        {
            cout << char(202);
        }
    }
    cout << char(188);
    cout << '\n';
}

/*
//function for journal entry
void initializeEntry (entry& e)
{
    cout << "Please enter the date of the entry (1-31): ";
    e.date = reader.readInt(1, 31);

    string daysOfWeek[] = {"Monday","Tuesday","Wednesday",
                           "Thursday","Friday","Saturday",
                           "Sunday"};
    for (int i=0; i<7; i++)
    {
        cout << "[" << (i+1) << "] " << daysOfWeek[i] << "\n";
    }
    cout << "select day of week from the menu & enter the corresponding number: ";
    e.dayOfWeek = daysOfWeek[reader.readInt(1,7)-1];

    cout << "Type your appt or to do List entry:\n";
    e.text = reader.readString();

    cout << "Enter the time (e.g.,1:45pm,2:01am ): ";
    e.timestamp = reader.readString();
}


//function to save data to a text file
void save (entry e)
{
    ofstream fout("journal.txt");
    if (!fout.fail())
    {
        fout << e.date << endl;
        fout << e.dayOfWeek << endl;
        fout << e.text << endl;
        fout << e.timestamp << endl;

        fout.close();
    }
}

//funtion to retrive data from a text file
void retrieve(entry& e)
{
    ifstream fin("journal.txt");
    if (!fin.fail())
    {
        string dateString;
        getline(fin, dateString);
        e.date = atoi(dateString.c_str());

        getline(fin, e.dayOfWeek);
        getline(fin, e.text);
        getline(fin, e.timestamp);

        fin.close();
    }
}
*/

This post has been edited by JackOfAllTrades: 05 May 2010 - 12:27 PM
Reason for edit:: Added code tags.

Was This Post Helpful? 0

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: new to C++ array problem lab due 5/9/2010

Posted 05 May 2010 - 12:17 PM

And, what are the errors? Please, post them. Also,use code tags when posting code.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: new to C++ array problem lab due 5/9/2010

Posted 05 May 2010 - 12:28 PM

:code:
Was This Post Helpful? 1
  • +
  • -

#7 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Re: new to C++ array problem lab due 5/9/2010

Posted 05 May 2010 - 12:29 PM

http://www.dreaminco...lab-due-592010/

I knew this code looked familiar.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: new to C++ array problem lab due 5/9/2010

Posted 05 May 2010 - 12:41 PM

Merged topics. Please do not create duplicate topics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1