| |

VerySource

 Forgot password?
 Register
Search
Author: iskiller

C ++ experts help to see a problem, anxious ~~~

[Copy link]

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-7-12 18:15:01
| Show all posts
Don't put so much detail in the main function.
Looking at the code starts with the main function, first of all gives a general overview.
Divide the code into several functions, and then call them from main to comment each function. Then it will feel a lot more refreshing.
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-7-15 14:45:02
| Show all posts
A shorter part, without changing your algorithm.

#include <iostream>
#include <iomanip>

using namespace std;

void date();
void OutputDays();
void printDays(int maxrow, int maxcol, int daysOfMonth);

int main () {

    int optionM;
    bool done = false;
    optionM = 0;

    while (!done) {
        //clrscr();//Clear the screen
        cout<<"\n\nWelcome to programming";
        cout<<"\n\nThis program is print out the calendar";
        cout<<"\n\nPlease select the month";
        cout<<"\n 1. Janruary";
        cout<<"\n 2. February";
        cout<<"\n 3. March";
        cout<<"\n 4. April";
        cout<<"\n 5. May";
        cout<<"\n 6. June";
        cout<<"\n 7. July";
        cout<<"\n 8. August";
        cout<<"\n 9. September";
        cout<<"\n 10. October";
        cout<<"\n 11. November";
        cout<<"\n 12. December";
        cout<<"\n\nPlease select one month: ";
        cin>>optionM;

       if (optionM == 1 || optionM ==3 || optionM == 5 || optionM == 7 || optionM == 8 || optionM ==10 || optionM == 12)
       {
            printDays (6,6,31);done = true;
        }
        else if (optionM == 4 || optionM == 6 || optionM == 9 || optionM ==11)
        {
            printDays (6,6,30); done = true;
        }
        else if (optionM == 2) {
            printDays (6,6,28); done = true;
        }
        else
        {
            cout<<"\n\nError!!!! Please try again";
        }
    }

    system("pause");
    return 0;
}

void date() {
    //clrscr();
     cout <<setw(6) << "Sun";
    cout <<setw(6) << "Mon";
    cout <<setw(6) << "Tue";
    cout <<setw(6) << "Wed";
    cout <<setw(6) << "Thu";
    cout <<setw(6) << "Fri";
    cout <<setw(6) << "Sat";
}

void OutputDays()
{
    cout<<"\n\nThis month have 30 days";
    cout<<"\n\n 1. Sunday";
    cout<<"\n\n 2. Monday";
    cout<<"\n\n 3. Tuesday";
    cout<<"\n\n 4. Wednesday";
    cout<<"\n\n 5. Thursday";
    cout<<"\n\n 6. Friday";
    cout<<"\n\n 7. Saturday";
    cout<<"\n\nPlease select one day: ";
}

void printDays(int maxrow, int maxcol, int daysOfMonth)
{
    int num = 1;
    bool done = false;
    while (!done) {
        OutputDays();
        int optionD = 0;
        cin>>optionD;
        date();
        cout<<"\n";
        if (optionD> 0&&optionD <= 7){
            for (int row = 1; row <= maxrow&&num <= daysOfMonth; row++)
            {
                for(int col = 0; col <= maxcol&&num <= daysOfMonth; col++)
                {
                    if (col + 1 <optionD&&!done)
                    {
                        cout<<" ";
                    }
                    else
                    {
                        if (num <=daysOfMonth)
                        {
                            cout<<setw(6) <<num++;
                        }
                        done = true;
                    }
                }
                cout<<"\n";
            }
        }
        else
        {
            cout<<"\n\nError!!!! Please try again";
        }
    }
}
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-7-16 10:00:01
| Show all posts
I want to trouble someone to help me change this paragraph:

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>



void days31();

void days();

int main () {
//declare and initialise variables
int month = 1;

//obtian the month in list
cout<<"\n\n January (1)";
cout<<"\n\n February (2)";
cout<<"\n\n March (3)";
cout<<"\n\n April (4)";
cout<<"\n\n May (5)";
cout<<"\n\n June (6)";
cout<<"\n\n July (7)";
cout<<"\n\n August (8)";
cout<<"\n\n September (9)";
cout<<"\n\n October (10)";
cout<<"\n\n November (11)";
cout<<"\n\n December (12)";
cout<<"\n Please select the month: ";
//user input a month
cin>>month;

switch (month){
//in those cases the days will be display 31 days
case 1: case 3: case 5:
    case 7: case 8: case 10:
    case 12:
days31();
break;
//in this case the days will be display 28 days
case 2:
cout<<"\n 28"; //days28();
break;
//in those cases the days will be display 30 days
case 4: case 6: case 9: case 11:
cout<<"\n 30";//days30();
break;
//other input will be show a error message
default:
cout<<"\n error messages!!! it is not correct input, please try again";
}

getch ();
return 0;
}
//this function will display 31 days in "days31"function
void days31()
{

//obtian and initialise variables
int day=1;
int maxrow=6;
int maxcol=6;
int date=0;


//obtain the sharp of calendar from Sun to Sat
cout <<"\n 1. Sunday";
    cout <<"\n 2. Monday";
    cout <<"\n 3. Tuesday";
    cout <<"\n 4. Wednsday";
    cout <<"\n 5. Thusday";
    cout <<"\n 6. Friday";
    cout <<"\n 7. Saturday";
//user input the day

cout<<"\n\n Please Insert the day you wanted: ";
cin>>date;

days(); //display the dates from Sun to Sat

if (date> 0&&date <= 7)
{
for (int row = 1; row <= maxrow&&day <= 31; row++)
{
for(int col = 0; col <= maxcol&&day <= 31; col++)
{
if (day <=31)
{//month have 31 day
   cout<<setw(6) <<day++;//print out the number from 1-31 by 6 spaces

                    }

}
cout<<"\n";
}
}
else
{
cout<<"\n\nError!!!! Please try again";
}


  }


void days() {
  

   cout <<setw(6) << "Sun";
   cout <<setw(6) << "Mon";
   cout <<setw(6) << "Tue";
   cout <<setw(6) << "Wed";
   cout <<setw(6) << "Thu";
   cout <<setw(6) << "Fri";
   cout <<setw(6) << "Sat";
   cout << "\n";
   
}
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-7-18 18:45:02
| Show all posts
Help out
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list