function inside function (most likely) problem

This is a discussion on function inside function (most likely) problem within the C++ Programming forums, part of the General Programming Boards category; As you may guess I'm pretty new to c++. I'm asked to write a program which would read and out ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Lithuania
    Posts
    4

    function inside function (most likely) problem

    As you may guess I'm pretty new to c++.
    I'm asked to write a program which would read and out put data files. Now im halfway through (that's why I'll ask you not to mind lots of what is on current code) but I'm stuck on this certain error for quite a bit already and from what I've read around it might be a function inside function problem which is not allowed in c++, yet I'm not able to see it and that's why I am asking for help out here.
    (I'm working with microsoft visual studio 2008 for that matter)

    Code:
    #include <iomanip>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std;
    //---------------------------------------------------------------
    const char CDfv[] = "studentai.txt";
    const char CRfv[] = "Rezultatai.txt";
    const int CMaksimalus = 400;
    //---------------------------------------------------------------
    class Studentas
    {
    private:
        string pavardė;
        string vardas;
        int įmetai;
        int epažymiai;
    public:
        Studentas();
        Studentas(string pav, string var, int įmet, int paž);
        ~Studentas();
        string DėtiPavardę();
        string DėtiVardą();
        int DėtiĮMetus();
        int DėtiPažymius();
        string ImtiPavardę();
        string ImtiVardą();
        int ImtiĮMetus();
        int ImtiPažymius();
    };
    //---------------------------------------------------------------
    // Pradinių duomenų skaitymas iš failo.
    void Duomenys (const char fv[], Studentas S[], int & n);
    // Rezultatų spausdinimas į failą.
    void Spausdinti (const char fv[], Studentas S[], int n);
    //---------------------------------------------------------------
    int main()
    {
        setlocale(LC_ALL, "Lithuanian");
        //ofstream fr(CRezultatųFailas); fr.close();
        Studentas S[CMaksimalus]; int n;
        Duomenys(CDfv, S, n);
        Spausdinti(CRfv, S, n);
        return 0;
    }
    //---------------------------------------------------------------
    Studentas::Studentas(string pav, string var, int įmet, int paž):pavardė(pav), vardas(var), įmetai(įmet), epažymiai(paž) { }
    Studentas::Studentas(): pavardė(""), vardas(""), įmetai(0), epažymiai(0) { }
    Studentas::~Studentas() { }
    //---------------------------------------------------------------
    string Studentas::ImtiPavardę()
    {
        return pavardė;
    }
    //---------------------------------------------------------------
    string Studentas::ImtiVardą()
    {
        return vardas;
    }
    //---------------------------------------------------------------
    int Studentas::ImtiĮMetus()
    {
        return įmetai;
    }
    //---------------------------------------------------------------
    int Studentas::ImtiPažymius()
    {
        return epažymiai;
    }
    //---------------------------------------------------------------
    string Studentas::DėtiPavardę()
    {
        return pavardė;
    }
    //---------------------------------------------------------------
    string Studentas::DėtiVardą()
    {
        return vardas;
    }
    //---------------------------------------------------------------
    int Studentas::DėtiĮMetus()
    {
        return įmetai;
    }
    //---------------------------------------------------------------
    int Studentas::DėtiPažymius()
    {
        return epažymiai;
    }
    //---------------------------------------------------------------
    void Duomenys (const char fv[], Studentas S[], int & n)
    {                                                             // 1st error here
        ifstream fd{fv};                                            // 2nd error here
        string pav, string var, int įmet, int paž;
        fd > n;
        for (int i = 0; i < n; i++) {
            fd >> pav >> var >> įmet >> paž;
            S[i] = Studentas(pav, var, įmet, paž);
        }
        fd.close();
    }
    //---------------------------------------------------------------
    void Spausdinti(const char fv[], Studentas S[], int n)
    {
      ofstream fr(fv);
      fr << " Pavardė      Vardas       Įstojimo į Universiteta        Kursas\n";
      fr << "                                    metai                      \n";
      fr << "-------------------------------------------------------------\n";
      for (int i = 0; i < n; i++)
        fr << left << setw(20) << G[i].ImtiPav()
                   << setw(15) << G[i].ImtiVardą()
           << "  " << fixed << right << setw(8)
           << setprecision(2) << S[i].ImtiĮMetus()
           << endl;
      fr << "-------------------------------------------------------------\n";
      fr.close();
    }
    Code:
    1>------ Build started: Project: L3_2, Configuration: Debug Win32 ------
    1>Compiling...
    1>L3_2.cpp
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(100) : error C2601: 'fd' : local function definitions are illegal
    1>        c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(99): this line contains a '{' which has not yet been matched
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(100) : error C2143: syntax error : missing ';' before '}'
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(101) : error C2146: syntax error : missing ';' before identifier 'var'
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(101) : error C2065: 'var' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(101) : error C2062: type 'int' unexpected
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(102) : error C2065: 'fd' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(102) : error C2065: 'n' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(103) : error C2143: syntax error : missing ')' before ';'
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(103) : error C2143: syntax error : missing ';' before ')'
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(103) : error C2143: syntax error : missing ';' before ')'
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(103) : error C2065: 'i' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(103) : error C2065: 'n' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(103) : error C2065: 'i' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(103) : error C2059: syntax error : ')'
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(104) : error C2146: syntax error : missing ';' before identifier 'fd'
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(104) : error C2065: 'fd' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(104) : error C2065: 'var' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(104) : error C2065: 'įmet' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(104) : error C2065: 'paž' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(105) : error C2065: 'S' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(105) : error C2065: 'i' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(105) : error C2065: 'var' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(105) : error C2065: 'įmet' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(105) : error C2065: 'paž' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(107) : error C2065: 'fd' : undeclared identifier
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(107) : error C2228: left of '.close' must have class/struct/union
    1>        type is ''unknown-type''
    1>c:\users\andriusa\desktop\ktu\programavimas\laborai\laboras 3\l3\l3\l3_2.cpp(108) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    1>Build log was saved at "file://c:\Users\andriusa\Desktop\KTU\PROGRAMAVIMAS\laborai\Laboras 3\L3_2\L3_2\Debug\BuildLog.htm"
    1>L3_2 - 27 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by vlaskiz; 10-16-2011 at 05:00 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > ifstream fd{fv}; // 2nd error here
    Probably because you used the wrong kind of braces.
    Perhaps

    fd(fv);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Lithuania
    Posts
    4
    Aight, everything is solved.
    Last edited by vlaskiz; 10-16-2011 at 06:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  2. Replies: 7
    Last Post: 04-11-2009, 09:44 AM
  3. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 09:01 AM
  4. problem with validating data inside function
    By DeathEvil in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 12:32 PM
  5. how to use a member function inside a function pointer?
    By mickey in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2002, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21