• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

fetching for some info in c++

Flourence

New Member
Joined
Sep 22, 2009
Messages
60
Reaction score
4
I was wondering on creating functions -custom ones- , can the function param be typeless like lua ones?

like
Code:
int add(a, b)
{return a+b;}

int main()
{int a =3; int b = 4; cout << add(a,b);}
 
Well, I'm not sure what you're requesting this for, and why does it need to be "typeless" (quotes, because it's never typeless).

Tell me if that's what you're requesting:

Preprocessor
PHP:
#define add(a,b,c) {c = a+b;}
int main()
{
    int a = 1;
    int b = 1;
    int c;

    add(a,b,c);
    std::cout<<c;
    return 0;
}

or...
Templates
PHP:
template <class _T>
_T add(_T a, _T b)
{
   return a+b;
}

int main()
{
    std::cout<<add<int>(1,1);
    return 0;
}

...at the end, they get a type. So, if you could enlighten me for what you need it to be "TYPELESS" I might be able to find a better way.
 
Last edited:
[cpp]#include <sstream>

template<typename _Tp>
std::string toString(const _Tp& __p)
{
std::stringstream ss;
ss << __p;
std::string ret;
ss >> ret;
return ret;
}
[/cpp]
 
You could have used using namespace std
ohh plz no plz no!... I totally dislike namespaces. It makes the code hard to follow when you're not familiar with it or doesn't have a good IDE. It's "ok" to use them on huge libraries like boost in order to save you a few keystrokes, for example:
boost::asio::ip::tcp::resolver::query
...Now thats fucking huge. But still, thats no excuse to risk readability and a few more issues that comes along with namespaces, just for a syntactic sugar.

Like Migxxx said but there's also another way:
Code:
#define add(x, y) x+y
Well, thats actually the same as my first example (and I'm talking about the usage of preprocessor definitions) with the difference that it return a value... which at the end it's almost the same as mine. But yeh :p
 
Last edited:
More info :)

Was learning some basics and came into making a little code , i dont get compile errors but after the first loop is done other ones is messed up it keep sending lines untill loop finishes (without waiting for input to continue) , tryed to remove getline() and replaced with cin and it worked well , but i need to get the whole line :

Code:
#include "iostream"
#include "cstdlib"
#include "string"

using namespace std;

struct db
{
    int num;
    string str;
};

int main ()
{

        db p[10];
        db *f = p;
        int x = 0;
        while (x<10)
        {
                cout << "What is his name?"<<endl;
                getline(cin,f[x].str);
                cout << "What is "<< f[x].str << "'s age?" <<endl;
                cin>>f[x].num;
                x++;
        }
        cout << "Name \t Age"<<endl;
        for(int y = 0; y<10 ; y++)
        {
               cout<< f[y].str << "\t" << f[y].num << endl;
        }




        system("PAUSE");
        return 0;
}
 
Last edited:
Back
Top Bottom