I am currently writing a program that reads in numbers from a.txt file but I require it to break the loop once it read stop in the file
I know this while
statement is wrong, but I this is the just of what I am trying to accomplish.
while (inFile >> x && x != stop)
{
//the basic adding up of the numbers
sum = sum + x;
cout<<sum<<endl;
sum = x;
}
if (x == stop)
{
cout<<"File reading stopped";
}
inFile.close();
return 0;
I need the code to stop reading int's as soon as it reads stop within the.txt file.
I know some of the code is completely wrong but I have tried to search for as much answers as possible.
int sum = 0;
std::string aLine;
bool bFoundStop(false);
while (getline(inFile, aLine) && !bFoundStop)
{
//the basic adding up of the numbers
if(aLine == "stop")
{
bFoundStop = true;
}
else
{
int x = atoi(aLine.c_str());
sum += x;
}
std::cout<<sum<<std::endl;
}
inFile.close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.