Suggestions

This is a discussion on Suggestions within the C++ Programming forums, part of the General Programming Boards category; I am producing a program that a user can input a date and the program will output the day it ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    12

    Question Suggestions

    I am producing a program that a user can input a date and the program will output the day it is for that date. I am getting several errors message and I can not figure out where they are in the code.

    #include <iostream>

    using namespace std;


    int get_date(int, int, int); // function declaration
    int calc_year(int the_year);

    int main()
    {

    int m, day, year, our_day; //month, day and year

    cout<< "\nEnter any date as three sets of numbers.\n";
    cout<< "(month, day, year). Example: 7 29 1971\n";
    cout<<"Entering invalid dates will exit the program.\n";
    cout<<"Enter date: ";
    cin>>m>>day>>year; //input date


    while((m != 13)&(day !=32)&(year != 0000)) //Invalid dates exit the loop
    {
    our_day = get_date(m, day, year);
    //Passes the three values to get_date function


    //Turns a numberical month into a named month when printed

    switch(m)
    {
    case 1:
    cout <<"January "<<end1;
    break;
    case 2:
    cout <<"February "<<end1;
    break;
    case 3:
    cout <<"March "<<end1;
    break;
    case 4:
    cout <<"April "<<end1;
    break;
    case 5:
    cout <<"May "<<end1;
    break;
    case 6:
    cout <<"June "<<end1;
    break;
    case 7:
    cout <<"July "<<end1;
    break;
    case 8:
    cout <<"August "<<end1;
    break;
    case 9:
    cout <<"September "<<end1;
    break;
    case 10:
    cout <<"October "<<end1;
    break;
    case 11:
    cout <<"November "<<end1;
    break;
    case 12:
    cout <<"December "<<end1;
    break;
    }


    cout<<" "<<end1;
    case 1:
    cout <<"Tuesday\n"<<end1;
    break;
    case 2:
    cout <<"Wednesday\n"<<end1;
    break;
    case 3:
    cout <<"Thursday\n"<<end1;
    break;
    case 4:
    cout <<"Friday\n"<<end1;
    break;
    case 5:
    cout <<"Saturday\n"<<end1;
    break;
    case 6:
    cout <<"Sunday\n"<<end1;
    break;
    }

    cout<<"\n\nEnter date: ";
    cin>>m>>day>>year;

    }



    }

    get_date(m, day, year); //function call
    //get_date function
    int get_date(int the_m, int the_day, int the_year)
    {

    //values for months of the year
    int jan = 0, febr = 31, mar = 59, apr = 90,
    may = 120, june = 151, july = 181, aug = 212,
    sept = 243, octo = 273, nov = 304, dec = 334;

    int the_month, the_date; //Variables used in calculations

    if(3 > the_m)
    {
    if(2 == the_m)
    {
    the_date = febr + the_day;
    }
    else{
    if(1 == the_m)
    {
    the_date = jan + the_day;
    }
    }
    }
    else
    {

    switch(the_m)
    {
    case 3: the_month = mar; break;
    case 4: the_month = apr; break;
    case 5: the_month = may; break;
    case 6: the_month = june; break;
    case 7: the_month = july; break;
    case 8: the_month = aug; break;
    case 9: the_month = sept; break;
    case 10: the_month = octo; break;
    case 11: the_month = nov; break;
    case 12: the_month = dec; break;
    }
    if((0 == the_year%4) &! (0 == the_year%100))
    {
    the_date = the_day + (the_month + 1);
    }
    else
    {
    if(0 == the_year%400)
    {
    the_date = the_day + (the_month + 1);
    }
    else
    {
    the_date = the_day + the_month;
    }
    }
    }
    int first_day;//declares int

    first_day = calc_year(the_year); //passing the_year to the new function, and assigning the return value to first_day

    int this_day; //declaration for calculation.

    this_day = the_date + first_day; //add the return value(count_day) to the_date and assign the sum to this_day.

    //While () loop counts backwards by decrementing until it reaches the day

    while(this_day >= 7)
    {
    this_day = this_day - 7;
    }

    this_day = this_day - 1; //Make the day before Monday (0 - 1) be Sunday
    if(this_day == -1)
    {
    this_day = 6;
    }

    return this_day;

    return 0;
    }


    //calc_year function

    int calc_year(int the_year)

    {

    int count_day = 0; //start on Monday

    //1900 started on a Monday, so don't enter the loop

    for(int i = 1901; i <= the_year; i++)
    {
    count_day++; //day of the week.
    if(count_day >= 7)
    {
    count_day = count_day - 7;
    }

    //reset at the end of each week

    if((0 == ((i-1)%4)) &! (0 == ((i-1)%100)))
    {
    count_day = count_day + 1; //adjust for leapyear
    }
    else
    {
    if(0 == ((i-1)%400))
    {
    count_day = count_day + 1;
    }
    }



    }

    if(count_day == 7) //Make the day after Sunday (6 + 1) become Monday
    {
    count_day = 0;

    }

    return count_day;
    }

    errors are:

    line 36 end1 undeclared identifier
    line 75 illegal case
    line 78 illegal case
    line 81 illegal case
    line 84 illegal case
    line 87 illegal case
    line 90 illegal case
    line 98 main function should return a value
    line 102 3 syntax error's missing ";" before }

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    First off, use Code Tags.
    Second, end1 should be endl. Third, you're missing a switch statement before all of those cases.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Definitely needs code tags. Also put some easy-to-spot comments on the relevant lines.

    The problem with main probably has to do with a path of execution which does not return a value or a misplaced brace, or something of that nature, but it is hard to tell right now.

    Not sure about the missing semi-colons right off.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation HINTS

    I am getting several errors message and I can not figure out where they are in the code.
    Quoting myself form a few days ago.....
    Don't try to write the whole program and compile. It's very hard to debug when you have multiple errors.

    Experienced programmers test-compile and test-run often during program "development". (It IS good practice to plan-out your program completely using a flow chart or psudo code.)
    Most programmers will test-compile (and test-run if possible) every function they write before moving-on to the next function. Of course, if it's a complex function they will test before the function is complete.

    Beginners should compile as soon as there is enough to compile... I mean you have to have "complete" functions with brackets and the return value if not void, etc. The function does not have to do anything for you to test-compile. Then, you should re-compile every line or two.

    You should also test each function as soon as there is enough code to do something. Sometimes it's helpful to add debug cout statements to let you know what's going on (i.e. cout << " Now in MyFunction()".)

    It takes some practice to learn how to sequence your programming so that you can test-comipile and test-run, but programming is a lot more fun if you only have to work on one or two errors at a time!

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    12
    Thanks for the advice. I have fixed my problems and it now compiles but the program does not run. I had some of my if statements backwards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free Book Suggestions!
    By valaris in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-08-2008, 10:25 AM
  2. Hi, Suggestions on my program please..
    By Siggy in forum C++ Programming
    Replies: 44
    Last Post: 11-23-2004, 09:55 PM
  3. Math Book Suggestions
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-09-2003, 10:43 AM
  4. Need Suggestions
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2003, 05:46 AM
  5. Learning C for OSX, tutorial suggestions please
    By Nexum in forum C Programming
    Replies: 2
    Last Post: 02-17-2003, 03:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21