Reputation: 655
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;
}
}
}
Upvotes: 2
Views: 422
Reputation: 13046
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++.snprintf()
or sprintf()
.std::to_string()
.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();
Upvotes: 1
Reputation: 1657
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.
Upvotes: 2
Reputation: 2578
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.
Upvotes: 3
Reputation: 9082
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.
Upvotes: 3