In: Computer Science
#include <chrono.h> #include <stdlib.h> #include <iostream.h> #include<conio.h> #include <iomanip.h> //#include <string.h> #include <fstream.h> //using namespace std; int main(int argc, char * argv[]) { ifstream myfile; myfile.open("Input.txt"); auto st=high_resolution_clock::now(); float sum = 0; float a=0; //myfile >> a; int i=0; while (!myfile.eof()) { myfile >> a; sum+=a; } myfile.close(); auto stp=high_resolution_clock::now(); auto dur=duration_cast<microseconds>(stp-st); cout<<"Total time taken ="<<dur.count()<<"ms"<<endl;// displaying time taken cout<<"Sum of integers: "<<sum<<endl; // displaying results getch(); return 0; }
can someone please tell me what is wrong with this code. Its C++ programming
Below is the modified version of your code :
The only error you were facing is that your code needs type casting . please go through this code below
#include <chrono>
#include <stdlib.h>
#include <iostream>
//#include<conio>
#include <iomanip>
//#include <string.h>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
ifstream myfile;
myfile.open("Input.txt");
//auto st=high_resolution_clock::now();
auto st = std::chrono::high_resolution_clock::now();
float sum = 0;
float a=0;
//myfile >> a;
int i=0;
while (!myfile.eof())
{
myfile >> a;
sum+=a;
}
myfile.close();
auto stp=std::chrono::high_resolution_clock::now();
//auto dur=duration_cast<microseconds>(stp-st);
auto dur =
std::chrono::duration_cast<std::chrono::milliseconds>(stp -
st);
cout<<"Total time taken ="<<dur.count()<<"ms"<<endl;// displaying time taken
cout<<"Sum of integers: "<<sum<<endl; // displaying results
getch();
return 0;
}