2

我要做的就是从另一个函数调用一个简单的函数,为什么每次在 convert_to_Roman 函数中调用 addRomanDigit 时都会出错?程序不完整我只是想完成第一个功能。

//
//  RomanCalculator.cpp
//  HelloWorld
//
//  Created by Feroze on 6/13/13.
//  Copyright (c) 2013 Feroze Shahpurwala. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;


string convert_to_Roman(int value)
{
    string retVal;
   /* if (value < 0)
    {
        retVal ="-";
        value = -value;
    } */

    retVal = addRomanDigit(retVal, value/1000, 'M'); // ERROR HERE and for one below 'Use of undeclared identifier 'addRomanDigit'
    value = value % 1000;
    retVal = addRomanDigit(retVal, value/500,  'D');
    value = value % 500;
    retVal = addRomanDigit(retVal, value/100,  'C');
    value = value % 100;
    retVal = addRomanDigit(retVal, value/50,   'L');
    value = value % 50;
    retVal = addRomanDigit(retVal, value/10,   'X');
    value = value % 10;
    retVal = addRomanDigit(retVal, value/5,    'V');
    value = value % 5;
    retVal = addRomanDigit(retVal, value,      'I');

    return retVal;
}


string addRomanDigit(string starting, int num, char digit)
{
    string retval = starting;
    for (int i=0; i < num; i++)
        retval += digit;
    return retval;
}


int convert_from_Roman(int x)
{
    return 0;
}

int calc_romans(/*figure out the calling sequence*/)
{
    // fill in your code
    // We will be discussing the string class in more detail in
    // subsequent chapters.  The following snippet illustrates a few
    // features we will encounter :

    // string str = "abcdefg";
    // for (int i=0; i < str.length(); i++)
    // {
    //     char c = str[i];  // pull of characters one at a time from the string
    // }

}


void print_Result(/* figure out the calling sequence */)
{
    // fill in your code
}

// Note the call by reference parameters:
void get_Data(ifstream & infile, string & operand1, string & operand2, char & oper, bool & badInput)
{
    // Read in operand1, operand2, oper
    // Check to see if an error condition has occurred.
    // Set badInput for any error, but main will only print out an error condition
    // if the reason for the error is something other than
    // hitting an end of file(out of data) condition.

    //
}

// I would rather have you leave main alone and just make your function calls above match
// the calling sequence required by main below:

int main()
{
    ifstream infile;
    infile.open("roman.txt");
    if (!infile)
    {
        cout << "Can't open roman.txt" << endl;
        return -1;
    }

    while(!infile.eof())
    {
        string operand1, operand2;
        char oper;
        bool badInput;
        get_Data(infile, operand1, operand2, oper,  badInput);

        if (badInput)
        {
            if (!infile.eof()) // Check to see if we are the end of the file
                cout << "Skipping Bad input"<<endl;  // Must be a bad input
        }
        else
        {
            int value1 = convert_from_Roman(operand1);
            int value2 = convert_from_Roman(operand2);
            cout << "The first operand is " << operand1 <<
            "(" << value1 << ")"<<endl;
            cout << "The second operand is " << operand2 <<
            "(" << value2 << ")"<<endl;
            cout <<"The operator is " << oper << endl;
            int answer = calc_romans(value1, value2, oper);
            print_Result(operand1, operand2, convert_to_Roman(answer), answer);
            cout << endl;
        }
    }

}
4

2 回答 2

7

addRomanDigit您应该在之前放置函数声明convert_to_Roman

 string addRomanDigit(string starting, int num, char digit);
 string convert_to_Roman(int value)
 {
     //... function body 
 }

addRomanDigit或者只是移动before的函数定义convert_to_Roman

于 2013-06-13T22:52:41.210 回答
2

您需要先声明一个函数,然后才能使用它。在定义之前添加这一行convert_to_Roman()

string addRomanDigit(string starting, int num, char digit);

addRomanDigit()或者,移动之前的整个定义convert_to_Roman()

于 2013-06-13T22:52:38.527 回答