0

I'm really, really tired and seems like I cant handle this. I've got the deadline in about 1 hour and a half and I've ran into this error. The problem is that, before, I handled some errors like this. It says multiple definitions if cont and seems like nothing works. If someone could help me I would be so grateful. Thanks

main. cpp

    #include "banca.h"

using namespace std;


extern TranzactieBancara cont[100];
extern TranzactieBancara h;

int main()
{



    char nume[10],prenume[10];

    int optiune;
    int i;
    marcaj:

    cout<<"Numele: ";
    cin>>nume;
    cout<<"Prenumele: ";
    cin>>prenume;
     i = h.cautare(nume,prenume);// am implementat o functie de cautare pentru a afla ce cont sa caut


    cout<<"Apasati tasta 1 pentru creearea unui cont nou"<<endl;
    cout<<"Apasati tasta 2 pentru inchiderea contului"<<endl;
    cout<<"Apasati tasta 3 pentru depunerea unei sume de bani"<<endl;
    cout<<"Apasati tasta 4 pentru extragerea unei sume de bani"<<endl;
    cout<<"Apasati tasta 5 pentru transferarea unei sume de bani intr-un alt cont"<<endl;
    cout<<"Apasati tasta 6 pentru afisarea conturilor active"<<endl;
    cin>>optiune;

    switch(optiune)
    {
    case 1:
        h.Initializare(); // functia Initializare implementeaza un cont nou
        goto marcaj;


    case 2:
        if(cont[i].Verificare(i)==1) // functia Verificare(i) verifica daca parola data de utilizator este corespunzatoare contului
        {
            cont[i].InchidereCont(); //aceasta functie inchide contul
            goto marcaj;
        }
        break;
    case 3:
        if(cont[i].Verificare(i)==1)
         {
             cont[i].DepunereBani(i); //aceasta functie depune bani in contul utilizatorului
             goto marcaj;
         }
        break;
    case 4:
        if(cont[i].Verificare(i)==1)
        {
            cont[i].RetragereBani(i); // aceasta functie retrage bani din contul utilizatorului
            goto marcaj;
        }
    case 5:
        if(cont[i].Verificare(i)==1)
         {
            cont[i].TransferBani(i); // aceasta functie trasfera bani din contul utilizatorului intr-un alt cont dat de el
            goto marcaj;
         }
    case 6:
        cont[i].AfisareCont(i); // afiseaza detaliile contului corespunzator utilizatorului
        goto marcaj;
    default:
        cout<<"Optiune invalida, incercati din nou:"<<endl;
        goto marcaj;

    }



getch();

return 0;
}

banca.cpp

#include "banca.h"

#define n 10

extern TranzactieBancara cont[100];
extern TranzactieBancara h;


TranzactieBancara::TranzactieBancara()
{
   ifstream fisier1("fisier1.txt");
   if(!fisier1)
        cout<<"Eroare la deschiderea fisierului 1 !"<<endl;


   int i = 1;

   fisier1.seekg(0,ios::beg); // ne pozitionam la inceputul fisierului
   while(!fisier1.eof())
   {
       fisier1>>cont[i].nume;
       i++;
   }

   ifstream fisier2("fisier2.txt");
   if(!fisier2)
        cout<<"Eroare la deschiderea fisierului 2 !"<<endl;


   i=1;
   fisier2.seekg(0,ios::beg);
   while(!fisier2.eof())
    {
        fisier2>>cont[i].prenume;
        i++;
    }

   ifstream fisier3("fisier3.txt");
   if(!fisier3)
        cout<<"Eroare la deschiderea fisierului 3!"<<endl;

   i=1;
   fisier3.seekg(0,ios::beg);
   while(!fisier3.eof())
   {
       fisier3>>cont[i].parola;
       i++;
    }

   ifstream fisier4("fisier4.txt");
   if(!fisier4)
        cout<<"Eroare la deschiderea fisierului 4!"<<endl;

   i=1;
   fisier4.seekg(0,ios::beg);
   while(!fisier4.eof())
   {
       fisier4>>cont[i].sold;
       i++;
   }


}

int TranzactieBancara::cautare(char nume[10],char prenume[10])
{
    ifstream fisier1("fisier1.txt");
    ifstream fisier2("fisier2.txt");
    int i,k = 1;
    for( i = 0;i < n;i++)
    {
        if(strcmp(cont[i].nume,nume)==0)
           k=0;
        if(strcmp(cont[i].prenume,nume)==0)
           k=0;
        if(k==0)
           return i;
    }
}

