简体   繁体   中英

No output for cout?

#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.

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.

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.

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 ie << 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;


}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM