Advertisement

C++ Workshop - Getting Started with C++ (Ch. 1 & 2)

Started by June 01, 2006 11:40 AM
182 comments, last by Dbproguy 16 years, 3 months ago
Problem 1
#include <iostream>using namespace std;int main(){    cout << "Paul Adamski";    //cin.get(); If the console flashes}


Problem 2
#include <iostream>int main(){   std::cout << "There are many mistakes in this code." << std::endl;   std::cout << "Can you help me fix all the bugs in this code" << std::endl << std::endl;   std::cout << "Thank You!" << std::endl;}

Counted 13.

Problem 3
#include <iostream>using namespace std;int main(){    cout << "*******\n";    cout << " *****\n";    cout << "*******\n";    cout << " *****\n";    //cin.get(); If program flases}


Problem 4
#include <iostream>using namespace std;int main(){    int age;        cout << "Please enter your age: ";    cin >> age;    cout << "You are " << age << " years old.";    //cin.ignore(2); If program flases}


Problem 5
[source="cpp"]#include <iostream>using namespace std;int main(){    int iNumberOne;    int iNumberTwo;        cout << "Please give us a number: ";    cin >> iNumberOne;    cout << "Please give us one more number: ";    cin >> iNumberTwo;    cout << endl;        cout << "If you add those two numbers together you get " << iNumberOne + iNumberTwo << endl;    cout << "If you subtract those two numbers together you get " << iNumberOne - iNumberTwo << endl;    cout << "If you multiply those two numbers together you get " << iNumberOne * iNumberTwo << endl;    cout << "If you divide those two numbers together you get " << iNumberOne / iNumberTwo << endl;    cin.ignore(2); }


None of them were hard, hopefully they'll get harder in the next lesson. BTW: For Problem 5, you'd probably want to do a if statement to see which number is bigger, because usually your going to do the big one minus the smaller one.
Quote: Original post by AdamskiAirsoft
None of them were hard, hopefully they'll get harder.


Wait until we have some more bricks to build programs with. If you want complex pure C++ problems involving the darkest corners of the language, just ask. [evil]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Great workshop folks! those extra execises were pretty cool! Is the next chapter Doom? ;)

New to Gamedev, got the programming bug again, for how long who knows. A perfect place to brush up on my C++!

Thanks again for all the time you folks put in!
Problem #1
#include <iostream>using namespace std;int main(){	cout << "My name is ken." << endl;	return 0;}


Problem #2

#include <iostream>// the main function is using the wrong operator the '>>' should be '<<'int main(){   // cout is undefined, and is missing a semi-colon   cout >> "There are many mistakes in this code." >> std::endl    // cout, end, and endl are undefined   cout >> "Can you help me fix all the bugs in this code" >> end >> endl;   // cout is undefined   cout >> "Thank You!" >> std::endl;   // function has a return type and no return defined}// the correct way to write it#include <iostream>int main(){   std::cout << "There are many mistakes in this code." << std::endl;   std::cout << "Can you help me fix all the bugs in this code" << std::endl << std::endl;   std::cout << "Thank you!" << std::endl;   return 0;}


Problem #3
#include <iostream>using namespace std;int main(){    cout << "*******" << endl;    cout << " ***** " << endl;    cout << "*******" << endl;    cout << " ***** " << endl;    return 0;}


Problem #4
#include <iostream>using namespace std;int main(){    int age;    cout << "How old are you? : ";    cin >> age;    cout << "You are " << age << " years old" << endl;    return 0;}


Problem #5
#include <iostream>using namespace std;int main(){    int num1, num2;    cout << "Please enter the first number: ";    cin >> num1;    cout << "Please enter the second number: ";    cin >> num2;    cout << "The sum of the two numbers is: " << (num1 + num2) << endl;    cout << "The subtraction of the two numbers is: " << (num1 - num2) << endl;    cout << "The multiplication of the two numbers is: " << (num1 * num2) << endl;    return 0;}
I have read through the two chapters and I have done all the exercises, which was all pretty straight forward. I understand all the theory but I know from experiences with other languages that some things are not practical at all. So I would love to hear from the tutors what the practical use of 'using' is.

