No output for cout?

蹲街弑〆低调 提交于 2019-12-13 16:14:58

问题


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


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.




回答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.




回答3:


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;


}



回答4:


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


来源:https://stackoverflow.com/questions/22001324/no-output-for-cout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!