A student was asking about "sscanf" and how it works for the week 3 ToDo (basicMath challenge), so here is the example:
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
double d;
char num1[80] = "1234.5678";
char num2[80] = "1234.5678a";
char num3[80] = "12a34.5678";
char num4[80] = "a1234.5678";
char ch ;
int ret;
d = 0.0;
ch = 'X';
ret = sscanf(num1,"%lf%c", &d, &ch);
cout<<ret<<endl;
cout<<ch<<endl;
cout<<d<<endl;
cout<<"----------------------------"<<endl;
d = 0.0;
ch = 'X';
ret = sscanf(num2,"%lf%c", &d, &ch);
cout<<ret<<endl;
cout<<ch<<endl;
cout<<d<<endl;
cout<<"----------------------------"<<endl;
d = 0.0;
ch = 'X';
ret = sscanf(num3,"%lf%c", &d, &ch);
cout<<ret<<endl;
cout<<ch<<endl;
cout<<d<<endl;
cout<<"----------------------------"<<endl;
d = 0.0;
ch = 'X';
ret = sscanf(num4,"%lf%c", &d, &ch);
cout<<ret<<endl;
cout<<ch<<endl;
cout<<d<<endl;
cout<<"----------------------------"<<endl;
return 0;
}
output:
1
X
1234.57
----------------------------
2
a
1234.57
----------------------------
2
a
12
----------------------------
0
X
0
----------------------------