Reputation: 21
I have started to teach myself C++ via "C++ Primer Plus sixth edition." For the most part I understand the basics of everything which I have read so far. But when trying to do the exercises, I am start to draw blanks and second question myself. The first two exercises I did well, but I am in need of assistance on the third.
The exercise states:
Write a C++ program that uses three user-defined functions (counting main() as one) and produces the following:
Three blind mice.
Three blind mice.
See how they run.
See how they run.
My code is as follows:
#include <iostream>
int blind(int);
int run(int);
int main() {
using namespace std;
int n;
cin >> n;
cout << "Three blind mice." << n << endl;
int s;
cin >> s;
cout << "See how they run." << s << endl;
cin.get();
return 0;
}
int blind(int n) {
using namespace std;
return 2 * n;
}
int run(int s) {
using namespace std;
cout << "See how they run.";
return 2 * s;
}
I was hoping to have it built in such a way, that when I typed in a number it would reply with the message multiplied by the number I typed. But maybe that is too complex for what they are asking? And I do not have it built correctly either.
Anyways, if you could please help me out, I would be extremely grateful! ~P. Suedo
Upvotes: 0
Views: 1532
Reputation: 75
There are two websites which provide solutions to most of the programming exercises found in this book. The websites are: www.ignatkov.net & http://github.com. I think they are very useful in providing a method to check your solutions to the exercises alongside the 'stack overflow website'
Upvotes: 0
Reputation: 26878
I'm not entirely sure what you are asking. You pose that your expected output should be:
Three blind mice.
Three blind mice.
See how they run.
See how they run.
So the simplest program to get that output would be:
#include <iostream>
using namespace std;
void blind() {
cout << "Three blind mice." << endl;
}
void run() {
cout << "See how they run." << endl;
}
int main() {
blind();
blind();
run();
run();
return 0;
}
I don't mean to be asking more questions from an answer, but if there is more to what you are asking, please let me know. You don't need any cin
or anything like that. Additionally, considering your example, you can hoist up your call to using namespace std;
so that you don't have to include it in each function.
Also, unless your functions blind()
and run()
rely on some state that hasn't been defined yet, (which they don't in this case) you don't need to declare them before main()
and then define them below. You can just declare and define them at the top before main()
Upvotes: 0
Reputation: 83537
One possible function can be as simple as
void threeBlindMice() {
cout << "Three blind mice." << endl;
}
I leave it to you to figure out another function and how to use both functions from main.
Upvotes: 2