"未找到标识符"从函数调用函数时出错

时间:2014-06-24 13:53:30

标签: c++ function

我是C ++的新手,并编写了一个小程序来绘制一些数据。我试图将表示月份的数字数组转换为图形输出的标签字符串。

当我编译时,我看到"标识符未找到"尝试从convertMonth()函数调用graph()函数时。

我无法弄清楚出了什么问题。这是我的代码:

#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;

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

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

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

    // the average rainfall for each month of the year
    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];
    }


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


    // the actual monthly rainfall for the previous 12 months
    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];
    }



    //  for the difference between the actual monthly rainfall and the average monthly
    rainfall
    cout << "\n";
    cout << "The differences by month are:" << endl;
    cout << "\n";
    cout << left << setw(TABLE_WIDTH) << " Month" << left << setw(TABLE_WIDTH) << "Difference" << endl;
    cout << 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] - averageRainfall[i]) << endl;
    }

    cout << "\n";
    graph(actualRainfall, averageRainfall);

    return 0;
}

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

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

    cout << left << setw(GRAPH_WIDTH) << " Month" << left << setw(GRAPH_WIDTH) << "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) << month;
        printAsterisks(asteriskCount[month]);
        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 == 8)
        showMonth = "September";
    else if (month == 10)
        showMonth = "October";
    else if (month == 11)
        showMonth = "November";
    else if (month == 12)
        showMonth = "December";
    else cout << "Invalid Input.";

    return (showMonth);

}

1 个答案:

答案 0 :(得分:4)

convertMonth在尝试调用之后首先声明。您需要将其定义移至graph之上,或在graph之前发出前瞻性声明。