Use of undeclared identifier 'FunctionName' C++ Xcode

Nearpoint picture Nearpoint · Jun 14, 2013 · Viewed 18.4k times · Source

All I am trying to do is call a simple function from another function, why am I getting errors for each time I call addRomanDigit in the convert_to_Roman function? The program is not complete I am just trying to finish the first function.

//
//  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;
        }
    }

}

Answer

taocp picture taocp · Jun 14, 2013

You should either put declaration of function addRomanDigit before convert_to_Roman

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

or simply move the function definition of addRomanDigit before convert_to_Roman.