ATOI

I keep getting the error:error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'


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
int main ()
{
	int totalChar = 0;
	int fileSize = 0;
	int temp;
	char cTemp;
	char input [100];
	ofstream pali;
	pali.open ("pali.txt");

	while (!pali.eof())
	{
		pali << input[fileSize];
		fileSize++;
	}

	for (int i = 0; i < fileSize; i++)
	{
		cTemp = tolower(input[i]);
		temp = atoi(cTemp);
		cout << temp;
	}

	return 0;
}
atoi() works on a C-string (char*), not a single letter (char).
i've tried making cTemp a short string and a pointer and it still say can't convert

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
int main ()
{
	int totalChar = 0;
	int fileSize = 0;
	int temp;
	char cTemp[3];
	char input [100];
	ofstream pali;
	pali.open ("pali.txt");

	while (!pali.eof())
	{
		pali << input[fileSize];
		fileSize++;
	}

	for (int i = 0; i < fileSize; i++)
	{
		cTemp[1] = tolower(input[i]);
		temp = atoi(cTemp[1]);
		cout << temp;
	}

	return 0;
}
temp = atoi(*cTemp); ?
cTemp[1] is a character. use cTemp + 1 instead. You need to set cTemp[2] to '\0' though.
I made the code work adding various comments. If there is anything you don't get, just ask.
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
#include <fstream>
#include <iostream>
#include <cctype>
#include <cstdlib>

int main ()
{
	int totalChar = 0;
	int fileSize = 0;
	int temp;

	// shouldn't this be 2, not 3?
//	char cTemp[3];
	char cTemp[2];

	char input [100];

	// shouldn't this be an input stream?
//	std::ofstream pali;
	std::ifstream pali;

	pali.open("pali.txt");

	// not a good way to loop through a file

//	while (!pali.eof())
//	{
//		pali << input[fileSize];
//		fileSize++;
//	}

	// The problem is that EOF is not signalled until AFTER
	// you attempt the read.

	while(pali >> input[fileSize])
	{
		// only gets here is read was a success
		fileSize++;
	}

	for (int i = 0; i < fileSize; i++)
	{
		// cTemp[0] is th efirst element, not 1
//		cTemp[1] = tolower(input[i]);

		cTemp[0] = tolower(input[i]);
		cTemp[1] = '\0'; // set string terminator

		// This passes a single char, need to pass the whole string
//		temp = atoi(cTemp[1]);
		temp = atoi(cTemp);
		std::cout << temp;
	}

	return 0;
}

Mainly arrays are indexed from 0, not 1. Strings need to be terminated by the value '\0'. So you needed to put a '\0' as your second char in cTemp.
1
2
cTemp[0]; // contains the char you read in
cTemp[1]; // contains '\0' (end of string marker) 
Last edited on
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
int main ()
{
	int totalChar = 0;
	int fileSize = 0;
	int temp;
	char cTemp;
	char input [100];
	ofstream pali;
	pali.open ("pali.txt");

	while (!pali.eof())
	{
		pali << input[fileSize];
		fileSize++;
	}

	for (int i = 0; i < fileSize; i++)
	{
		cTemp = tolower(input[i]);
		temp = atoi(cTemp);//here change to "temp = atoi(&cTemp);"
		cout << temp;
	}

	return 0;
}
That wouldn't work, because &cTemp wouldn't be NULL-terminated.
Topic archived. No new replies allowed.