Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hello,
I wrote such a program and I want to move functions "sudeti" and "atimti" to different cpp file. Can you help me?
:
C++
#include <iostream>
int i,mas[5],n,suma,skirtumas,test;
char kodas;
int sudeti(int i, int mas[5], int n, int suma);
int atimti(int i, int mas[5], int n, int suma);

int sudeti()
{
    using namespace std;
    for (i = 0; i < n; i++)
    {
        cout << "Iveskite " << i+1 << "-aji skaiciu: " << endl;
        cin >> mas[i];
        suma = suma + mas[i];
    }
    cout << "******************************" << endl;
    return suma;
}
int atimti()
{
    using namespace std;
    for (i = 0; i < n; i++)
    {
        cout << "Iveskite " << i+1 << "-aji skaiciu: " << endl;
        cin >> mas[i];
    }
    cout << "******************************" << endl;
    skirtumas = mas[0] - mas[1] - mas[2] - mas[3] - mas[4];
    return skirtumas;
}
int main()
{
    using namespace std;

    cout << "Kiek skaiciu naudosite?" << endl;
    cin >> n;
    cout << "******************************" << endl;
    cout << "Iveskite norimo veiksmo koda:" << endl;
    cin >> kodas;
    cout << "******************************" << endl;
        switch (kodas)
    {
        case '+' :
         {
              cout << "Suma yra lygi: "<< sudeti();
         }
        break;
        case '-' :
        {
            cout << "Skirtumas yra lygus: " << atimti();
        }
        break;
        default : cout << "Tokio veiksmo nera" << endl;
    }

}
Posted
Comments
Thomas Daniels 17-Dec-12 12:25pm    
"Can you help me?"
I think that's not difficult.
Just create new files, and use Ctrl+X and Ctrl+V to move the methods.
Espen Harlinn 17-Dec-12 12:30pm    
That's more or less the correct answer :-D
KothasLT 17-Dec-12 12:46pm    
I get such error: too few arguments to function 'int sudeti(int, int*, int, int)'|

Create new text file (extension .cpp) and copy/paste the function code into them.

Create a header file (extension .h) for the prototype (copy the 2 prototypes into that file).

#include that header file in your original file ( where the main is).

Add all the necessary #include in your new cpp file.

What platform are you using ? Visual Studio ? gcc/g++ Linux ? something else.

You will have to add your new file to either the current project (in Visual Studio, add the file to the project).
On linux with gcc/g++ you might have to create or update your makefile (don't know how this work if you are using an IDE).

Good luck.

Max.
 
Share this answer
 
Comments
Thomas Daniels 17-Dec-12 12:38pm    
Yes, a 5!
You need to move the functions, something like the following:
1. Main header file : program.h
C++
#include <iostream>

int sudeti(int mas[5], int n, int suma);
int atimti(int mas[5], int n, int suma);
 
using namespace std;


2. sudeti.cpp
C++
#include "program.h"
 
int sudeti(int mas[5], int n, int suma)
{
    int i;

    for (i = 0; i < n; i++)
    {
        cout << "Iveskite " << i+1 << "-aji skaiciu: " << endl;
        cin >> mas[i];
        suma = suma + mas[i];
    }
    cout << "******************************" << endl;
    return suma;
}


3. atimti.cpp
C++
#include "program.h"
 
int atimti(int mas[5], int n, int suma)
{
    int i; 
    int skirtumas; 

    for (i = 0; i < n; i++)
    {
        cout << "Iveskite " << i+1 << "-aji skaiciu: " << endl;
        cin >> mas[i];
    }
    cout << "******************************" << endl;
    skirtumas = mas[0] - mas[1] - mas[2] - mas[3] - mas[4];
    return skirtumas;
}


4. main.cpp
C++
#include "program.h"

int main()
{
    char kodas;
    int mas[5];
    int n;
    int suma;

    cout << "Kiek skaiciu naudosite?" << endl;
    cin >> n;
    cout << "******************************" << endl;
    cout << "Iveskite norimo veiksmo koda:" << endl;
    cin >> kodas;
    cout << "******************************" << endl;
        switch (kodas)
    {
        case '+' :
         {
              cout << "Suma yra lygi: "<< sudeti(mas, n, suma);
         }
        break;
        case '-' :
        {
            cout << "Skirtumas yra lygus: " << atimti(mas, n, suma);
        }
        break;
        default : cout << "Tokio veiksmo nera" << endl;
    }
 
}

Note the changes to the calls to sudeti() and atimti() to include the actual parameters corresponding to the function signatures.
 
Share this answer
 
v4
Comments
KothasLT 17-Dec-12 12:57pm    
It seems good, but I still get some errors:
In header: too few arguments to function 'int sudeti(int, int*, int, int)' and in main.cpp: invalid conversion from 'int' to 'int*'|
Richard MacCutchan 17-Dec-12 13:02pm    
Sorry about that, I had not updated the header file correctly. Try it now.
KothasLT 17-Dec-12 13:05pm    
Closer and closer, but yet :D. Still 2 errors in main.cpp: C:\Users\Kothas\Documents\C++\Testai\main.cpp|20|undefined reference to `sudeti(int*, int, int)'|
Richard MacCutchan 17-Dec-12 13:21pm    
You missed something, probably in the header file, as I have just compiled this and it's OK. But, you need to change the declaration of suma to set it to zero.
KothasLT 18-Dec-12 2:05am    
Can you show me how? Sorry for stupid questions, but I'm learning C++ only for week :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900