Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling simple C++ program in linux (first time)

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;

        }        
    }
}
like image 379
user2079828 Avatar asked Feb 07 '26 21:02

user2079828


2 Answers

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.

like image 88
jcm Avatar answered Feb 09 '26 11:02

jcm


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.

like image 40
Paul92 Avatar answered Feb 09 '26 11:02

Paul92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!