2

I'm getting an error compiling a simple program using c++ in linux. There are 3 files: Employee.h , Employee.cpp, and Salary.cpp (main). I'm including several system headers in Salary.cpp <iostream>, <fstream>, <string>, <stdio.h>, <stdlib.h> The only reason I'm using is for the itoa() function. I could not get it to compile and read somewhere that '' is sometimes a prerequisite.
The error I get is: 'Salary.cpp:195:47: error: itoa was not declared in this scope

Now, I i've included the headers in the global scope, and itoa() is only used in the file that I include so I don't know why this would happen other than it is not including the correct system headers. Do I need to specify all the system headers in the command line or something? I'm not quite sure what is going on.

Edit: Here is some of the source code...I've only expanded on what is needed to keep it short. The error occurs in the addEmployee() function near the bottom...i'm not sure I know how to put line numbers. It's right after create a new Employee()

#include "Employee.h"

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>


void findEmployees(Employee*[], const short);
void addEmployee(Employee*[], short&);
unsigned short parseDataFile(Employee* [], short);
bool insertEmployee(Employee*[], short, Employee*);

int main(int argc, char** argv)
{
    //100 employees max, too lazy to create dynamic container
    Employee* employeeList[100];

    //Parse data file, return the length of the list made
    short listLen = parseDataFile(employeeList, 100);

    //Employee database is built, run query engine
    std::cout << "*************************************************\n";
    std::cout << "This program lets you search for salaries of employees or input new employee information.\n";
    std::cout << "*************************************************\n\n\n";

    char choice = { 0 };
    while (true)
    {
        std::cout << "Please choose an option: \n";
        std::cout << "(1) Search for employee salaries.\n";
        std::cout << "(2) Input new employee data\n";
        std::cout << "(3) Exit\n\n";
        std::cin >> choice;

        switch (choice)
        {

        case '1':
            findEmployees(employeeList, listLen - 1);
            break;

        case '2':
            addEmployee(employeeList, listLen);
            break;

        case '3':
            exit(0);
            break;
        }
    }

    return 0;
}

unsigned short parseDataFile(Employee* empList[], short len)
{
    //Do stuff
}

void findEmployees(Employee* empList[], const short len)
{
    //Do stuff
}

void addEmployee(Employee* empList[], short& len)
{
    char first[32] = "";
    char last[32] = "";
    char salary[32] = "";
    char id[32] = "";
    bool loop = true;

    while (loop)
    {
        std::cout << "Input Last Name:  ";
        std::cin.clear();
        std::cin.sync();
        std::cin.getline(last, 31);
        std::cout << std::endl << std::endl;

        std::cout << "Input First Name:  ";
        //Get user name
        std::cin.clear();
        std::cin.sync();
        std::cin.getline(first, 31);
        std::cout << std::endl << std::endl;

        std::cout << "Input Salary:  $";
        //Get user name
        std::cin.clear();
        std::cin.sync();
        std::cin.getline(salary, 31);
        std::cout << std::endl << std::endl;

        Employee* employee = new Employee();
        employee->setID( itoa((int)len, id, 10) ); //Set id

        employee->setFirstName(first); //Set first name
        employee->setLastName(last); //Set last name
        employee->setSalary(salary); //Set salary

        //Inserts new employee at the end of the list, no real reason to sort the list for this assignment
        //I guess I could have used std::vector to make it easy
        empList[len] = employee;
        ++len; //Increment length of the list

        char yesNo = { 0 };
        std::cout << "Would you like to enter another employee?  (Y, N):  ";
        std::cin >> yesNo;
        std::cout << std::endl << std::endl;
        switch (yesNo)
        {
            case 'Y':
            case 'y':
                //do nothing
            break;

            case 'N':
            case 'n':
                loop = false;
            break;

        }        
    }
}
5
  • I think (but I'm not sure, I have very little C++ experience) that you need to include <cstdlib> and <cstdio> so that those functions are in the std namespace. Commented Nov 23, 2013 at 20:40
  • Can you please show us some of your source code? I think it should work with the headers you are using. Commented Nov 23, 2013 at 20:43
  • itoa not a member of std. When including those files I still get the same error. Updating with source, 1-2 min. Commented Nov 23, 2013 at 20:44
  • Also, possible duplicate of stackoverflow.com/questions/6462938/… Commented Nov 23, 2013 at 20:45
  • Yes, I did not find that question for some reason, my fault. Commented Nov 23, 2013 at 21:05

4 Answers 4

3

Linux does not provide an itoa implementation. Best way to achieve same behavior, and working with C++11, is using std::to_string in the following way:

std::string tmp = std::to_string(1);

If you're using an older C++ version, you can use string streams:

std::stringstream tmpSS;
tmpSS << 1;
std::string tmp = out.str();

Edit: In provided example, you would need to also call std::string's c_str() method:

employee->setID( (char*) tmp.cstr() ); //Set id

Where tmp is one of previous options.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I am trying this now. Edit: Oh, well I guess I'll have to convert everything to std:string as I have it using char arrays.
@user2079828 Please, check my last update, you can use std::string's c_str method and cast it to (char*) (this is important or you will get an const correctness error if setID expects a pointer to char)
Yes, currently working that. Didn't see the update. Edit: nm, I'm idiot...member is tmpSS not the function durr. Edit again: That worked thank you, and yes it is an older version I'm assuming.
3

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

So it seems that the problem is that your compiler dows not support the itoa function.

Comments

2

Not sure what is the problem (show part of you code?). itoa is a non-standard function in C++.

If you want to convert integer to string in C++, and you can use std::to_string function in C++11.

Comments

1
  1. If we're speaking about itoa() simple answer is you should not use this function. It does not even belongs to standard C library. Of course it is not related to C++.
  2. If you still want to use C style, please use appropriate formatting function like snprintf() or sprintf().
  3. If you really want C++ and have C++11 support, please use std::to_string().
  4. If you don't have C++11 support but want to be C++ native, you can use things like str::ostringstream.

2 approaches in cases of C++ without C++11

Example of last approach which you could actually wrap in class / method:

int a = 13;
stringstream ss2;
ss2 << a;
string str2 = ss2.str();

Alternative, something like (warning, actually it is somewhat dirty, please use approach above unless you critically need it):

int Number = 123;
string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str();

2 Comments

Yes, this is what jcm provided. Nevertheless, I thank you for the information.
Well, it was probably after I already started to write. For me most important old C++ practice here is last one (which looks like not proposed by anyone here) but I don't like static_cast<>. Why it is so important? Looks like it is only alternative to std::to_string() which can be inserted in expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.