Beginner Exercises

Pages: 1... 1011121314... 16
closed account (jwC5fSEw)
He does state somewhere that scoping the using declarations is better than putting outside of everywhere.


Yeah, he does, which I why I originally conceded to computerquip. Then he deleted his post and reposted something that applies to more than just proper namespace usage, so I changed my post to reflect that.

My original post was made because I'd misread that particular paragraph in TC++PL, so my original point was wrong.
I'm with computerquip. I never use using namespace std;.
closed account (S6k9GNh0)
I got rid of my original post because I couldn't explain what I wanted to say and ended up being blunt instead of sugar coating everything.
So i use std:: namespace instead?

Also, I completed the beverages one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
int main()
{
    int a;
    cout << "1 for coke.2 for sprite,3 for fanta, 4 for water. 0 to quit"<<endl;
    cin>>a;
    while (a !=0)
    {
          if (a > 4)
          {
                cout <<"That was not a valid option.";
                cout<<"Would you like to choose again?"<<endl;
                cin>>a;
          }
          
          if (a == 1)
          {
             cout<<"You chose coke"<<endl;
             cout<<"Would you like to choose again?"<<endl;
             cin>>a;
          }
          if (a == 2)
          {
             cout <<"You chose sprite.";
             cout<<"Would you like to choose again?"<<endl;
             cin>>a;
          }
          if (a ==3)
          {
                cout<<"You chose Fanta.";
                cout<<"Would you like to choose again?"<<endl;
                cin>>a;
          }
          if (a==4)
          {
                 cout<<"You chose water.";
                 cout<<"Would you like to choose again?"<<endl;
                 cin>>a;
          }
              
    }
    cout<<"Thanks for coming??";
    cin.get();
    return 0;
}
    


And yes, I used namespace std;I tried to use the other one, but I kept getting an error. Would someone care to tell me what's wrong with globally declaring the namespace?
Last edited on
Just wait until you want to declare a global symbol called 'max', or 'sort', or 'complex'. Then, you'll maybe learn, but most likely you'll just rename it and go on using the using keyword. That is, until you have around four different namespaces in global scope and will be damned to patiently resolve the hundreds of compiler errors you'll inevitably get.


PS: Nobody dare infer this has happened to me.


Evolution of a C++ Programmer


1. Learned basic C++ from an inadequate tutorial.
1
2
3
4
5
6
7
#include <iostream.h>

using namespace std; // thanks, joe

void main(int argc, char** argv) {
	cout << "Hello, world!" << endl;
}


2. Learned basic C++ from a slightly better tutorial.
1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
	cout << "Hello, world!" << endl;
	return 0;
}


3. Learned about namespace pollution.
1
2
3
4
5
6
#include <iostream>

int main(int argc, char* argv[]) {
	std::cout << "Hello, world!" << std::endl;
	return 0;
}


4. Learned about classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string.h>

class Hello {
private:
	char* msg;
public:
	Hello() {
		this->msg = strdup("Hello, world!");
	}
	~Hello() {
		delete this->msg;
	}
	char* hello() {
		return this->msg;
	}
};

int main(int argc, char* argv[]) {
	Hello* hello = new Hello();
	std::cout << hello->hello() << std::endl;
	delete hello;
	return 0;
}


5. Learned about the C++ string container.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

class Hello {
private:
	std::string msg;
public:
	Hello() {
		this->msg = "Hello, world!";
	}
	std::string hello() {
		return this->msg;
	}
};

int main(int argc, char* argv[]) {
	Hello hello;
	std::cout << hello.hello() << std::endl;
	return 0;
}


6. Found out about the STL.

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/bits/ios_base.h:783: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/iosfwd:52: error: within this context
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/iosfwd: In copy constructor ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/iosfwd:61: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here
test.cpp: In function ‘int main(int, char**)’:
test.cpp:7: note: synthesized method ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’ first required here
test.cpp:7: error: initializing argument 3 of ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Funct = std::basic_ostream<char, std::char_traits<char> >]’
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/bits/stl_algo.h: In function ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Funct = std::basic_ostream<char, std::char_traits<char> >]’:
test.cpp:7: instantiated from here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.1/../../../../include/c++/4.3.1/bits/stl_algo.h:3791: error: no match for call to ‘(std::basic_ostream<char, std::char_traits<char> >) (char&)’


7. Mastered the basic containers of the STL.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>

int main(int argc, char* argv[]) {
	std::string hello = "Hello, world!";
	std::vector<char> v;
	for (unsigned int j = 0; j < hello.length(); ++j) {
		v.push_back(hello[j]);
	}
	std::vector<char>::iterator i;
	for (i = v.begin(); i != v.end(); ++i) {
		std::cout << *i;
	}
	std::cout << std::endl;
	return 0;
}


8. Mastered the STL.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <algorithm>

