Calling a function in a function without parameters ?

Hey, in line 24th void print I don't want to use p and int parameters because I'm not using them for printing only the value that I got from line 18th function. However, I'm calling a function in the function. Is there another way how I could do that without using p and t in parameters or if I'm calling a function in a function I must use the values ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const float spendsPerMonth = 43.6;
const int hundredPercent = 100;

const char duom[] = "d.txt";
const char rez[] = "r.txt";

void read(int& p, int& t)
{
  ifstream in(duom);
  in >> p >> t;
}
float spendsForLiving(int p, int t)
{
  float spends;
  spends = (float)p * spendsPerMonth / hundredPercent;
  return spends;
}
void print(int p, int t)
{
  float expenses = spendsForLiving(p, t);
  ofstream out(rez);
  out << "Paul spends per month: " << fixed << setprecision(2) << expenses << " euros" << endl;

}

int main()
{
    int p, t;
    read(p, t);
    spendsForLiving(p, t);
    print(p, t);
    return 0;
}
Last edited on
It's usually a good idea to separate computation from presentation. In other words, compute your results in one place and present them to the user somewhere else:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
void print(float e, const char *fileName)
{
  ofstream out(fileName);
  out << "Paul spends per month: " << fixed << setprecision(2) << e << " euros"\
 << endl;

}

int main()
{
    int p, t;
    read(p, t);
    float expenses = spendsForLiving(p, t);
    print(expenses, rez);
    return 0;
}

Topic archived. No new replies allowed.