So i just recently learned about functions (very new to programming) and after reading the section in the tutorial on this site about void functions i got a bit confused.
From what i understand a void function is just there to seperate your code from the main function since the function wont really return anything to you.
I made a simple program a few days ago that is supposed to separate numbers from two files (A and B) and then put them in the correct order in a new file (C).
I was thinking about adding a function into a separate .cpp file but i did not know where to start. Last time i did this i had a function return true or false and therefore i could easily come to the conclusion that i should use a bool function.
But looking a my code right now i did not really know what kind function to use but i am going to guess it would be some sort of void function?
Also as a separate question, i made my bool function without any parameters and it worked perfectly, do i need any parameters and if so what would they be?
This is my code for my bool function. As you can see i have no parameters.
Please ignore the swedish comments. ;)
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 41 42 43 44 45
|
#include <iostream>
#include <fstream>
#include "function1.h" // Länkar till headerfilen function1.h
using namespace std;
// Deklarerar variabler
ifstream numbers;
int newnr;
int oldnr;
//Bool funktion som ska returna (true) eller (false)
bool IsFileSorted()
{
// Läser in värdena ur vald fil.
numbers.open("B");
// Kollar att filen öppnades ordentligt, annars meddelas vi.
if (!numbers.is_open())
cout << "The file did not open correctly" << endl;
// Lägger in värdet i variabeln numbers i variabeln oldnr.
numbers >> oldnr;
// While-sats som läser in värden tills de är slut.
while (!numbers.eof())
{
numbers >> newnr;
// If-satsen kollar att de tidigare nummret inte är större än det nya.
// Om detta sker returnas (false) och funktionen avbryts.
if (oldnr > newnr)
return false;
oldnr = newnr;
}
// Stänger filen som en extra säkerhetsåtgärd.
numbers.close();
// Ifall funktionen aldrig avbryts i if-satsen ovanför gör den de här
// och returnerar (true).
return true;
}
|
And this is my code that put the numbers in the correct order. As you can see i have it all in the main function. Is possible to use a void function here to separate my code from main function, and how would that look like?
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
include <iostream>
#include <fstream>
using namespace std;
int main ()
{
// Deklarerar variabler.
ifstream firstn1;
ifstream firstn2;
ofstream correctorder;
double value1;
double value2;
// Öppnar filerna
firstn1.open("A");
firstn2.open("B");
correctorder.open ("C");
// If-satser som dubbelkollar att filerna öppnades korrekt.
if (!firstn1.is_open())
{
cout << "Fil 1 öppnades ej korrekt." << endl;
}
if (!firstn2.is_open())
{
cout << "Fil 2 öppnades ej korrekt." << endl;
}
// Läser in de första numrerna i variablerna.
firstn1 >> value1;
firstn2 >> value2;
// while-sats med nästlad if-sats som lägger in numrerna i rätt ordning
// och sedan lägger in ett nytt värde i variabeln.
while (!firstn1.eof() && !firstn2.eof())
{
if (value1 < value2)
{
correctorder << value1 << endl;
firstn1 >> value1;
}
else
{
correctorder << value2 << endl;
firstn2 >> value2;
}
}
// while-satser som lägger in resterande värden ifall ena filens
// nummer tagit slut.
while (!firstn1.eof())
{
correctorder << value1 << endl;
firstn1 >> value1;
}
while (!firstn2.eof())
{
correctorder << value2 << endl;
firstn2 >> value2;
}
// Stänger filerna som en extra säkerhetsåtgärd.
firstn1.close();
firstn2.close();
correctorder.close();
return 0;
}
|
Thanks
- Zorac