Converting between data types? C++

I'm getting an error saying [error] cannot convert char to double. i declared double and int type in the structure. How would I fix this? Im still in the beginning phases of learning C++. Please help

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
  #include <iostream>
  #include <fstream>
  #include <string>

  using namespace std;

  struct Employees
  {
	string employeeName;
	string employeeID;
	int rate;
	int hours;
  };

  istream& operator >> (istream& is, Employees& payroll)
  {
	char payrollStuff[256];

	is.getline(payrollStuff, sizeof(payrollStuff));
	payroll.employeeName = payrollStuff;
	
	is.getline(payrollStuff, sizeof(payrollStuff));
	payroll.employeeID = payrollStuff;
	
	is.getline(payrollStuff, sizeof(payrollStuff));
	payroll.rate = payrollStuff;
  };

  int main()
  {
	const int PAYROLL_SIZE = 5; // declare a constant
	

	return 0;
  }
you cannot convert a string to a double directly, you need to use functions that do this (these are part of the language, see stof() for example)

a single char is just an integer, and that works:
char duh = 'x';
double d = duh; //works, d contains the integer value of the ascii table for 'x'

don't use char array strings. use strings.

payroll.rate = payrollStuff; //nope.
payroll.rate = atoi(payrollStuff); //this is the way
Last edited on
Helped me out so much, thanks a lot it worked!
Topic archived. No new replies allowed.