class printc { public: void operator() (char c) { std::cout << c; } };

int main(int argc, char* argv[]) {
	std::string hello = "Hello, world!";
	std::for_each(hello.begin(), hello.end(), printc());
	std::cout << std::endl;
	return 0;
}


9. Read the C++ FAQ Lite and realized C++ mastery is impossible.
1
2
3
4
5
6
#include <iostream>

int main(int argc, char* argv[]) {
	std::cout << "Hello, world!" << std::endl;
	return 0;
}


10. Read the C++ FQA Lite.
1
2
3
4
5
6
#include <stdio.h>

int main(int argc, char* argv[]) {
	printf("Hello, world!\n");
	return 0;
}


(There's more, but after this it gets stupid and irrelevant. Plus, ten is a nice round number.)
Last edited on
I also started using stdio.h in C++ a while ago because I was told it was faster.
Also you spelt FAQ as FQA on number 10.
Edit: also you tried to use an HTML tag on number 8.
Last edited on
Also you spelt FAQ as FQA on number 10.
Ohoho. You have a lot to learn, little grasshopper.
chrisname wrote:
I also started using stdio.h in C++ a while ago because I was told it was faster.


It is. Everyone knows that printf stands for print fast, and fprintf stands for faster print fast.
@helios,
orly.jpg
http://i39.tinypic.com/akbk9y.png
edit: also http://i44.tinypic.com/2lsb0c6.png
@firedraco,
Oh yeah. I remember now...
Last edited on
Now try Googling the term itself instead of its definition.
I guessed it might be "Frequent Questions and Answers" or something. "Fequently Questioned answers" though?
closed account (S6k9GNh0)
http://codepad.org/SEGRGA0c

Latest Graduation program. Run completely however, it runs for about 10 seconds on my computer since it fails to kill them off when they get to high in numbers and outputs well over a megabyte of text.

If you're looking at the code, ignore the OutputManager for now. I'm still implementing it (correctly) and I'll finish it today since its the actual reason I started working on the program.
Um, what's up with some of the stuff like:

age = rand() % (10 - 1 + 1) + 1; // 1 - 10 int tmp = rand() % (100 - 1 + 1) + 1; //1 - 100

Wait...wtf??

name = firstNames[rand() % (firstNames.size() - 0 + 0) + 0];????

Don't need to close ever really, streams will close when they are destructed.

//Don't complicate, just pass the whole mother.
What if I just want to make a certain color bunny?

Why do you need va_args? Just pass a vector (or just add the streams later with an addStream function into a vector, so that the user can add streams whenever)

There were probably some other weird things in there I missed...
closed account (S6k9GNh0)
1) @ rand() equation: I don't know. If I change it to some other shorter form, it doesn't work and isn't random at all. I found it rather strange and I couldn't figure out why but I'm leaving it as is for now until I figure out a more accurate equation or until I probably get rid of rand() entirely for randomness fun :D. I just wanted something that worked here lol.

2) You can't make a bunny a certain color. Its in the challenge description. It's either random at the beginning and they always inherit the mothers color.

3) In the Game constructor: Game(std::ostream& _out = std::cout), you have to choose the streams you wish to output to. The comment was made just to remind me to make some solution to pass multiple streams.
Last edited on
I like the comment on line 40.
However, I'm not sure what to think about the comments on lines 36 and 37.
The comment on line 43 frightens me.

I don't like the C++ FQA site. Its purpose is to indoctrinate hatred of C++, and uses outlandish arguments. You can't kill an abstract object with hate. Further, the author of the site is so fond of logical fallacies I stopped perusing it. One thing is always true: personal bias trumps all reason and discourse.
@Duoas,
static std::vector<std::string> firstNames; //List of bunny names. Static to prevent huge memory usage ( one instance ).
This? Yes, static doesn't do that in classes. In a class, static makes the variable accessible from outside the class without using an object (that's why std::string::npos works)

the comments on lines 36 and 37.
1
2
	int color; //Male / Female
	int gender;   //White / Brown / Black / Spotted 

Lol!

I don't like the C++ FQA site. Its purpose is to indoctrinate hatred of C++, and uses outlandish arguments.

It has some good arguments, but otherwise, I agree with you.

You can't kill an abstract object with hate.

Hate the FQA harder! It might go away!
bRadioactiveMutantVampireBunny?
WTF!?!
EDIT: That was a joke. See later post for details.

The FQA website does fail to list exactly one language we should use instead of C++.
EDIT: Python? It's so much slower, although it is less forgiving for smaller jobs.

-Albatross
Last edited on
Did you read the topic?
The assignment calls for Radioactive Mutant Vampire Bunnies...
http://www.cplusplus.com/forum/articles/12974/
(Scroll down to the Graduation exercise.)

The FQA author likes Python.
Pages: 1... 1011121314... 16