Member Avatar for Aneurinbiker

Hi I'm learning C++ as a beginner through the book "C++ Primer by Stephen Prata". I have just learned a bit about creating my own functions and I'm now doing some practice at the end of the chapter. The problem is I'm meant to make output of:

"Three blind mice
Three blind mice
See how they run
See how they run"

Using my own functions, now so far the book has only taught me how to make functions with integers, I do no know what to make as a prototype at the top, and to call the functions where highlighted, could someone please advise me on what to do where red as a prototype if all I want is a function to display text?

// using 3 user defined functions

#include <iostream>
[B]tbm;
shr;[/B]
int main()
{
    using namespace std;
    cout << tbm << endl;
    cout << tbm << endl;
    cout << shr << endl;
    cout << shr << endl;
    
    system ("pause");
    
    return 0;
    
}

[B]tbm [/B]
{
     using namespace std;
     cout << "Three blind mice";
}

[B]shr[/B] 
{
     using namespace std;
     cout << "See how they run";
}
Member Avatar for Aneurinbiker

I also have a problem with this:

// light years to astronomical units conv

#include <iostream>
int lytas (int);
int main ()
{
    using namespace std;
    cout << "Enter the number of light years: ";
    long int ly;
    cin >> ly;
    int ay = lytas(ly);
    cout << ly << " light years = ";
    cout << ay << " astronomical units.";
    
    system ("pause");
    
    return 0;
    
}

int lytas (int sts)
{
    return sts * 63240.0;
}

Because after the prompt for a number, it enters and works fine...but it's still an integer value through the converter, not floating point so it's a bit off, and I was told that if I just put a floating point number in the division things would be fine but it obvs isn't.

Ty :)

In your first post you can do this :

void print(const char*p){
 cout << p << endl;
}

and call it like so :

int main(){
 print("hello");
 print("hello");

}

Try that first

Regarding your second post, the int data type can only hold integers, not floating point values. You should use a float or double to hold numbers with decimal places.

Member Avatar for Aneurinbiker

And how would I call and write a floating point function too? Thanks :)

Member Avatar for Aneurinbiker

In your first post you can do this :

So basically thats the actual function wherein I change p to whatever text I want... what do I do for a prototype? And how do I name the function? Sorry I basically just want to know how to make two functions to print these to the screen, I've never done these before...

Member Avatar for Aneurinbiker

Right I've changed it to this and it still doesn't work, I'm trying to work out what's wrong can someone please help and say exactly what's wrong as this is confusing for me, I only started the book yesterday :(

I reiterate: The output is meat to be

"Three blind mice
Three blind mice
See how they run
See how they run"

// using 3 user defined functions

#include <iostream>
void print(const char*p);
int main()
{
    using namespace std;
    print ("Three blind mice");
    print ("Three blind mice");
    print ("See how they run");
    print ("See how they run");
    
    system ("pause");
    
    return 0;
    
}

void print(const char*p)
{
      cout << p << endl;

You're missing a closing brace '}' at the end of the code snippet.

Member Avatar for Aneurinbiker

still doest compile :(

You would write and call a floating point function the same way you would an int except with the keyword float or double. for example:

#include <iostream>

// prototypes
double subtraction(double a, double b); 
float  addition(float x, float y);

int main()
{
   int number = 5;   

   double a = 5,   b = 5;
   float  x = 123, y = 456;

   // puts the return value of subtraction into the variable number
   number = subtraction(a, b);
   
   // shows the return value of additon to the console
   std::cout << "addition = " << addition(x, y) << "\n"
	         << "number = " << number << "\n\n";

   /*
      Because I haven't used using namespace std;
      I am manually adding it on to the things that need it
      like so 'std::'
   */   
   std::cout << "Press any key to continue...";
   std::cin.ignore();  // Use this instead of system("pause");

   return 0;
};

double subtraction(double a, double b)
{
    return a - b;
}
float addition(float x, float y)
{
    return x + y;
}

If the function has int at the front of its declaration then it must always return an int. The same applies for all the other types (that they must return their own data type) except void which returns nothing.

By the way your code wont compile because the cout and endl you are using in the function are not in the same scope as your 'using namespace std;' which is in main. Either add std:: to the beginning of cout and endl or declare using namespace std; globaly (at the top of the page).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.