Problem Learning

Posts 16–30 of 41 · Page 2 of 3
heres the problem:
double Feet;
and
double Furlong = Ft2Frlng(Feet);

What type is the param of Ft2Frlng? and what are you passing to it?
Quote Originally Posted by Hell_Demon View Post
heres the problem:
double Feet;
and
double Furlong = Ft2Frlng(Feet);

What type is the param of Ft2Frlng? and what are you passing to it?
Yup. I figured it out. =) I changed the Param of Ft2Furlong(int) to Ft2Furlong(double) and Ft2Furlong(double Feet). Now i'm struggling to compile a program that has 2 functions in it that prints
Code:
Three Blind Mice.
Three Blind Mice.
See How They run.
See How They Run.
I recon I use void functionname(void) but I don't now what I would return? But if I have to return Three Blind Mice, wouldn't I need a return of some sort? Would it be String? char? What?
Quote Originally Posted by ilovecookies View Post
Yup. I figured it out. =) I changed the Param of Ft2Furlong(int) to Ft2Furlong(double) and Ft2Furlong(double Feet). Now i'm struggling to compile a program that has 2 functions in it that prints
Code:
Three Blind Mice.
Three Blind Mice.
See How They run.
See How They Run.
I recon I use void functionname(void) but I don't now what I would return? But if I have to return Three Blind Mice, wouldn't I need a return of some sort? Would it be String? char? What?
A void does not return anything.
either use return; at the end of your void or nothing at all(both are fine).
This is where i'm at right now HD

Code:
#include <iostream>

char ThrBlMi();
char CHwThyRn();

int main()
{
    using namespace std;
    cout << ThrBlMi() << endl;
    cout << ThrBlMi() << endl;
    cout << CHwThyRn() << endl;
    cout << CHwThyRn() << endl;
    system("pause");
    return 0;
}

char ThrBlMi()
{
      return "Three Blind Mice.";
}

char CHwThyRn()
{
       return "See How They Run.";
}

I get errors and I know why, but I don't know how to fix it. It's because i'm using char as the function header. But I don't know what I could use. I tried string and it said string was invalid. What does my header need to be to get the return i'm looking for?
Quote Originally Posted by ilovecookies View Post
This is where i'm at right now HD

Code:
#include <iostream>

char ThrBlMi();
char CHwThyRn();

int main()
{
    using namespace std;
    cout << ThrBlMi() << endl;
    cout << ThrBlMi() << endl;
    cout << CHwThyRn() << endl;
    cout << CHwThyRn() << endl;
    system("pause");
    return 0;
}

char ThrBlMi()
{
      return "Three Blind Mice.";
}

char CHwThyRn()
{
       return "See How They Run.";
}

I get errors and I know why, but I don't know how to fix it. It's because i'm using char as the function header. But I don't know what I could use. I tried string and it said string was invalid. What does my header need to be to get the return i'm looking for?
Usualy int, but I just tried to compile it and it said I couldn't convert const char to int :/.

It should be compiling without a problem (At least thats what it seems like to me).

EDIT:
It might be bacuase your calling them in 'cout'. The complier is looking for text to output.

Just and idea, not sure.
Really dumb question, but how else could I call them? Just on their own? Like instead of cout just state them?

EDIT!

ObamaBinLaden, i'm getting the same const char conversion error. And i've tried changed my function headers to everything I can think of. No clue what's wrong.
Quote Originally Posted by ilovecookies View Post
This is where i'm at right now HD

Code:
#include <iostream>

char ThrBlMi();
char CHwThyRn();

int main()
{
    using namespace std;
    cout << ThrBlMi() << endl;
    cout << ThrBlMi() << endl;
    cout << CHwThyRn() << endl;
    cout << CHwThyRn() << endl;
    system("pause");
    return 0;
}

char ThrBlMi()
{
      return "Three Blind Mice.";
}

char CHwThyRn()
{
       return "See How They Run.";
}

I get errors and I know why, but I don't know how to fix it. It's because i'm using char as the function header. But I don't know what I could use. I tried string and it said string was invalid. What does my header need to be to get the return i'm looking for?
try this:
Code:
prototype:
char *CHwThyRn();

function:
char *CHwThyRn()
{
    char *retbuf="See How They Run.";
    return retbuf;
}
Apply the same to the other function
Try figuring out why this works by yourself. if you're unable to figure it out just ask
Quote Originally Posted by Hell_Demon View Post
try this:
Code:
prototype:
char *CHwThyRn();

function:
char *CHwThyRn()
{
    char *retbuf="See How They Run.";
    return retbuf;
}
Apply the same to the other function
Try figuring out why this works by yourself. if you're unable to figure it out just ask
This would also allow you to call them the way you previously were.
Quote Originally Posted by Hell_Demon View Post
try this:
Code:
prototype:
char *CHwThyRn();

function:
char *CHwThyRn()
{
    char *retbuf="See How They Run.";
    return retbuf;
}
Apply the same to the other function
Try figuring out why this works by yourself. if you're unable to figure it out just ask


I integrated this into my program, but now it just displays "Press any key to continue..." It doesn't display the contents of the functions. This is the code

Code:
#include <iostream>

char *ThrBlMi();
char *CHwThyRn();

