Anybody please show how to convert a string, for example in a text doc, to an int... and then maybe add 1 and show total output.

text.doc could simply contain

I have 3 apple.


output:

4


this is my try, i know i used atoi() incorrectly.. please show how to fix this error

# include <iostream>
# include <fstream>
# include <cstdlib>
# include <string>
# include <cstdio>
# include <cctype>
using namespace std;

int main()
{	int i;
	ifstream reader;

	reader.open("test.txt");

	if(reader.fail())
	{
		perror("error");
	}

	else
	{
		puts("ok");
	}

	string line;
	
	while(reader>>line)
	{
		if(isDigit(line))
		{

		i=atof(line);
		}

		i+=1;
		cout<<i<<" ";
	}

	return 0;

}

1>------ Build started: Project: testThing, Configuration: Debug Win32 ------
1>Compiling...
1>testThing.cpp
1>c:\documents and settings\lih\my documents\visual studio 2005\projects\testthing\testthing\testthing.cpp(29) : error C3861: 'isDigit': identifier not found
1>c:\documents and settings\lih\my documents\visual studio 2005\projects\testthing\testthing\testthing.cpp(32) : error C2664: 'atof' : cannot convert parameter 1 from 'std::string' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://c:\Documents and Settings\lih\My Documents\Visual Studio 2005\Projects\testThing\testThing\Debug\BuildLog.htm"
1>testThing - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

>>isDigit
misspelled, its isdigit()

>>i=atof(line);
atof() converts a string to float, not integer. what you want is atoi() or atol(), and takes a char*, not a std::string i = atol(line.c_str()); If there is only one digit then you don't need any conversion function i = line[0] - '0';

>>isDigit
misspelled, its isdigit()

>>i=atof(line);
atof() converts a string to float, not integer. what you want is atoi() or atol(), and takes a char*, not a std::string i = atol(line.c_str()); If there is only one digit then you don't need any conversion function i = line[0] - '0';

still anything wrong with my code?

# include <iostream>
# include <fstream>
# include <cctype>
# include <string>

using namespace std;

int main()
{
	ifstream reader;
	reader.open("text.txt");
	
	string line;
	int i;
	while(reader>>line)
	{
		if(isdigit(line))
		{
			i=atoi(line.c_str());
		}

	}

	i=i+1;

	cout<<i;

	return 0;
}

1>------ Build started: Project: testThing, Configuration: Debug Win32 ------
1>Compiling...
1>testThing.cpp
1>c:\documents and settings\lih\my documents\visual studio 2005\projects\testthing\testthing\testthing.cpp(17) : error C2664: 'isdigit' : cannot convert parameter 1 from 'const char [5]' to 'int'
1> There is no context in which this conversion is possible
1>Build log was saved at "file://c:\Documents and Settings\lih\My Documents\Visual Studio 2005\Projects\testThing\testThing\Debug\BuildLog.htm"
1>testThing - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

>>if(isdigit(line))
idigit takes a single character and you are sending the whole string. What you should do is this: if(isdigit(line[0])) Your work would go a lot faster when you learn to recognize how to correct your errors. Your compiler told you exactly what line number had the error and what the error was. All you had to do was correct it.

>>if(isdigit(line))
idigit takes a single character and you are sending the whole string. What you should do is this: if(isdigit(line[0])) Your work would go a lot faster when you learn to recognize how to correct your errors. Your compiler told you exactly what line number had the error and what the error was. All you had to do was correct it.

I tried different attempt to fix the error, however, I couldn't correctly break a string down into a char array which fit in the isdigit() .... say if my text has "i have 322 apple" ... how do i read every string and check if it is a digit? And after checking, how do i atoi() the string ?

the code below logically would read the '3' ? instead of 322 because it is reading char? ... if possible, could anyone make it read the string "322" and turn it into an int?

int main()
{
	ifstream reader;
	reader.open("test.txt");

	if(reader.fail())
	{
		cerr<<"fail opening";
	}
	
	string line;
	int i=0;

	while(reader>>line)
	{
		
		if(isdigit(line[0]))
		{
			i=atoi(line.c_str());
		}

		
	}

	i=i+1;

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.