-
March 6th, 2014, 10:53 AM
#1
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
-
March 6th, 2014, 10:56 AM
#2
Re: what did I do wrong
Move
Code:
using namespace std;
up to be outside the main().
Victor Nijegorodov
-
March 6th, 2014, 10:58 AM
#3
Re: what did I do wrong
Or you can move it to be near the top of the definition of zin1 instead.
-
March 6th, 2014, 11:17 AM
#4
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
'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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 6th, 2014, 11:50 AM
#5
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 6th, 2014, 03:47 PM
#6
Re: what did I do wrong
Thanks C++ is more difficult then I thought.
But this one is solved.
Roelof
-
March 6th, 2014, 04:11 PM
#7
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 7th, 2014, 07:56 AM
#8
Re: what did I do wrong
Originally Posted by 2kaud
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)
-
March 7th, 2014, 10:26 AM
#9
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 9th, 2014, 04:27 PM
#10
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?
-
March 10th, 2014, 07:55 AM
#11
Re: what did I do wrong
Originally Posted by panqnik
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)
-
March 20th, 2014, 10:46 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|