Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hello,
I wrote such a program and I want to move functions "sudeti" and "atimti" to different cpp file. Can you help me?
:
#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 17-Dec-12 6:23am
Comments
ProgramFOX at 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 at 17-Dec-12 12:30pm
   
That's more or less the correct answer :-D
KothasLT at 17-Dec-12 12:46pm
   
I get such error: too few arguments to function 'int sudeti(int, int*, int, int)'|
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
ProgramFOX at 17-Dec-12 12:38pm
   
Yes, a 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to move the functions, something like the following:
1. Main header file : program.h
#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
#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
#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
#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.
  Permalink  
v4
Comments
KothasLT at 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 at 17-Dec-12 13:02pm
   
Sorry about that, I had not updated the header file correctly. Try it now.
KothasLT at 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 at 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 at 18-Dec-12 2:05am
   
Can you show me how? Sorry for stupid questions, but I'm learning C++ only for week :/
KothasLT at 18-Dec-12 2:54am
   
Fixed it. I use Codeblocks and forgot to #include in cpp files :D. Thank you for all help! :)

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



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100