I have the basics of the program, but I am stuck on how to establish the functions of the program and how to call them. I know that there is probably an easier way of doing this, but this is what i know how to do. the program is supposed to call a file called uptime, read out the amount of time the system has been up, and the amount of time the system has been idle. It is then suppused to run a program to find an average of 1000 floats to apply work to the system. Then it goes to back to the file and reads the uptime and idle time and calculates a couple of values regarding how much work the program caused for the system. I am at a complete loss as to how to get this program to work. I have 4 functions (starttime, work, endtime, and main), but the only function that works is main. when I put them all into one function (main), it does not make the system work, and therefore does not cause any changes to the uptime and idle time, but it works the way that it should.
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <algorithm>
using namespace std;
float stopTotaltime;
float startTotaltime;
float stopIdletime;
float startIdletime;
int starttime(){
ifstream uptimefile_("/proc/uptime");
std::string line_;
int line = 0;
std::string value1;
std::string value2;
while(line < 1)
{
getline(uptimefile_, line_);
++line;
for(int line =0; line < 1; line++)
{
stringstream iss(line_);
getline(iss, value1, ' ');
getline(iss, value2);
}
}
stringstream beginutime(value1);
stringstream beginitime(value2);
float startTotaltime = 0;
float startIdletime = 0;
beginutime >> startTotaltime;
beginitime >> startIdletime;
uptimefile_.close();
}
int work(){
float i = 0.1;
float sum = 0;
float avg;
while(i < 1000){
sum = sum + i;
i = i + 0.1;
}
avg = sum/i;
}
int endtime(){
ifstream uptimefile1_("/proc/uptime");
std::string value3;
std::string value4;
std::string line_;
int line1 = 0;
while(line1 < 1)
{
getline(uptimefile1_, line_);
++line1;
for(int line1 =0; line1 < 1; line1++)
{
stringstream iss(line_);
getline(iss, value3, ' ');
getline(iss, value4);
}
}
stringstream endutime(value3);
stringstream enditime(value4);
float stopTotaltime = 0;
float stopIdletime = 0;
endutime >> stopTotaltime;
enditime >> stopIdletime;
uptimefile1_.close();
}
int main(){
float beginTotaltime;
float endTotaltime;
float beginIdletime;
float endIdletime;
beginTotaltime = startTotaltime;
endTotaltime = stopTotaltime;
beginIdletime = startIdletime;
endIdletime = stopIdletime;
float programTotaltime = endTotaltime - beginTotaltime;
float programIdletime = endIdletime - beginIdletime;
float programWorktime = programTotaltime - programIdletime;
float percentage = (programWorktime/programTotaltime)*100;
std::cout<<"Time the Program Ran:"<<programTotaltime<<'\n';
std::cout<<"Program Idle Time:"<<programIdletime<<'\n';
std::cout<<"Amount of Time the Program Worked:"<<programWorktime<<'\n';
std::cout<<"Percent of Time the Program Worked:"<<percentage<<'\n';
return 0;
}
I have very little experience with C++ or programming in general and barely understand the basics of it, let alone the complexity of multiple functions within the program. I am willing to learn, but have not been able to find anything that can help me understand these concepts that I am needing for this program.