what did I do wrong
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: what did I do wrong

  1. #1
    Join Date
    Mar 2014
    Posts
    2

    what did I do wrong

    Hello,

    Im self-studing c++ with C++ Primer book.
    Now I have this code:
    Code:
    // myfirst.cpp -- displays a message
    #include <iostream>
    
    int main()
    {
    using namespace std;
    zin1();
    return 0;
    }
    
    int zin1()
    {
    	cout >> "Three blind mouse";
    	return 0 ;
    }
    But now I see a message that zin1 in main and cout in zin1 function are not reconigzed.

    Can someone explain to me what I did wrong so I can learn from it.

    Roelof

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: what did I do wrong

    Move
    Code:
    using namespace std;
    up to be outside the main().
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: what did I do wrong

    Or you can move it to be near the top of the definition of zin1 instead.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,369

    Re: what did I do wrong

    Code:
    int main()
    {
        using namespace std;
        zin1();
        return 0;
    }
    You are calling the function zin1() before it has been defined and haven't provided a forward declaration for the function. Before main() you need
    Code:
    void zin1();
    'using' statements are usually placed in .cpp files after the '#include' statements (note they don't fo in .h header files).

    Another way of using cout without having to have a 'using' statement would be
    Code:
    std::cout >> "Three blind mouse";
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,369

    Re: what did I do wrong

    Your cout statement is also wrong. It should be
    Code:
    cout << "Three blind mouse";
    You are trying to extract from the cout stream rather then insert into it which is needed for output.

    For streams, << is output to (insertion), >> is get from (extraction).
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Mar 2014
    Posts
    2

    Re: what did I do wrong

    Thanks C++ is more difficult then I thought.
    But this one is solved.

    Roelof

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,369

    Re: what did I do wrong

    Im self-studing c++ with C++ Primer book.
    Doesn't the book explain this?

    You might also like to have a look at these web sites
    http://www.learncpp.com/
    http://www.cplusplus.com/doc/tutorial/

    Are you new to computer programming in general, or just to c++?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,882

    Re: what did I do wrong

    Quote Originally Posted by 2kaud View Post
    Doesn't the book explain this?
    I have unfortunately seen more than enough books and tutorials that do a pretty poor (or none at all) job at explaning the elementary "hello world" program. And it isn't until a next chapter that the stuff used in that elementary program are explained.
    I've even seen a C (wasn't C++) for beginners book that only had 1 sentence explaining the #include command. "This line of code includes the file mentioned". Chapter 2 of said book was about I/O to screen using the bios interrupt for TTY output. (printf() nowhere in the entire book). (yes, it was a pretty fail book overall)

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,369

    Re: what did I do wrong

    I haven't seen a copy of this particular book, but a review of it includes this comment

    The exercises in the first chapter are impossible with only the knowledge in the first chapter (knowledgeable programmers who I went to for help told me this). There are also no solutions provided. The authors clearly haven't written this to teach C++ to a beginner.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Mar 2014
    Location
    Warsaw, Poland
    Posts
    3

    Re: what did I do wrong

    I think, OP should dig a bit more while making these exercises - isn't programming about problem solving and curiosity?

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,882

    Re: what did I do wrong

    Quote Originally Posted by panqnik View Post
    I think, OP should dig a bit more while making these exercises - isn't programming about problem solving and curiosity?
    It should at least tell you what the bare minimum program is, and what the code in said bare minimum does as well as some elementary rules of the language.

    At least, if the book actually does have novice users in mind (which the title seems to suggest)

  12. #12
    Join Date
    Mar 2014
    Posts
    17

    Re: what did I do wrong

    the function zin1 needs to be defined before main function and using namespace std; needs to be written just after the header files you are using.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center