使用cout和setw for循环使列对齐困难

时间:2014-06-25 12:17:06

标签: c++ for-loop cout

我尝试使用for循环,cout和setw将3列打印到控制台。

数据打印到屏幕上,但严重损坏。

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const int TABLE_WIDTH = 12;
const int GRAPH_WIDTH = 12;

int i = 0;
int rainfall = 0;
int difference = 0;
int formatType;
char repeatAnswer;

string monthNames[12] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
string convertMonth(int month);

int averageRainfall[12];
int actualRainfall[12];

void inputAverage();
void inputActual();

void printAsterisks(int numberAsterisks);
void graphActual(const int asteriskCount[], int lastMonth[]);
void graphAverage(const int asteriskCount[], int lastMonth[]);

int _tmain(int argc, _TCHAR* argv[])
{
do
{
    inputAverage();

    //  int currentMonth;

    inputActual();

    cout << "\n";
    cout << "This program can display the results in tabular form or graphical 
form." << endl;
    cout << "\n";
    cout << "For graphical form please press the [1] key, then the [ENTER] key.  
For" << endl;
    cout << "tablular form please press the [2] key, then the [ENTER] key: ";
    cin >> formatType;

    if (formatType == 1)
    {
        cout << "\n";
        graphActual(actualRainfall, averageRainfall);
        cout << "\n";
        graphAverage(averageRainfall, actualRainfall);
    }

    if (formatType == 2)
    {
        cout << left << setw(TABLE_WIDTH) << " Month" << left <<
 setw(TABLE_WIDTH) << "Actual" << left << setw(TABLE_WIDTH) << "Average";
        cout << left << setw(TABLE_WIDTH) << " -----" << left << 
 setw(TABLE_WIDTH) << "------" << left << setw(TABLE_WIDTH) << "-------";

        cout << "\n";
        for (int i = 0; i < 12; i++)
        {
            cout << left << setw(TABLE_WIDTH) << monthNames[i] << left 
 << setw(TABLE_WIDTH) << actualRainfall[i] 
                << left << setw(TABLE_WIDTH) << averageRainfall[i];
        }

    }

    cout << "Would you like to run this program again? (y/n): ";
    cin >> repeatAnswer;

}while (repeatAnswer == 'Y' || repeatAnswer == 'y');
cout << "\n";
cout << "This program will now terminate.";

return 0;
}

void printAsterisks(int numberAsterisks)
{
for (int count = 1; count <= numberAsterisks; count++)
    cout << "*";
}

void graphActual(const int asteriskCount[], int lastMonth[])
{
int counter = 0;
int currentMonth = 2;

cout << left << setw(GRAPH_WIDTH) << " Month" << left << setw(GRAPH_WIDTH) <<
 "Actual Rainfall" << endl;
cout << left << setw(GRAPH_WIDTH) << " -----" << left << setw(GRAPH_WIDTH) << "----
-----------";
cout << "\n";

for (int month = currentMonth - 1; counter < 12; month = (month + 1) % 12, 
counter++)
{
    string textMonth;
    textMonth = convertMonth(month);
    cout << left << setw(GRAPH_WIDTH) << textMonth;
    printAsterisks(asteriskCount[month-1]);
    cout << endl;
}
}

void graphAverage(const int asteriskCount[], int lastMonth[])
{
int counter = 0;
int currentMonth = 2;

cout << left << setw(GRAPH_WIDTH) << " Month" << left << setw(GRAPH_WIDTH) <<
 "Average Rainfall" << endl;
cout << left << setw(GRAPH_WIDTH) << " -----" << left << setw(GRAPH_WIDTH) << "----
------------";
cout << "\n";

for (int month = currentMonth - 1; counter < 12; month = (month + 1) % 12,
 counter++)
{
    string textMonth;
    textMonth = convertMonth(month);
    cout << left << setw(GRAPH_WIDTH) << textMonth;
    printAsterisks(asteriskCount[month - 1]);
    cout << endl;
}
}

string convertMonth(int month)
{
string showMonth;

if (month == 1)
    showMonth = "January";
else if (month == 2)
    showMonth = "February";
else if (month == 3)
    showMonth = "March";
else if (month == 4)
    showMonth = "April";
else if (month == 5)
    showMonth = "May";
else if (month == 6)
    showMonth = "June";
else if (month == 7)
    showMonth = "July";
else if (month == 8)
    showMonth = "August";
else if (month == 9)
    showMonth = "September";
else if (month == 10)
    showMonth = "October";
else if (month == 11)
    showMonth = "November";
else if (month == 0)
    showMonth = "December";

else cout << "Invalid Input.";

return (showMonth);
}


void inputAverage()
{
cout << "Please enter the average rainfall for each month of the year" << endl;
cout << "\n";
cout << left << setw(TABLE_WIDTH) << " Month" << setw(TABLE_WIDTH) << "Rainfall" <<
 endl;
cout << left << setw(TABLE_WIDTH) << " -----" << setw(TABLE_WIDTH) << "--------" <<
 endl;

for (int i = 0; i < 12; i++)
{
    cout << left << setw(TABLE_WIDTH) << monthNames[i];
    cin >> averageRainfall[i];
}
return ;
}

void inputActual()
{
int currentMonth;

cout << "\n";
cout << "Please enter the number of the current month" << endl;
cout << "(e.g. January = 1, February = 2, March = 3, etc.): ";
cin >> currentMonth;

cout << "\n";
cout << "Please enter the actual rainfall for each month of the previous year" <<
 endl;
cout << "\n";
cout << left << setw(TABLE_WIDTH) << " Month" << setw(TABLE_WIDTH) << "Rainfall" <<
 endl;
cout << left << setw(TABLE_WIDTH) << " -----" << setw(TABLE_WIDTH) << "--------" <<
 endl;

int counter = 0;
for (int month = currentMonth - 1; counter < 12; month = (month + 1) % 12, counter++)
{
    cout << left << setw(TABLE_WIDTH) << monthNames[month];
    cin >> actualRainfall[month];
}
}

当用户选择&#34; 2&#34;时,我试图以表格形式在整齐定义的列中打印数据。在提示。我相信问题是由这个for循环产生的:

for (int i = 0; i < 12; i++)
{
    cout << left << setw(TABLE_WIDTH) << monthNames[i] << left << setw(TABLE_WIDTH) 
    << actualRainfall[i]  << left << setw(TABLE_WIDTH) << averageRainfall[i];
}

如果有人能指出我正确的方向,我将非常感激。

提前感谢您看一下。

亲切的问候, 伊丽莎白C.

0 个答案:

没有答案