int main()
{
    using namespace std;
   char *ThrBlMi();
   char *ThrBlMi();
   char *CHwThyRn();
   char *CHwThyRn();
    system("pause");
    return 0;
}

char *ThrBlMi()
{
      char *retbuf="Three Blind Mice";
      return retbuf;
}

char *CHwThyRn()
{
       char *retbuf="See How They Run.";
       return retbuf;
}
Quote Originally Posted by ilovecookies View Post
I integrated this into my program, but now it just displays "Press any key to continue..." It doesn't display the contents of the functions. This is the code

Code:
#include <iostream>

char *ThrBlMi();
char *CHwThyRn();

int main()
{
    using namespace std;
   char *ThrBlMi();
   char *ThrBlMi();
   char *CHwThyRn();
   char *CHwThyRn();
    system("pause");
    return 0;
}

char *ThrBlMi()
{
      char *retbuf="Three Blind Mice";
      return retbuf;
}

char *CHwThyRn()
{
       char *retbuf="See How They Run.";
       return retbuf;
}
Code:
#include <iostream>

char *ThrBlMi();
char *CHwThyRn();

int main()
{
    using namespace std;
    cout<<ThrBlMi()<<endl;
    cout<<ThrBlMi()<<endl;
    cout<<CHwThyRn()<<endl;
    cout<<CHwThyRn()<<endl;
    system("pause");
    return 0;
}

char *ThrBlMi()
{
      char *retbuf="Three Blind Mice";
      return retbuf;
}

char *CHwThyRn()
{
       char *retbuf="See How They Run.";
       return retbuf;
}
Quote Originally Posted by Hell_Demon View Post
Code:
#include <iostream>

char *ThrBlMi();
char *CHwThyRn();

int main()
{
    using namespace std;
    cout<<ThrBlMi()<<endl;
    cout<<ThrBlMi()<<endl;
    cout<<CHwThyRn()<<endl;
    cout<<CHwThyRn()<<endl;
    system("pause");
    return 0;
}



char *ThrBlMi()
{
      char *retbuf="Three Blind Mice";
      return retbuf;
}

char *CHwThyRn()
{
       char *retbuf="See How They Run.";
       return retbuf;
}


Ooooh oh oh oh, I should've went with my original coding which included the cout object so it can display the content of the character pointer. But what differs a character pointer from a variable? Or are these questions i'll have answered by the book soon enough?
Ok the problem as i see it is that the function return type should be char*. A pointer to character, because what you are returning is a string, which in C++ can be represented as a pointer to char. I may be incorrect but that's the first thing that jumped out at me.
Quote Originally Posted by zeco View Post
Ok the problem as i see it is that the function return type should be char*. A pointer to character, because what you are returning is a string, which in C++ can be represented as a pointer to char. I may be incorrect but that's the first thing that jumped out at me.
Alright! That brings up a whole new issue. I haven't quite reached pointers yet. Should I just abandon said project until I know more about pointers?

Quote Originally Posted by user186590 View Post
If so, why don't you thank him?

I just went through and thanked everyone who replied to my thread. No information is bad information.
original:

Code:
#include <iostream>

char ThrBlMi();
char CHwThyRn();

int main()
{
    using namespace std;
    cout << ThrBlMi() << endl;
    cout << ThrBlMi() << endl;
    cout << CHwThyRn() << endl;
    cout << CHwThyRn() << endl;
    system("pause");
    return 0;
}

char ThrBlMi()
{
      return "Three Blind Mice.";
}

char CHwThyRn()
{
       return "See How They Run.";
}
lala's version:

Code:
#include <iostream>

using namespace std;

void ThrBlMi();
void CHwThyRn();

int main()
{
    
    ThrBlMi();
    ThrBlMi();
    CHwThyRn();
    CHwThyRn();
    system("pause");
    return 0;
}

void ThrBlMi()
{
      cout << "Three Blind Mice." <<endl;
}

void CHwThyRn()
{
       cout << "See How They Run." << endl;
}
try to make the code as easy as possible. (in main at least)
when you don't need to use variables in your functions don't call them.

here is another example with .... i forgot the name...

dunno if it works because i am not at my "C++ PC"

edit: tested and works

when there is no variable put in when calling the function foramountoftimesdisplayed is 1.

lala's 1337 version:
Code:
#include <iostream>

using namespace std;

void ThrBlMi(int amountoftimesdisplayed = 1);
void CHwThyRn(int amountoftimesdisplayed = 1);

int main()
{
    ThrBlMi(2);
    CHwThyRn(2);
    system("pause");
    return 0;
}

void ThrBlMi(int amountoftimesdisplayed)
{
      for(int i=1; amountoftimesdisplayed >= i; i++)
      cout << "Three Blind Mice." <<endl;
}

void CHwThyRn(int amountoftimesdisplayed)
{
       for(int i=1;amountoftimesdisplayed >= i; i++)
       cout << "See How They Run." << endl;
}
Quote Originally Posted by ilovecookies View Post
Hey, thanks alot for the great explanation! No less than what i've seen you describe to other people. But now I have one last question regarding functions.

Everytime the function is called, do you have to make a prototype? Or do you only have to make the prototype at the initial use of the function?
If so, why don't you thank him?
Posts 16–30 of 41 · Page 2 of 3
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?