int TranzactieBancara::Verificare(int i)
{
    char parola[20];
    cout<<"Introduceti parola contului aferent";
    for(int j=1;j<=6;j++)
        cin>>parola[i];

    if(strcmp(parola,cont[i].parola)==0)
        return 1;
    else
        return 0;
}

void TranzactieBancara::Initializare()
{
    ofstream f("sciere.txt");

    char nume[100],parola[20];
    cout<<"Introduceti numele dumneavoastra: ";
    cin>>nume;
    f<<nume;
    strcpy(cont[11].nume,nume);

    cont[11].sold=0;

    cout<<"Stabiliti-va parola: ";
    cin>>parola;
    f<<parola;

    strcpy(cont[11].parola, parola);

}

void TranzactieBancara::InchidereCont()
{
   /* delete nume;
    delete parola;
    delete sold*/
}

void TranzactieBancara::DepunereBani(int i)
{
    float suma;
    cout<<"Suma pe care doriti s-o depuneti: ";
    cin>>suma;

    cont[i].sold=cont[i].sold+suma;

    cout<<"In acest moment in cont aveti : "<<cont[i].sold<<" lei " ;
}

void TranzactieBancara::RetragereBani(int i)
{
    float suma;
    cout<<"Suma pe care doriti s-o retrageti: ";
    cin>>suma;

    if(suma>cont[i].sold)
        cout<<"EROARE. Depasire plafon curent";
    else
    {
        cont[i].sold=cont[i].sold-suma;
        cout<<"In acest moment in cont aveti: "<<cont[i].sold<<"lei";
    }
}

void TranzactieBancara::TransferBani(int i)
{
    float suma;
    char nume[10],prenume[10];
    cout<<"Numele celui caruia vreti sa-i transferati bani: ";
    cin>>nume;

    cout<<"Prenumele celui carui vreti sa-i transferati bani: ";
    cin>>prenume;
    cout<<"Introduceti suma pe care vreti s-o transferati";
    cin>>suma;

    if(suma>cont[i].sold)
        cout<<"Eroare, sold insuficient";
    else
        cont[i].sold = cont[i].sold - suma;

    for(int j=1;j<=n;i++)
        if(strcmp(cont[j].nume,nume)==0 && strcmp(cont[j].prenume,prenume)==0)
            cont[j].sold=cont[j].sold+suma;

}

void TranzactieBancara::AfisareCont(int i)
{
    cout<<"Nume: "<<cont[i].nume<<endl;
    cout<<"Prenume: "<<cont[i].prenume<<endl;
    cout<<"Sold curent"<<cont[i].sold<<endl;

}

banca. h

#include<iostream>
#include<string.h>
#include<conio.h>
#include<fstream>

#define n 100

using namespace std;


class TranzactieBancara{

    char parola[20];
    float sold;

public:
    char nume[10],prenume[10];

    TranzactieBancara();
    int Verificare(int i);
    void Initializare();
    void DepunereBani(int i);
    void RetragereBani(int i);
    void TransferBani(int i);
    void InchidereCont();
    void AfisareCont(int i);
    int cautare(char nume[10],char prenume[10]);

} ;
TranzactieBancara cont[100];
TranzactieBancara h;
101010
  • 41,839
  • 11
  • 94
  • 168
Gusti
  • 71
  • 2
  • 11
  • It's not the answer, but I would wrap the `banca.h` file with `#ifndef BANCA_H` `#define BANCA_H` at the top and `#endif` at the end – SomethingSomething Nov 16 '15 at 20:42
  • [This: `using namespace std;` has a great deal of pain causing potential.](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Using it in a header magnifies that pain. – user4581301 Nov 16 '15 at 22:17

4 Answers4

4

Never do this in a header file:

TranzactieBancara cont[100];

Put that in one of the .cpp files, then add this in the header:

extern TranzactieBancara cont[100];
Adam
  • 16,808
  • 7
  • 52
  • 98
3

That's easy enough. You have to declare variables wherever you need them. In the header file usually. You have to define the variable where it really exists, in a source file.

You have it backward. Your "extern" declaration lines should be in the header and ONE source file should have the actual array definition.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
3

When you have

TranzactieBancara cont[100];
TranzactieBancara h;

in a header file, all the .cpp files that #include the header file end up defining those variables.

Change them to declarations:

extern TranzactieBancara cont[100];
extern TranzactieBancara h;

and define them in one .cpp file.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

You have created an array "cont" in a header file and included the header file in two files: one is main.cpp and one more is banca.cpp

You need to use header guards.

For eg:

 #ifndef BANCA_H
 #define BANCA_H
-- you header file code ---
 #endif

EDIT: R Sahu's answer is more better way of solving this.

Sreekar
  • 995
  • 1
  • 10
  • 22