A complex c++ question.
-
hi friends, i have a c++ program like this : #include "iostream.h" int main() { cout << "John" << endl; return 1; } The program will print "John" on screen. Now my problem is that i want to print "Hello John Bye" on the screen without changing anything inside main function. CAN SOMEBODY PROVIDE ME THE ANSWER.
-
hi friends, i have a c++ program like this : #include "iostream.h" int main() { cout << "John" << endl; return 1; } The program will print "John" on screen. Now my problem is that i want to print "Hello John Bye" on the screen without changing anything inside main function. CAN SOMEBODY PROVIDE ME THE ANSWER.
#include "stdafx.h"
using std::cout;
using std::endl;class CTest
{
public:CTest()
{
cout << "Hello John bye" << endl;exit(0);
}
};
int main(int argc, char* argv[])
{
cout << "John" << endl;return 0;
}CTest test;
-
#include "stdafx.h"
using std::cout;
using std::endl;class CTest
{
public:CTest()
{
cout << "Hello John bye" << endl;exit(0);
}
};
int main(int argc, char* argv[])
{
cout << "John" << endl;return 0;
}CTest test;
-
Based on Imran's solution, here is a bit more customized version which does as desired.
#include <iostream>
using namespace std;class CFirst
{
public:
CFirst() { cout << "Hi "; }
}class CLast
{
public:
CLast() { cout << " Bye"; }
}CFirst object1;
int main( int argc, char* argv[] )
{
cout << "John" << endl;
return 0;
}CLast object2;
This piece would print "Hi John\n Bye". Removing the line change from inside
main
is impossible, as far as I know, without doing some serious stream interception. All this seems unnecessary though. Just boot the line change from there, or pretend that you didn't notice it :P -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible. -
#include "stdafx.h"
using std::cout;
using std::endl;class CTest
{
public:CTest()
{
cout << "Hello John bye" << endl;exit(0);
}
};
int main(int argc, char* argv[])
{
cout << "John" << endl;return 0;
}CTest test;
What about as Imran had but :
CTest()
{
cout << "Hello ";
}~CTest()
{
cout << " Bye";
}... although you should really do your own homework. ;) [edit] Hmmm, this doesn't print the " Bye", altough the code goes there. Even adding cout.flush() doesn't print it. Looks like ostream system has been shut down by then. Using printf() does work in printing " Bye", but you need to eat the \n first, and adding \b doesn't seem to work ... hmmm [/edit] ...cmk Save the whales - collect the whole set
-
Please refer the code i wrote above: In the constructor of
CTest
class, there is anexit(0)
call. So this program will never enter the main function. Trick is that, in C++, global objects are created before program enters the main function. So we created the object ofCTest
class as a global object. As such its constructor gets called in which we first print our desired string and thenexit(0)
gets calls and the program ends. -
Based on Imran's solution, here is a bit more customized version which does as desired.
#include <iostream>
using namespace std;class CFirst
{
public:
CFirst() { cout << "Hi "; }
}class CLast
{
public:
CLast() { cout << " Bye"; }
}CFirst object1;
int main( int argc, char* argv[] )
{
cout << "John" << endl;
return 0;
}CLast object2;
This piece would print "Hi John\n Bye". Removing the line change from inside
main
is impossible, as far as I know, without doing some serious stream interception. All this seems unnecessary though. Just boot the line change from there, or pretend that you didn't notice it :P -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.Actually this will/may put : "Hi ByeJohn" because both objects are initialized (constructors called) before main(). I say may, because as far as i know you can't count on the order that global objects will be initialized within a given init_seg. Having said that you could use the #pragma init_seg() directives to put CFirst, CLast and main in different .cpp files and use init_seg(lib) for CFirst and init_seg(user) for CLast to ensure the order they were constructed ... but that seems like a whole lot of work to still get the wrong output. :) ...cmk Save the whales - collect the whole set
-
What about as Imran had but :
CTest()
{
cout << "Hello ";
}~CTest()
{
cout << " Bye";
}... although you should really do your own homework. ;) [edit] Hmmm, this doesn't print the " Bye", altough the code goes there. Even adding cout.flush() doesn't print it. Looks like ostream system has been shut down by then. Using printf() does work in printing " Bye", but you need to eat the \n first, and adding \b doesn't seem to work ... hmmm [/edit] ...cmk Save the whales - collect the whole set
Couldn't remember the console cursor movement functions (ala curses). However, here is a hack that works (and is within the contraints given :)), but not likely the solution you need :
#include
using namespace std;class CFirst{
public:
CFirst() {
cout << "Hi ";
}
~CFirst() {
printf("Bye\n");
}
};
CFirst f;#define endl ' '
int main( int argc, char* argv[] )
{
cout << "John" << endl;
return 0;
}...cmk Save the whales - collect the whole set
-
What about as Imran had but :
CTest()
{
cout << "Hello ";
}~CTest()
{
cout << " Bye";
}... although you should really do your own homework. ;) [edit] Hmmm, this doesn't print the " Bye", altough the code goes there. Even adding cout.flush() doesn't print it. Looks like ostream system has been shut down by then. Using printf() does work in printing " Bye", but you need to eat the \n first, and adding \b doesn't seem to work ... hmmm [/edit] ...cmk Save the whales - collect the whole set
-
Hello all, please don't laugh :laugh: but here is one solution #include #define cout cout << "Hello " #define endl " Bye" << endl int main(int argc, char* argv[]) { cout << "John" << endl; return 1; } :rolleyes: When all else fails read the manual
-
Hello all, please don't laugh :laugh: but here is one solution #include #define cout cout << "Hello " #define endl " Bye" << endl int main(int argc, char* argv[]) { cout << "John" << endl; return 1; } :rolleyes: When all else fails read the manual
That's the kind of solution that pisses teachers off. I love it. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Hello all, please don't laugh :laugh: but here is one solution #include #define cout cout << "Hello " #define endl " Bye" << endl int main(int argc, char* argv[]) { cout << "John" << endl; return 1; } :rolleyes: When all else fails read the manual
-
hi, this solution should result in compilation error, although i haven't checked it but we can't apply #define on the cout and endl like this.
It isn't doable, because you #define cout as a part of itself.. Like, what you're doing is:
#define MAX_AMOUNT MAX_AMOUNT + 20
Similar type of error. Man I really wish that that thing would've worked, it would've been a kickass "spoil-a-teacher's-day" for some C++-course :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
-
It isn't doable, because you #define cout as a part of itself.. Like, what you're doing is:
#define MAX_AMOUNT MAX_AMOUNT + 20
Similar type of error. Man I really wish that that thing would've worked, it would've been a kickass "spoil-a-teacher's-day" for some C++-course :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.