I can imagine that, when you're working in team with several others, and you have a big code base, it's error prone to use 'using' somewhere in the code. Especially when you use 'using namespace'.

So would it be best for me to teach myself to always prefix the namespaces?
Quote: Original post by RinusMaximus
So would it be best for me to teach myself to always prefix the namespaces?


You could do that, or you could use entire namespaces only in functions that require them. Namespaces are susepctible to all rules of scope:
int main(){   using namespace std;   //code}int function1(){   cout<<"Stuff";}

You will recieve a compilation error on line one of function1() because only main() has access to std; it was not made global at the top of the source file. You can use either of these methods, as giving a namespace global scope is considered bad practice and can lead to name clashes, as you said.
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
Advertisement
Quote: Original post by RinusMaximus
I can imagine that, when you're working in team with several others, and you have a big code base, it's error prone to use 'using' somewhere in the code. Especially when you use 'using namespace'.

So would it be best for me to teach myself to always prefix the namespaces?


I tend to use fully-qualified names (i.e. "prefix"), out of habit.

[warning]
On the other hand, you should always use fully-qualified names in header files. If you place a using declaration or directive in a header file, you essentially have taken the decision for anybody who uses that header to dump the contents of the namespace into the global namespace, whether they want it or not. Never do that.
[/warning]


On the other hand, there are coding techniques you can use if you don't, akin to using typedefs to switch between types, except more powerful:

#include <vector>#include <fruny/vector>namespace foo{   int a;   void b();   using std::vector;}namespace bar{   int a;   void b();   using fruny::vector;}// using namespace foousing namespace bar;int main(){   vector<int> vec;   a = 25;   b();}


It can get even more subtle with the use of namespace aliasing.

namespace mylib = foo;mylib::vector<int> vec;



[Extra Credit]
You could have a look at the beginning of SiCrane's article on the Standard C++ Library, he discusses namespaces in some detail.
[/Extra Credit]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I noticed in an earlier post that it was said std::endl flushes the buffer. Just to check I understand this; in layman's terms a buffer is simply an area of memory where some thingy that's been processed is sent to. There it waits, in the buffer, until it's called. If the buffer is flushed it means that once the thingy has left, the area of memory it occupied is freed for new operations. If it wasn't flushed, a copy of the thingy would still be there. Have I got that right?

One more thing. I remember the last time I tried to learn c++ I was confused by all the references to 'foo' and 'bar' while trawling the web. I thought they were more arcane keywords I didn't know until I realised they were just nonsense words which essentially meant 'any old crap you want here'. Sorry, they meant 'word standing in for highly erudite phrase that the c++ faq people would thoroughly approve of'. I write this just in case there's anyone as thick as me who didn't get that. Doubtful, I know.

simesf
Quote: Original post by simesf
I noticed in an earlier post that it was said std::endl flushes the buffer. Just to check I understand this; in layman's terms a buffer is simply an area of memory where some thingy that's been processed is sent to. There it waits, in the buffer, until it's called. If the buffer is flushed it means that once the thingy has left, the area of memory it occupied is freed for new operations. If it wasn't flushed, a copy of the thingy would still be there. Have I got that right?

Yes. "Flush" is a synonym for "empty to," with the destination of the flush being implied depending on the context of the operation.

Quote: One more thing. I remember the last time I tried to learn c++ I was confused by all the references to 'foo' and 'bar' while trawling the web. I thought they were more arcane keywords I didn't know until I realised they were just nonsense words which essentially meant 'any old crap you want here'. Sorry, they meant 'word standing in for highly erudite phrase that the c++ faq people would thoroughly approve of'. I write this just in case there's anyone as thick as me who didn't get that. Doubtful, I know.

Metasyntactic variables
I'm confused.

is iostream part of the standard C++ library?
is namespace std part of iostream?
is namespace std part of the standard C++ library?

This topic is closed to new replies.

Advertisement