i have this line taken from a txt file:
"#operation=1(create circle and add to picture) name X Y radius." (first line in the file)
why does this code doesnt take the integer 1 and puts it into k?
Circle Circle::CreateCirc(Circle c){
int k;
ifstream myfile("cmd.txt");
if (!myfile.is_open())
cout<<"Unable to open the requested file "<<endl;
string line,line2="create circle";
for (int i=1;i<countrows();i++)
{
getline(myfile,line);
if (line.find(line2)!=string::npos)
{
istringstream ss(line);
ss>>k;
cout<<k<<endl;
}
}
return c;
}
The read operation on line 13 will fail because the stream reads from the beginning of the line. It fails to read an integer because the first character it finds is the # character.
int n = 0;
string line = "#operation=1(create circle and add to picture) name X Y radius.";
istringstream ss(line);
ss.ignore(100,'='); // ignore up to and including '='
if (ss >> n)
cout << "Number is " << n << endl;