2
#include<iostream>
#include<iomanip>
#include <math.h>


using namespace std;

int doIt(int a){
    a=a/1000;
    return a;
}

void main(){
    int myfav= 23412;
    cout<<"test: "+doIt(myfav);
    cin.get();
}

just wondering why i am not getting a print out for this. Thanks in advance.

  • 1
    That should be `int main`. It shouldn't compile as is, which would explain no output. – chris Feb 24 '14 at 23:25
  • 2
    @chris Most compilers will take `void main` to mean `int main` always returning 0. There might even be something in a spec somewhere about that interpretation. – ssube Feb 24 '14 at 23:26
  • @ssube, Neither GCC, nor Clang takes it, and I presume MSVC as well (though maybe only newer versions if it compiles for the OP). And I'm sure it's not allowed. The standard explicitly says it must return `int`. The only compiler I know of that I'm sure accepts that is TurboC++. – chris Feb 24 '14 at 23:28
  • @chris VS will allow it (and assume a `return 0` always). I believe VS2013 gives a warning about it, but VS2010 does not. That said, it is non-standard. – Zac Howland Feb 24 '14 at 23:32
  • @chris It looks like, judging from [this question](http://stackoverflow.com/questions/9356510/int-main-vs-void-main-in-c), that it's an old C-ism and may actually be legal. I've certainly done that before on accident, although the specifics of how and when escape me. – ssube Feb 24 '14 at 23:32
  • @ssube, Yes, the wording changed from C to C++. – chris Feb 24 '14 at 23:35
  • @DOUGLASO.MOEN: Presumably, thinking that it would convert the integer to a string and concatenate it, as other languages do. That's the error, of course. – Mike Seymour Feb 24 '14 at 23:40

4 Answers4

2

Using C++ streams, you should cout << "test: " << doIt(myfav), rather than trying to + them together. I'm not sure off the top of my head whether << or + takes precedence, but regardless of whether you're adding to a stream or a string literal, that's not going to work very well.

ssube
  • 47,010
  • 7
  • 103
  • 140
2

void main() is not a valid signature for your main function (though, VS will recognize it, it is not standard-compliant). It should be int main().

You cannot insert an integer into to a string using +. You need to use the extraction operator of std::ostream: operator<<. What you have will result in pointer arithmetic (adding the result from doIt to the address of your const char*, which is undefined behavior).

std::cout is a buffered output stream. Since you do not flush your buffer, there is a chance that the program ends before you are able to see the output (prior to the console closing). Change your output line to one of the following:

std::cout << "test:  " << doIt(myFav) << std::endl; // flush the buffer with a newline

or

std::cout << "test:  " << doIt(myFav) << std::flush; // flush the buffer

All in all, what you have will compile, but will not do what you want it to, at all.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • 1
    The system *should* flush the buffers when the program ends, as long as it terminates normally. – ssube Feb 24 '14 at 23:30
  • Indeed, `std::cout`'s destructor calls `flush`. – chris Feb 24 '14 at 23:31
  • @ssube Given that his current code will not even compile, that is the least of his worries. IIRC, the version of the standard library that shipped with VS2010 did not flush the buffer in the destructor for `std::ostream`. – Zac Howland Feb 24 '14 at 23:36
  • @ZacHowland, Eww, that's not nice. Although I do wonder what doesn't compile. If it's the addition, that actually will (and invoke UB here). – chris Feb 24 '14 at 23:38
  • Additionally, since `flush` is synced with `std::cin`, it could be flushed as the program is exiting ... meaning it is printed, but the console closes before he can see it. – Zac Howland Feb 24 '14 at 23:39
  • @chris - Actually, you are correct ... it would do pointer arithmetic. I'll update. – Zac Howland Feb 24 '14 at 23:40
2

There are few this i would like to point out. First return type of main function void main() it should be int main().

Don't use using namespace std; for more detail visit Why is "using namespace std" considered bad practice?

Finally problem in your code you cannot insert an integer into to a string using +, you will have to extraction operator i.e. << again.

#include<iostream>
#include<iomanip>
#include <math.h>

//using namespace std;

int doIt(int a)
{
    a=a/1000;
    return a;
 }
int main()
{
    int myfav= 23412;
    std::cout<<"test: "<<doIt(myfav)<<"\n";
    std::cin.get();
    return 0;


}
Community
  • 1
  • 1
Shravan40
  • 8,922
  • 6
  • 28
  • 48
  • Without `using namespace std;`, what you have will not compile (unless you prepend `std::` in front of `cout` and `cin`. – Zac Howland Feb 25 '14 at 00:28
0

This expression "test: "+doIt(myfav) in statement

cout<<"test: "+doIt(myfav);

means that you add some integral value to the pointer that points to the first character of string literal "test: ". And as the result the statement outputs obtained pointer value.

Your could use operator + if you would convert the integral value returned by the function to an object of type std::string. For example

cout<<"test: "+ to_string( doIt(myfav) );

To do this you need include header <string> Take into account that function main shall have return type int in C/C++. And it is better to use header <cmath> instead of heder

Resuming all what I said I will show how the program can look

#include<iostream>
#include <string>

inline int doIt( int a ) { return a / 1000; }


int main()
{
    int myfav = 23412;

    std::cout << "test: " + std::to_string( doIt( myfav ) ) << std::endl;

    std::cin.get();
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • There isn't really a need to use `std::to_string` here as `std::ostream` has an overloaded `operator<<` for `int`. – Zac Howland Feb 25 '14 at 00:27
  • @Zac Howland I showed how the statement could be written with operator + if he want to use it the same way as it is used in C# – Vlad from Moscow Feb 25 '14 at 00:32
  • @Zac Howland Also it is important to demonstrate when and where the operator + is overload that is to demonstrate the difference between "test: " + integer and "test: " + std::string. – Vlad from Moscow Feb 25 '14 at 00:35
  • I get that, but since `to_string` is going to call the `operator<<` overload anyway, it is a little silly to use it in this case. – Zac Howland Feb 25 '14 at 00:52