Adblock breaks this site

help me find my problem

Discussion in 'Programming General' started by rogue poser, Oct 9, 2007.

  1. rogue poser

    rogue poser Member

    Joined:
    Jul 10, 2005
    Posts:
    51
    Referrals:
    0
    Sythe Gold:
    0
    help me find my problem

    hey guys i have a problem, i wrote this code for my C++ class and i cant seem to find why im always outputting the same command at the end of the program every time, you will know when u paste this into your compiler

    --------------------------------------------------------------------------#include <iostream>
    #include <windows.h>

    using namespace std;

    int main()
    {
    SetConsoleTitle("James Russick's Project");
    int month,day,year,leftover,daysleft,back,classesleft,daysleft1;
    const int oct=31;
    const int nov=30;
    const int dec=11;

    back = 1;

    do
    {
    cout << "What is the current month? \n";
    cout << "october = 10, november=11 , december= 12 \n";
    cin >> month;
    if (month == 10 || month == 11 || month == 12)
    {
    back = 2;
    do
    {
    cout << "\n" "What is the current day? \n";
    cin >> day;
    if (day >= 1 && day<=31)
    {
    back=3;
    do
    {
    cout << " \n" "What is the current year? \n";
    cout << "ex: 2007\n";
    cin >> year;
    if (year == 2007)
    {
    back=4;
    cout << " \n" "Okay so the date is " << month << " " << day << " " << year << "?\n";

    classesleft = 1;
    do
    {
    //OCTOBER
    if (month == 10) // lines 44-58 calculate days and classes left and output them
    {
    leftover = oct - day;
    daysleft = leftover + nov + dec;
    cout << "\n " "There are " << daysleft << " days left until the end of the semester \n";
    do
    {
    daysleft = daysleft - 7;
    classesleft = classesleft + 1;
    }
    while (daysleft >= 7);


    cout << "And there are " << classesleft << " class sessions left";

    }

    //NOVEMBER
    if (month == 11) // lines 62-75 calculate days and classes left and output them
    {
    leftover = nov - day;
    daysleft = leftover + dec;
    cout << "\n " "There are " << daysleft << " days left until the end of the semester \n";
    do
    {
    daysleft = daysleft - 7;
    classesleft = classesleft + 1;
    }
    while (daysleft >= 7);

    cout << "And there are " << classesleft << " class sessions left";

    }

    //DECEMBER
    if (month == 12) // lines 78-92 calculate days and classes left and output them
    {
    leftover = dec - day;
    daysleft = leftover ;
    cout << "\n " "There are " << daysleft << " days left until the end of the semester \n";
    do
    {
    daysleft = daysleft - 7;
    classesleft = classesleft + 1;
    }
    while (daysleft >= 7);


    cout << "And there are " << classesleft << " class sessions left";
    back=5;



    }
    { break; }
    if (back == 5)
    system("pause");
    return 0;
    }
    while (back=4);

    }
    else
    {

    cout << " \n" "Sorry, the correct answer is 2007 \n";
    cout << " \n";

    }

    }
    while (back = 3);

    }
    else
    {

    cout<< " \n" "Invalid Day, the day must fall between 1 and 31! \n ";
    cout<< " \n";

    }

    }
    while (back = 2);

    }
    else
    {

    cout << " \n" "Invalid month!!!!!! \n please re-enter the month in the correct format! \n";
    cout << " \n";
    // back = 1;
    }

    }
    while (back = 1);
    system("pause");
    return 0;

    }
    -------------------------------------------------------------------------
     
  2. The End

    The End Forum Addict

    Joined:
    Dec 10, 2005
    Posts:
    397
    Referrals:
    0
    Sythe Gold:
    0
    help me find my problem

    Well your code is a bit bloated, which is fine for now, I redid the entire program but I intentionally left out all error handling and IO error handling, you can use this code, but try to incorporate error handling with it, good luck :D

    Code:
    #include <iostream>
    using namespace std;
    #include <windows.h>
    int main(){
        
        SetConsoleTitle("James Russick's Project");
        char month [15];
        int day,daysleft;
        const int dec = 31;
        const int nov = 30;
        const int oct = 31;
        
        cout << "Enter the month" << endl;
        cout << "Your options are: November, December, and October." << endl;
        cout << "Month: ";
        cin >> month;
        if (month == "October" || "october"){
                  cout << "You have selected " << month << endl;
                  cout << "Enter the numerical day: ";
                  cin >> day;
                  daysleft = oct - day;
                  cout << "There are " << daysleft << " days left in " << month << endl;;
                  system("pause");
                  return 0;
                  }
                      if (month == "December" || "december"){
                  cout << "You have selected " << month << endl;
                  cout << "Enter the numerical day: ";
                  cin >> day;
                  daysleft = dec - day;
                  cout << "There are " << daysleft << " days left in " << month << endl;;
                  system("pause");
                  return 0;
                  }
                      if (month == "November" || "november"){
                  cout << "You have selected " << month << endl;
                  cout << "Enter the numerical day: ";
                  cin >> day;
                  daysleft = nov - day;
                  cout << "There are " << daysleft << " days left in " << month << endl;;
                  system("pause");
                  return 0;
                  }
                  }
    
    For future note you can use char (nameofchar) [(charsize)] for characters there and you can see that the if statements compared the users input to the string to determine the next set of events.
     
  3. rogue poser

    rogue poser Member

    Joined:
    Jul 10, 2005
    Posts:
    51
    Referrals:
    0
    Sythe Gold:
    0
    help me find my problem

    thankyou sir!!! i really appreciate that.... i know its a bit long but i wasnt really sure of another way to handle the possible errors that people inputted, so i set everything in a do while loop and made a variable so that it would continue the loop while the wrong input was in..... i dunno... thanks tho i really appreciate it
     
  4. rogue poser

    rogue poser Member

    Joined:
    Jul 10, 2005
    Posts:
    51
    Referrals:
    0
    Sythe Gold:
    0
    help me find my problem

    if anyone cares, heres the finished product....

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int monthnum = 0;int leftover = 0;int daysleft = 0;int day,classesleft;
    char month[8];   
    
    char tmonth(char month [8])
    {
        if ((!(strcmpi(month,"january")) || (!(strcmpi(month,"January")))))   //couldn't find a better way to do this
               monthnum = 1;                                                 // i couldnt figure out how to use
        if ((!(strcmpi(month,"february")) || (!(strcmpi(month,"February"))))) // characters in a case statement
               monthnum = 2;
        if ((!(strcmpi(month,"march")) || (!(strcmpi(month,"March")))))       // so this checks if the input is a month
               monthnum = 3;                                                 // and if it is then it will assign it a 
        if ((!(strcmpi(month,"april")) || (!(strcmpi(month,"April")))))       // value and continue
               monthnum = 4;
        if ((!(strcmpi(month,"may")) || (!(strcmpi(month,"May"))))) 
               monthnum = 5;
        if ((!(strcmpi(month,"june")) || (!(strcmpi(month,"June")))))
               monthnum = 6;
        if ((!(strcmpi(month,"july")) || (!(strcmpi(month,"July"))))) 
               monthnum = 7;
        if ((!(strcmpi(month,"august")) || (!(strcmpi(month,"August"))))) 
               monthnum = 8;
        if ((!(strcmpi(month,"november")) || (!(strcmpi(month,"November")))))
               monthnum = 9;
        if ((!(strcmpi(month,"october")) || (!(strcmpi(month,"October"))))) 
               monthnum = 10;
        if ((!(strcmpi(month,"november")) || (!(strcmpi(month,"November"))))) 
               monthnum = 11;
        if ((!(strcmpi(month,"december")) || (!(strcmpi(month,"December")))))
               monthnum = 12;
    	return(monthnum);
    }  
    
    void calculate()
    {
         switch (monthnum)
         {
                case 1:                     
                     daysleft = 365 - day ; // calculates all the days left in the month
                     break;
                case 2:
                     daysleft = 334 - day ; // see above statement           
                     break;
                case 3:
                     daysleft = 306 - day ;                 
                     break;
                case 4:
                     daysleft = 275 - day ;                 
                     break;
                case 5:
                     daysleft = 245 - day  ;                 
                     break;
                case 6:
                     daysleft = 214 - day ;                 
                    break;
                case 7:
                     daysleft = 184 - day ;               
                     break;
                case 8:
                     daysleft = 153 - day ;                 
                     break;
                case 9:
                     daysleft = 122 - day ;                 
                     break;
                case 10:
                     daysleft = 92 - day ;               
                     break;
                case 11:
                     daysleft = 61 - day ;                
                     break;
                case 12:
                     daysleft = 31 - day ;                 
                     break;
         }
        daysleft = daysleft - 20;            // since the class ends on dec 11 there are 20 less days
        cout << "\n " "There are " << daysleft << " days left until the end of the semester \n"; //outputs the days left
                                    do
                                    {
                                    daysleft = daysleft - 7;  // takes daysleft and subtracts 7 until its less than 7
                                    classesleft = classesleft + 1;  //"                                             "
                                    }
                                     while (daysleft >= 7);                      
                                 cout << "And there are " << classesleft << " class sessions left \n"; // outputs value
                                 cout << "-------------------------------------------------------------- \n";   
    }
    
    int main()
    {
        int back;                 // declare the variables needed
        int classesleft=0;
                      SetConsoleTitle("James Russick's Project"); // change console title
      do
      {
        cout << "What is the current month? \n";         // get current month from user
        cout << "please write out the entire month \n";  // format of the month
        cin >>  month;                                   //recieve the month from the user
        cout << " \n";                                   //space to make the output easier to read
        tmonth(month);                                   //use the function above
            if (monthnum >=1 && monthnum <=12)
          {
           cout << "what is the current day? \n";           //prompts the user for the current day
           cout << "please choose a day between 1 and 31\n";
           cin >> day;                                      //recieve the day from the user
           cout << " \n";                                   // space for easier reading
              if (day >=1 && day <=31)                      //allows you to continue if you have the correct day in here
               {
                   cout << "so todays date is " << month << " " << day << " 2007"; //out is the date
                   back = 2;
                        while(back = 2)
                             {  
                                calculate();                 
                                system ("pause");
    							//ExitWindows(1,1);
                                return 0;   
                               }
               }
              else
               cout << "---incorrect day plese start over\n";  //failsafe
           }
           else
           cout << "---incorrect month, please start over\n"; //failsafe
      }
      while(back=1);
    }
    
    better coding strategy than b4..... lesss effun lines
     
< My second ever VERY useful program [its a source!] | Authentication help !? >


 
 
Adblock breaks this site