-2

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.

  • This is **a lot** of code. Please create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Geno C Jul 27 '20 at 02:52
  • 2
    Trying to write code in a programming language when you don't have a basic understanding of that language tends to result in code that nobody else can understand either. That appears to be exactly what you have done. Your description is also unclear - anyone would have to be a mindreader to make head or tail of it, and there aren't too many mindreaders here. About all I can suggest is that you find a good introductory text on C++, and work through it gradually to learn the basics BEFORE trying to write a larger program. – Peter Jul 27 '20 at 02:57
  • @Peter do you know of any good sources for me? I keep getting stuck in these classes for my degree that require programming and have not had any luck with finding something or someone who can help me. I would much rather not learn how to program, but I am finding that much harder to do with these classes I have to take. – Stephen Dangel Jul 27 '20 at 03:01
  • Don't write much code at a time. You catch mistakes faster and are much less likely to repeat them. Write a few lines. Compile. Test. Only when the program works as expected do you add more lines. If the program does not work, odds are good the error is in the few lines that were just added. Also do not ignore compiler warnings. A compiler warning means the syntax is correct and a run-able program cam be generated, but odds are high that the program will not work as expected. – user4581301 Jul 27 '20 at 03:02
  • @StephenDangel - The link given by Geno C in his answer has some good books. Focus on overtly beginner-centric ones where you can. If you're having to do classes on programming for your degree, you might want to focus more effort on learning for those classes - if your degree requires programming classes, that's normally a hint that work related to that degree depends on such skills. Doing a degree that requires programming (directly or indirectly) for someone who wants to avoid programming is usually a critical mismatch - and a reason to question why they are doing that degree. – Peter Jul 27 '20 at 04:46

1 Answers1

1

There is a few things wrong with your code.

For starters, your three functions: starttime(), work(), and endtime() do not return a value. There is a few things you can do to at least run your program.

starttime() needs to say void starttime() instead of having int. So does endtime().

Then in int work() you need to return avg. So you would put in the last line of the function definition int work(): return avg;.

That should atleast get your program running without a thorough explanation. However, you should invest in a good C++ book.

Geno C
  • 1,401
  • 3
  • 11
  • 26