Thread: What can I do in c++, that I can't do in c?

  1. #31
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: If you get a compile error, it helps to post the error message you got.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #32
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I have to say, C being mathematical is debateable. People with maths degrees writing programs tend to write in python, matlab, things like this... don't let your own biases completely close off what you learn. Much of the point in the thread so far has been you would learn and do much more picking another language and forgetting how "bare metal" C is.

  3. #33
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by stahta01 View Post
    FYI: If you get a compile error, it helps to post the error message you got.

    Tim S.
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    
    //Simulate user input for testing purposes.
    int user_input(char endchar = '\n');
    
    int main()
    {
        int i;
        int i2;
        //  100-by-100 int's, each intialized to 0.
        vector<vector<int> > x(100, vector<int>(100,0));
        int rowsUp;
        int rowsAcross;
    
        for(;;) {
            printf("Rows move up? (0-100 or -1 to quit): ");
            rowsUp = user_input();  //scanf("%i\n", &rowsUp);
            if (rowsUp == -1) {
                printf("Bye.\n");
                break;
            }
    
            printf("Rows to move across?        (0-100): ");
            for( i = 0; i <= rowsUp; i++)
            if(rowsUp >= 0 && rowsUp <= 100)
            {
                printf("up { ");
                for(i = 0; i <= rowsUp; i++)
                {
                    x.at(i-1).at(1) = i;
                    printf("%i ", x.at(i-1).at(1));
                }
                
                printf("}\n");
                printf("You moved %i ", x.at(1).at(i2 - 2));
                printf("rows across.\n");
            }
        }
        exit(0);
    }
    
    int user_input(char endchar /*= '\n'*/)
    {
        static vector<int> test_input = {
            0,0, //rowsup, 0 rows across.
            1,1, //1 row up, 1 row across.
            2,2,
            19,45,
            100, 100, //maximum valid values.
            101, 100,
            101, 101,
            -1, //QUIT
        };
        static size_t next = 0;
    
        if (next == test_input.size()) {
            printf("%s: No more test input ! Program will abort. \n", "user_input")
            exit(1);
        }
    
        int v=test_input.at(next++);
        printf("%d", v);
        if (endchar != '\0') printf("%c", endchar);
        return v;
    }
    
    
    Output:
    
    In function 'int user_input(char)':
    Line 56: error: scalar object 'test_input' requires one element in initializer
    compilation terminated due to -Wfatal-errors.

  4. #34
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by whiteflags View Post
    I have to say, C being mathematical is debateable. People with maths degrees writing programs tend to write in python, matlab, things like this... don't let your own biases completely close off what you learn. Much of the point in the thread so far has been you would learn and do much more picking another language and forgetting how "bare metal" C is.
    It's not that I've never learned languages other than c. I have learned c++, c#, Visual Basic and vb.net, javascript, Java, and SQL. But I believe my educational background is best suited to allow me write in c at the moment. And I don't have a math degree, I actually have 2 business degrees. But I have studied tremendous amounts of mathematics, particularly game theory, in which I put in close to 2000 hours of research. I also studied and coded in c/c++/c#, putting in over 1000 hours before going to college.

    Now that I'd like to be a professional programmer again, I would like to start off by mastering c again. While I realize c is bare metal, in terms of how you have to use it, I would still like to master c before moving to another language. Further, if I wanted to study other languages, such as python, I would probably be best off taking some kind of coding bootcamp that specializes in teaching the currently hot languages in today's job market. This is because there will always be boot camps that will teach students languages that are currently popular in the job market.

  5. #35
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I got something completely different when I compiled your code?
    Code:
    prog.cpp: In function 'int user_input(char)':
    prog.cpp:61:9: error: expected ';' before 'exit'
             exit(1);
             ^
    Try looking at the line above this one.

    Did you compile with the option to use C++14? It is not used by default in many compilers or online compilers either.
    Last edited by whiteflags; 10-16-2016 at 04:39 PM.

  6. #36
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287

    Did you compile with the option to use C++14? It is not used by default in many compilers or online compilers either.
    I compiled the code with an online compiler.

  7. #37
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Aside from the missing semicolon:
    Code:
    $ g++ --std=c++11 --pedantic -Wall -Wextra -Wfatal-errors xx.cpp
    xx.cpp: In function 'int main()':
    xx.cpp:16:9: warning: unused variable 'rowsAcross' [-Wunused-variable]
         int rowsAcross;
             ^
    xx.cpp:38:51: warning: 'i2' may be used uninitialized in this function [-Wmaybe-uninitialized]
                 printf("You moved %i ", x.at(1).at(i2 - 2));
                                                       ^
    C++11 mode is enough to compile that.

  8. #38
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Terrance View Post
    Code:
    In function 'int user_input(char)':
    Line 56: error: scalar object 'test_input' requires one element in initializer
    compilation terminated due to -Wfatal-errors.
    This error is probably caused by compiling in C++98 mode. For the user_input function to work in C++98 mode, an initializer list cannot be used. Instead you could initialize the test_input vector in the following C++98 way:

    Code:
    static const int arr[] = {
            0, 0, // 0 rows up, 0 rows across.
            1, 1, // 1 row up, 1 row across.
            2, 2,
            19, 45,
            100, 100, // Maximum valid values.
            101, 100, 
            101, 101, 
            -1,     // Quit.        
        };
        const vector<int> test_input(arr,arr+NELEMS(arr));
    NELEMS is a convenient macro for such situations and is defined as follows

    Code:
    #define NELEMS(arr) (sizeof(arr)/sizeof(*arr))
    prog.cpp: In function 'int user_input(char)':
    prog.cpp:61:9: error: expected ';' before 'exit'
    exit(1);
    ^
    This error is caused by a missing semicolon on the previous line (looks to be a typo).

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Terrance
    Why c is so mathematically intuitive, is because it works at such a low level, it practically allows you to manipulate a computer at the assembler level.
    You should be aware that due to optimisations applied by modern compilers even at the lowest optimisation levels, this is somewhat of an illusion unless you actually do dip into assembly.

    Quote Originally Posted by Terrance
    But I feel I will have to relearn c++/c# again in the future, but I'd prefer to stick with and master c first, then I can consider writing code in c++ afterwards.

    (...)

    It's not that I've never learned languages other than c. I have learned c++, c#, Visual Basic and vb.net, javascript, Java, and SQL. But I believe my educational background is best suited to allow me write in c at the moment. And I don't have a math degree, I actually have 2 business degrees. But I have studied tremendous amounts of mathematics, particularly game theory, in which I put in close to 2000 hours of research. I also studied and coded in c/c++/c#, putting in over 1000 hours before going to college.

    Now that I'd like to be a professional programmer again, I would like to start off by mastering c again. While I realize c is bare metal, in terms of how you have to use it, I would still like to master c before moving to another language. Further, if I wanted to study other languages, such as python, I would probably be best off taking some kind of coding bootcamp that specializes in teaching the currently hot languages in today's job market. This is because there will always be boot camps that will teach students languages that are currently popular in the job market.
    It seems to me that you made up your mind before starting this topic, and no amount of reasoning is going to persuade you otherwise because you are utterly convinced even before any argument has been made that C is most suited to your particular mathematical background. Hence, stop wasting your time trying to justify yourself on the C++ programming forum as to why you would choose to (re)learn C; focus on learning/mastering C instead. After that, if you would like to (re)learn other programming languages, perhaps even C++, go ahead. In the end, once you're back in the game with C, you'll have re-established the programming background to more easily learn other programming languages.
    Last edited by laserlight; 10-17-2016 at 12:17 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #40
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Terrance View Post
    But I'd rather prefer to learn complex languages that make better use of my mathematical background. Again, I'd prefer to stay away from Matlab or haskell
    Why don't you consider Matlab as a complex language that makes better use of mathematical background? Another complex mathematical language called APL (A Programming Language) dates back to the 1960's. Here's an example of Conway's somewhat complex game of life implemented with a small amount of code in APL:

    Conway's Game Of Life in APL - YouTube

    Quote Originally Posted by Terrance View Post
    the c language is designed as an allocation memory language, since it works at such a low level and allows you to manipulate a computer much like assembler language does.
    Not quite, there aren't many languages that include support for some of the SIMD / SSD instructions. Here are a pair of links, the first describes a fast CRC algorithm, the second is assembly code of an enhanced version from Intel that "folds" 128 bytes at a time. Note - the comment in the assembly code for rk3 and rk4 are wrong, the comments should be rk3 == (2 ^(32*31)) mod q, and rk4 == (2 ^(32*33)) mod q. I converted the assembly to Microsoft Masm / ML syntax to work with Visual Studio, to confirm that the code works.

    http://www.intel.com/content/dam/www...lqdq-paper.pdf

    isa-l/crc16_t10dif_01.asm at master * 01org/isa-l * GitHub
    Last edited by rcgldr; 10-17-2016 at 01:25 AM.

  11. #41
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    I don't have a mathematics degree, so my background in mathematics is specialized around game theory, which goes deeply into matrix algebra, and Matlab does not necessarily suit me well in terms of my mathematical background. Also, I have never learned Matlab in the past, so it would take me considerably longer to learn Matlab over c. Further, I have already learned c in the past and it suits my mathematical training well.
    Last edited by Terrance; 10-17-2016 at 01:55 AM.

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It sounds like you're pretty much dead set on C, so why bother asking about other languages?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    If I showed you some linear algebra that I wrote in C++, would it help change your mind? I've been thinking of ways to improve the interface of the classes and functions as well.

    Also, someone's had to have mentioned it by now but there _is_ a language literally based off of category theory.

  14. #44
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    I'd probably be the least useful person to help with improving your code, but I'd be interested to see it, MutantJohn.

    (I've mentioned Haskell, which you may have in mind... OP isn't interested. Somehow.)
    Last edited by Xupicor; 10-17-2016 at 10:46 AM.

  15. #45
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Oh awesome. I'll post some code later tonight when I have time to explain my problem domain and my methodology.

    And yes, I was referring to Haskell. I'm glad you mentioned it then! I kind of skimmed the thread lol but I got the gist. The thing is, C and C++ map naturally to scientific math, imo. To me, pure math is more what Haskell attempts to do with its roots in category theory.

Popular pages Recent additions subscribe to a feed

Tags for this Thread