需要帮助将第二个功能合并到我的程序中

时间:2016-02-06 04:47:55

标签: c++

嗨,所以我正在制作一个程序,要求用户输入一个整数并用罗马数字吐出来。我有它的工作,所以罗马数字部分将工作,但我无法循环该程序。现在我有循环的程序,但我不能让罗马数字函数工作。程序执行,但就好像function1不存在,我不知道如何让main函数使用它。我必须在主函数中保持所有用户交互。我想要cout<<罗马<< ENDL;用罗马数字吐出整数的行,但它不起作用。这只是我的第二次任务,所以任何帮助将不胜感激!感谢

#include <iostream>
#include <string>


using namespace std;
string roman;
int integer;
int num;

char answer;

int main()
{
  while (true)
  {
    cout << "Enter Integer " << endl;
    cin >> integer;
    cout << roman << endl;
    cout << "convert another (y/n)? " << endl;
    cin >> answer;
    if (answer == 'n' || answer == 'N')
        break;
  }
}

int function1()
{
  if ((integer >= 4000) || (integer <= 0))
  {
    cout << endl << "Invalid Integer" << endl;
  }
  //3286
  else
  {
    if (integer >= 1000)
    {
        num = (integer / 1000);

        for (int i = 0; i < num; i++)
        {
            roman += 'M';
        }
        integer %= 1000;
        // care ^^^
    }
    // 286
    if (integer >= 100)
    {
        num = (integer / 100);

        if (num == 9)
        {
            roman += "CM";
        }
        else if (num >= 5)
        {
            roman += 'D';

            for (int i = 0; i < num - 5; i++)
            {
                roman += 'C';
            }
        }
        else if (num == 4)
        {
            roman += "CD";
        }
        else if (num >= 1)
        {
            for (int i = 0; i < num; i++)
            {
                roman += 'C';
            }
        }
        integer %= 100;
    }

    // 86
    if (integer >= 10)
    {
        num = (integer / 10);

        if (num == 9)
        {
            roman += "XC";
        }
        else if (num >= 5)
        {
            roman += 'L';

            for (int i = 0; i < num - 5; i++)
            {
                roman += 'X';
            }
        }
        else if (num == 4)
        {
            roman += "XL";
        }
        else if (num >= 1)
        {
            for (int i = 0; i < num; i++)
            {
                roman += 'X';
            }
        }
        integer %= 10;
        // 6

    }

    if (integer >= 1)
    {
        num = integer;

        if (num == 9)
        {
            roman += "IX";
        }
        else if (num >= 5)
        {
            roman += "V";

            for (int i = 0; i < num - 5; i++)
            {
                roman += 'I';
            }
        }
        else if (num == 4)
        {
            roman += "IV";
        }
        else if (num >= 1)
        {
            for (int i = 0; i < num; i++)
            {
                roman += 'I';
            }
        }
    }


    cout << "--> " << roman << endl;

}
system("PAUSE");
return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要实际调用function1,例如:

while (true)
{
    cout << "Enter Integer " << endl;
    cin >> integer;
    function1();
    cout << roman << endl;
    cout << "convert another (y/n)? " << endl;
    cin >> answer;
    if (answer == 'n' || answer == 'N')
        break;
}