Thread: Very New to C++

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Inverness, Florida, United States
    Posts
    2

    Very New to C++

    Hello I am very very new to C++
    I am currently reading a book called
    "Sams teach yourself C++ in 24 hours"
    And now I am stuck on like, the second program you are told to program.

    It's called the "calculator program"

    Code:
    #include <iostream>
    
    int add(int x, int y)
    {
        //add the numbers x and y together and return the sum
          std::cout << "Running calculator ...\n";
          return (x+y);
    }
    
    
    int main()
    {
           /* this program calls an add() function to add two different
           sets of numbers togethor and display the results. The
           add() function doesn't do anything unless it is called by
           a line in the main() function. */
        std::cout << "what is 867 + 5309?\n";
        std::cout << "the sum is " << add(867, 5309) << "\n\n";
        std::cout << "what is 777 + 9311?\n";
        std::cout << "the sum is " << add(777, 9311) << "\n\n";
        return 0;
    }


    The purpose of the lesson was to teach you about functions and writing comments in your code.

    So i write as it has it in the book and try to compile and run it.

    But my program, (DEC C++) says there is a syntax error before the ":" on line 6

    and i'm just so very confused, I barely understand what the book has to teach me already. And now an unknown error. All very disconcerting.
    I really reallly want to learn how to write in C++ but yeah, i'm so lost.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    There is no syntax error in the code you posted.

    I barely understand what the book has to teach me already.
    Let me lay it down for you: you can't learn any programming language in "24 hours", "7 days", "28 days", "30 days", or any such variation.

    It takes time and effort to learn a new thing.

    That said, the book is terrible. (I advise you to invest in "Accelerated C++".)

    However, you deserve part of the blame. You are obviously moving too fast for yourself.

    Slow down, reread the earlier bits, and above all else practice a lot.

    Soma

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by phantomotap View Post
    There is no syntax error in the code you posted.
    is it possible that this "DEC C++" compiler is so old that it predates the standard? if this is an alpha or a vax, it's entirely possible that the C++ compiler on this platform knows nothing of namespaces.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Let me lay it down for you: you can't learn any programming language in "24 hours", "7 days", "28 days", "30 days", or any such variation.
    Except for this one -> Teach Yourself Programming in Ten Years

    > But my program, (DEC C++) says there is a syntax error before the ":" on line 6
    Did you manage to make this compile and run?
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World\n";
        return 0;
    }
    If you did, I think it's worth trying to rename your 'add' function to be something a little less generic like 'addTwoNumbers'

    > But my program, (DEC C++) says there is a syntax error before the ":" on line 6
    Now, is this a typo for DEV-C++, or are you really programming on some old big-iron from Digital Equipment Corporation
    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.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Inverness, Florida, United States
    Posts
    2
    Yes, that was a typo, I'm using DEV-C++
    And I got the program to run, but now it runs, stays open for a whole second, and then automatically closes

    I'm trying to put in a

    cin.get();

    to keep it open, but every time i try it responds with

    `cin' undeclared (first use this function)

    And now im confused
    again

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    cin.get();

    to keep it open, but every time i try it responds with

    `cin' undeclared (first use this function)
    Notice how you put std:: in front of your cout calls.

    Try std::cin
    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.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I ran your program on Visual Studio 2012 and it went fine, so I'm guessing your compiler might be a bit outdated or something.
    I suggest downloading a new IDE. Alot of them are free
    Your code works just fine.
    Last edited by Salem; 03-04-2013 at 02:07 AM. Reason: snipped illegal suggestion

Popular pages Recent additions subscribe to a feed

Tags for this Thread