Conversion failure (strings to int and double)

I've made the conversions of strings to a int and a double but it displays errors. Can someone help/tell me what I did wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <cstdlib>
 
using namespace std;
 
class Employee
{
    private:
        string firstName;
        string lastName;
        char gender;
        int dependents;
        double annualSalary;
 
    public:
        Employee();
        Employee(string, string, char, int, double);
        double calculatePay();
        void displayEmployee();
        void setFirstName(string);
	string getFirstName();
	void setLastName(string);
	string getLastName();
	void setGender(char);
	char getGender();
	void setDependents(int);
	int getDependents();
        void setAnnualSalary(double);
	double getAnnualSalary();
	void DisplayApplicationInformation();
	void DisplayDivider(string);
	string GetInput(string);
	void TerminateApplication();
};
 
Employee::Employee()
{
    firstName = "not given";
    lastName = "not given";
    gender = 'U';
    dependents = 0;
    annualSalary = 20000;
};
 
Employee::Employee(string first, string last, char gen, int depends, double anSalary)
{
    firstName = first;
    lastName = last;
    gender = gen;
    dependents = depends;
    annualSalary = anSalary;
};
 
void Employee::setFirstName(string newFirstName)
{
	firstName = newFirstName;
}

string Employee::getFirstName()
{
	return firstName;
}

void Employee::setLastName(string newLastName)
{
	lastName = newLastName;
}

string Employee::getLastName()
{
	return lastName;
}

void Employee::setGender(char newGender)
{
	gender = newGender;
}

char Employee::getGender()
{
	return gender;
}

void Employee::setDependents(int newDependents)
{
	dependents = newDependents;
}

int Employee::getDependents()
{
	return dependents;
}

void Employee::setAnnualSalary(double newannualSalary)
{
	annualSalary = newannualSalary;
}

double Employee::getAnnualSalary()
{
	return annualSalary;
}

void Employee::displayEmployee()
{
    cout << "Employee Name: " << firstName << " "<< lastName << endl;
    cout << "Employee Gender: " << gender << endl;
	cout << "Employee Dependents: " << dependents << endl;
    cout << "Empoyee Annual Salary: " << annualSalary << endl;
    cout << "Annual Salary:\t" << setprecision(2) << showpoint << fixed << annualSalary << "\n";
	cout << "Employee Weekly Salary: " << annualSalary/52 << endl << endl;
};

double Employee::calculatePay()
{
    return annualSalary/52;
};

void DisplayApplicationInformation()
{
	cout << "Welcome the Basic User Interface Program" << endl;
	cout << "CIS247, Week 2 Lab" << endl;
	cout << "Name: Ignacio Huerta" << endl << endl;
}; 
void DisplayDivider(string outputTitle)
{
	cout << "**********"<< outputTitle << "**********" << endl << endl;
};

string GetInput(string inputType)
{
	string strInput;
	cout << "Enter the Employee " << inputType << endl;
	cin >> strInput;
	return strInput;
}

void TerminateApplication()
{
	cout << "Thank you for using the Basic User Interface program" << endl;
}

void main()
{

	DisplayApplicationInformation();
	DisplayDivider("Employee 1");
	Employee Employee1;

	string firstName = GetInput("First Name");
	Employee1.setFirstName(firstName);
	
	string lastName = GetInput("Last Name");
	Employee1.setLastName(lastName);

	string dependents = GetInput("Dependents");
	dependents = atoi(dependents.c_str());
	Employee1.setDependents(dependents);

	string annualSalary = GetInput("Annual Salary");
	annualSalary = atof(annualSalary.c_str());
	Employee1.setAnnualSalary(annualSalary);

	Employee1.displayEmployee();

	TerminateApplication();
}
You are trying to assign an int to a string. So why do you wonder that the compiler issued an error?!

dependents = atoi(dependents.c_str());
Also better than atoi - atod there are "strtol" for integers and "strtod" for decimals. They have error checking, so using them is safer.

http://www.cplusplus.com/reference/cstdlib/strtol
http://www.cplusplus.com/reference/cstdlib/strtod
Last edited on
> conversions of strings to a int and a double

If the implementation supports it, use std::stoi() and std::stod()
http://en.cppreference.com/w/cpp/string/basic_string/stol
http://liveworkspace.org/code/wICOj$1


If not, use std::istringstream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <sstream>
#include <stdexcept>

template < typename NUMERIC_TYPE > NUMERIC_TYPE to_number( const std::string& str )
{
    std::istringstream stm(str) ;
    NUMERIC_TYPE n ;
    if( stm >> n ) return n ;
    else throw std::invalid_argument( "not a number" ) ;
}

int main()
{
    const std::string a = "1234", b = "-3.67", c = "not a number" ;
    try
    {
        const int i = to_number<int>(a) ;
        std::cout << i << '\n' ;
        const double d = to_number<double>(b) ;
        std::cout << d << '\n' ;
        const long long ll = to_number<long long>(c) ;
        std::cout << ll << '\n' ;
    }
    catch( const std::exception& e )
    {
        std::cerr << "error in converting string to numeric type\n" ;
    }
}
Topic archived. No new replies allowed.