Thread: Code written in Dev-C++ won't compile on linux

  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    Code written in Dev-C++ won't compile on linux

    The teacher gave us (in the course) the choice of either C or C++. I started with C and my friend with C++. I'm trying to help this friend with this code because she said it is not formatting okay. The students all do the exercises in Dev-C++ on windows. I'm the only one doing it on linux and vim. The problem is that when I try to compile the c++ code I get some errors. It runs fine in Dev-C++.

    $ g++ luciana_matriz.cpp -o luciana_matriz
    luciana_matriz.cpp: In function ‘int main()’:
    luciana_matriz.cpp:105:13: error: ‘else’ without a previous ‘if’
    luciana_matriz.cpp:142:12: error: ‘stdin’ was not declared in this scope
    luciana_matriz.cpp:142:17: error: ‘fflush’ was not declared in this scope
    luciana_matriz.cpp:143:13: error: ‘getchar’ was not declared in this scope

    Here's the code:
    Code:
    #include <iostream>
    #include <cstdlib>
    /* #include <cstring> */
    /* #include <unistd.h> */
    /* #include <cstdio.h> */
    /* #include <stdio.h> */
    
    
    using namespace std;
    
    main() {
        // exercicio 1:
        int l, c, TL, TC, num;
        cout<< "Informe o numero de linhas: \n";
        cin >> TL;
        cout<< "Informe o numero de de colunas: \n";
        cin>> TC;
        int M[TL][TC], N[TL][TC], Mm[TL][TC], S[TL][TC], A[TL][TC], M1[TL][TC], D[5][5], Id[4][4];
        srand(time(NULL));
    
        cout<< "\n M: \n" ;
        for(l=0;l<TL; l++) {
            for(c=0; c<TC; c++)
            {
                M[l][c]=rand()%500;
                cout<< M[l][c] <<"\t";
            }  
            cout <<"\n";
        }
    
        cout<< "\n N: \n" ;
        for(l=0;l<TL; l++) {
    
            for(c=0; c<TC; c++)
            {    
                N[l][c]=rand()%500;
                cout<< N[l][c] <<"\t";
    
            }
            cout <<"\n";
        }
    
        // exercicio 1 a:
    
        cout<< "\n\n a) Escolha um numero para multiplicar a matriz M:\t";
        cin>> num;
    
        cout<< "\n   M: \t\t\t\t   Mm: \n" ;
        for(l=0;l<TL; l++) {
            for(c=0; c<TC; c++) {
                cout << M[l][c]<<"\t"; 
            }
            cout<<"\t\t";
            for(c=0; c<TC; c++) {
                Mm[l][c]= num * M[l][c];    
                cout << Mm[l][c]<<"\t"; 
            }
            cout <<"\n\n";
    
        }    
    
        //exercicio 1 b:
        cout<< "\n\n b) Gerar Matriz S sendo a soma de M e N. \n" <<" Matriz S: \n";
    
        for(l=0;l<TL; l++) {
            for(c=0; c<TC; c++)
            {
                S[l][c]= M[l][c] + N[l][c];
                cout<< S[l][c]<< "\t";
            }
            cout <<"\n";
        }  
    
        // exercício 1 c:
        cout<< "\n\n c) Gerar Matriz A de qualquer dimensao formada pela lei A[L,C]=(3*L-2*C). \n" <<"Matriz A: \n";
    
        cout<< "\n A: \n" ;
        for(l=0;l<TL; l++) {
            for(c=0; c<TC; c++) {
                A[TL][TC]=(3*l - 2*c);
                cout<< A[TL][TC] <<"\t";
            }  
            cout <<"\n";
        } 
        //exercício 1 d
        cout<< "\n\n d) Mostrar a matriz lida e criar matriz M1,trasnposta da matriz M. \n" <<"Matriz M1: \n";
        cout<< "\n M1: \n" ;
        for(l=0;l<TL; l++) {
            for(c=0; c<TC; c++) {
                M1[l][c]= M[c][l];
                cout<< M1[l][c] <<"\t";
            }  
            cout <<"\n";
        }
        //exercicio 1 e  
            cout<< "\n\n d) Montar uma matriz diagonal. \n" <<"Matriz D: \n";
        cout<< "\n D: \n" ;
    
        for(l=0; l<5; l++) {
            for(c=0; c<5; c++) {
                if(l==c)
                    D[l][c]= '1';
                cout<<"\t %l" << D[l][c]<<"\n";
    
                else 
    
                    D[l][c]= '0';
                cout<< "\t %c" << D[l][c];
            }
        }   
    
        for(l=0; l<5; l++)
        {
            for(c=0; c<5; c++)
            {
                cout<<D[l][c]<< " \t ";
            }
            cout<< "\n";
        }                
        // exercício 1 f
            cout<< "\n\n f) Montar uma Matriz identidade. \n" <<"Matriz Id: \n";
    
        cout<< "\n Id: \n" ;
    
        for(l=0;l<4; l++)
        {
            for(c=0; c<4; c++) {
                if(l==c)
                    Id[l][c]=1;
    
                else
                    // if(l!=c)
                    Id[l][c]= 0;  
                cout<< "\t%c" <<'%' << Id[l][c] << "\t";
            }
            cout << "\n";
        }
        cout<< "\n";    
    
    
    
        fflush(stdin);
        getchar();
    }
    Any help would be appreciated.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Are you sure you didn't just copied the code in a wrong way?, because I doubt Dev-C++( using GCC ) compiles just fine with these diamonds! ( C++ is strongly-typed, meaning it isn't as torelant as C )

    EDIT: Seeing by the comments, obviously you didn't include the necessary headers.
    Last edited by GReaper; 11-25-2011 at 05:26 AM.
    Devoted my life to programming...

  3. #3
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by GReaper View Post
    Are you sure you didn't just copied the code in a wrong way?, because I doubt Dev-C++( using GCC ) compiles just fine with these diamonds! ( C++ is strongly-typed, meaning it isn't as torelant as C )
    She sent the source file to me by email. I guess she saved it from Dev-C++ to the hard disc before
    sending it to me. (I didn't copy).

    What do you mean by "these diamonds"?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by FernandoBasso View Post
    What do you mean by "these diamonds"?
    Well, obviously there is an error, as it says, that an else is used without an if. That happens as I see because there's a stray line in between. ( If you want to execute multiple lines in if statements you must use curly braces )

    All the others are because C++ is picky about declaration-prior-use. Include <cstdio> and you'll be fine. ( Although, I wouldn't recommend mixing C and C++ Input/Output )
    Devoted my life to programming...

  5. #5
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by GReaper View Post
    Well, obviously there is an error, as it says, that an else is used without an if. That happens as I see because there's a stray line in between. ( If you want to execute multiple lines in if statements you must use curly braces )
    All right. I hadn't noticed that. The teacher doesn't teaches us the language. She only "teaches" algorithms. And yes, she instructed the students that "we didn't need curly braces for if else if we wrote only one line after if or else. I got used to always use them, no matter how much lines I write (and I use autoclose.vim so that I don't forget to close a curly brace or a parenthesis or even a ".

    Quote Originally Posted by GReaper View Post
    All the others are because C++ is picky about declaration-prior-use. Include <cstdio> and you'll be fine. ( Although, I wouldn't recommend mixing C and C++ Input/Output )
    Well, I was not aware of that (and I bet the "teacher" doesn't either). I write a little bit php/sql already but found C much more interesting and enjoyable). I see the students doing all kinds of wrong things, like mixing indenting styles, different styles of where to put the opening curly brace after if or for. Well, bad practices. So, when the code just runs, even if it throws a lot of warnings, it is okay to the teacher and the students.

    I search for solutions in this forum, books and google. One of my goals is really learn good standards.

    Thanks for your help. Solved.

    By the way, can't we mark threads as solved in this forum?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Quote Originally Posted by GReaper View Post
    Are you sure you didn't just copied the code in a wrong way?, because I doubt Dev-C++( using GCC ) compiles just fine with these diamonds! ( C++ is strongly-typed, meaning it isn't as torelant as C )

    EDIT: Seeing by the comments, obviously you didn't include the necessary headers.
    Be careful here with your wording. In programming, strongly-typed has a very defined meaning and that isn't it. Strongly type means that the languages variables need to be assigned a type, either directly like when you state "int myvar;“ or inferred like when you say "var myVar=42;" as opposed to weak or duck typed languages ( as in, looks like a duck, quacks like a duck, its a duck )

    What I believe you meant to say in C++ sources are sensitive to character encoding.


    Sadly character encoding is platform dependant, although most will support UtF8 in addition to ascii

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Strongly type means that the languages variables need to be assigned a type, either directly like when you state "int myvar;“ or inferred like when you say "var myVar=42;" as opposed to weak or duck typed languages ( as in, looks like a duck, quacks like a duck, its a duck )
    (It seems to me..)Your definition does not fit into C++'s new features like auto, decltype etc.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Anyways, I said it because he didn't include the prototypes of the functions, and a strongly-typed language demands, among other, to declare everything before using them. So essentially you said the same thing but in a different way!
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Not true, auto is still strongly typed, its type is simply inferred by the rvalue. I would need to google to verify, as I've not used auto yet, but I assume it will throw an error if you violate the initial type it infers. So for example

    auto meaningOfLife=42.0f; // type inferred a float
    meaningOfLife = "Brian"; // blammo!

    In a weak typed language, the above would be perfectly fine, but in a strongly typed language like C# or C++, once the type is assign via inference, it cannot be changed.

    Now C#`s dynamic muddies the waters of strongly/weakly typed language divide, although at its core I believe dynamic still forces a type on the far, even if a obscenely base one like object.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Oddly I can't edit my post using the mobile interface.


    A key thing I should point out, and the biggest difference between dynamic and strongly typed languages is *when* type is determined.

    In strongly typed language, the inference happens as part of the compilation process. So if you assign x type a value it cannot support once x has been inferred by the compier, it will through an error. Behind the scenes, x is still assigned a type, its just the compiler that determines at, as I said earlier, generally using the rvalue.

    Dynamically typed languages however are resolved at run time, generally allowing variables to be mutable, something untrue of strongly typed languages. This a double edged sword, as it is extremely flexible and can result in some remarkably concise code, it also is slower and potentially dangerous as hell.

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Serapth View Post
    Not true, auto is still strongly typed, its type is simply inferred by the rvalue. I would need to google to verify, as I've not used auto yet, but I assume it will throw an error if you violate the initial type it infers. So for example

    auto meaningOfLife=42.0f; // type inferred a float
    meaningOfLife = "Brian"; // blammo!

    In a weak typed language, the above would be perfectly fine, but in a strongly typed language like C# or C++, once the type is assign via inference, it cannot be changed.
    Agreed..
    I meant your "( as in, looks like a duck, quacks like a duck, its a duck )" analogy.
    When you use auto... if the rvalue quacks like a duck.. the newly declared one will be a duck!

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Ah, good point, it does somewhat invalid the expression.

    I suppose "once a duck, always a duck" may take its place!

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Serapth View Post
    I suppose "once a duck, always a duck" may take its place!
    Generally...
    But in python (which claims to be strongly typed) ... you can delete the duck and make a rabbit with the same name inside the same scope.

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Quote Originally Posted by manasij7479 View Post
    Generally...
    But in python (which claims to be strongly typed) ... you can delete the duck and make a rabbit with the same name inside the same scope.
    Well Python takes the screwy cake prize for typing. I by no means pretend to be an expert on the subject, but I believe it goes something like this...


    someVar = 42
    someVar = "42"


    What happens here ( I believe ), is that a completely new string object is created and the label "someVar" is rebound to it, effectively replacing someVar's completely. Then the int version of someVar is released if its reference count is 0. However:

    someVar = 42
    someOtherVar = someVar;
    someVar = "42"
    someVar = 42

    Will cause the same action to change again, in that new int object is created and the "someVar" label or name is applied to the new object, and if the old string version has no external references, it is purged. In this case however, since someOtherVar refers to it, it is kept around, but no longer accessable as "someVar" or as anything other than the reference someOtherVar.

    To use your example, if you turned a variable "animal" from a duck object to a rabbit object, you actually are creating a new rabbit object and assigning the label "animal" to it. The type never actually changed behind the scenes, just the object referenced did.

    So, really, the type doesn't actually changed, thus Python remains a "strongly typed" langauge. And a screwy language.


    There is a reason I don't really seem to mesh well with Python after all. You are right though, it really does muddy the metaphor up even worse.
    Last edited by Serapth; 11-25-2011 at 01:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to compile Pro*C code on Linux..
    By anwar_pat in forum Linux Programming
    Replies: 2
    Last Post: 10-18-2006, 11:49 PM
  2. This code won't compile in Red Hat Linux 9
    By array in forum Linux Programming
    Replies: 7
    Last Post: 04-27-2004, 06:30 AM
  3. If C++ is king, why was the linux kernel written in C/ASM?
    By CompiledMonkey in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 03-05-2003, 05:24 PM
  4. Compile my code for Linux?
    By CompiledMonkey in forum Linux Programming
    Replies: 5
    Last Post: 11-17-2002, 02:02 AM
  5. Is Linux Source written in C or C++ ?
    By pritesh in forum Linux Programming
    Replies: 3
    Last Post: 12-02-2001, 06:21 AM