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 ==========