Random number is returned - c++

The following code :
#include <iostream>
using namespace std;
int test()
{
cout<<"question \n";
return 0;
}
int main(){
cout<<test;
}
Output:
question
1
The following code gives 1 everytime I run but I am expecting the output to be 0.
Whereas when I replace the test by test() then I get the expected output. Not sure why is this happening. Please suggest and comment if any rule behind it.

C++ always requires you to use parentheses to call functions. If you omit them, it thinks you want to know the address of the function instead of calling it. The address is then considered to be a (truthy) boolean and gets output as 1. This warning (from gcc -Wall) explains it well:
x.cpp: In function ‘int main()’:
x.cpp:9:11: warning: the address of ‘int test()’ will always evaluate as ‘true’ [-Waddress]
cout<<test;
^

std::cout << test;
Outputs 1 because test is a function pointer, it will be converted to bool with std::cout.

Related

What is logic behind output of this code postfix and prefix on same varible in one line c++

How for post increment(a++) return 5. As according to operator precedence a++ should execute first and should return 4.
#include <iostream>
using namespace std;
int main()
{
int a=4,b=4;
cout<<++b<<" "<<b++<<" "<<a++<<" "<<++a<<endl;
return 0;
}
Output: 6 4 5 6
You should always compile your code with warnings enabled. The compiler will tell you that the outcome might be undefined:
prog.cc: In function 'int main()':
prog.cc:6:22: warning: operation on 'b' may be undefined [-Wsequence-point]
cout<<++b<<" "<<b++<<" "<<a++<<" "<<++a<<endl;
~^~
prog.cc:6:41: warning: operation on 'a' may be undefined [-Wsequence-point]
cout<<++b<<" "<<b++<<" "<<a++<<" "<<++a<<endl;
^~~
If you want to use and modify one variable with in the same expression, you need to check the order of evaluation and sequence rules to see if it is valid, and what the expected result is.

C++ Expected string literal before args

#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
string argstr[argc];
for(int c = 1; c++; c<argc) {
argstr[c].assign(argv[c]);
}
for(int c = 1; c++; c<argc) {
__asm__(argstr[c]); //This is where the error occurs
cout << argstr[c] << endl;
}
}
If I try to compile it with MinGW, I get the following error:
Main.cpp: In function 'int main(int, char**)':
Main.cpp:15:6: error: expected string-literal before 'args'
asm(args);
I know this, that's why I assign the arguments to a vector of strings.
__asm__() is a compile-time construct. The argument must be a string literal, and not a variable.
You can't dynamically execute assembly code like this. The compiler needs to know about the assembly instructions at compile time so it can check if they are valid, but you are trying to pass them in at run time.
Edit: One workaround is to write a wrapper program in python (for example) that injects a string literal into your C++ __asm__ construct and then compiles it and executes it for you.

Unit testing a vector string

I have this really simple line of code in my production-code(A.cpp) as follows:
std::string A::getString(int i) {
return sVect_[i];
}
with the header as follows:
class A{
public:
std::string getString(int i);
...
private:
vector<std::string> sVect_;
...
};
I've been trying to test the getString() function using googletest but an error keeps popping out:
error: invalid conversion from 'char* (*)(const char*, int)throw ()' to 'int'
error: initializing argument 1 of 'std::string A::getString(i)'
This was my test program:
TEST(ATest, getString){
A a;
EXPECT_EQ("c", a.getString(i));
}
I couldn't quite grasp the workaround of the vector string and how to call it in my test program without ever changing the production code. I even use the hack, adding #define statements, to access the private member but still couldn't do it.
How do my test actually looks like to successfully call that function?
Note: I'm on Linux and using gcc. Thank you in advance guys.
Perhaps the error message is misleading. Have you defined i globally somewhere else? To me it looks like in the local scope because it does not know what the value of the variable i is, it is misbehaving in an unexpected way
TEST(ATest, getString){
A a;
EXPECT_EQ("c", a.getString(i)); //here what is the 'i' and where is it defined
}

Compilation error while using to_string in c++ program

To get precision and scale of a number i am using this simple program. But while converting number into string it is giving compilation error.
g++ precision.cpp
precision.cpp: In function ‘int main()’:
precision.cpp:6: error: ‘to_string’ was not declared in this scope
When I compile with the -std=c++0x switch I get
g++ precision.cpp -std=c++0x
precision.cpp: In function ‘int main()’:
precision.cpp:6: error: call of overloaded ‘to_string(int)’ is ambiguous
/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/basic_string.h:2604: note: candidates are: std::string std::to_string(long long int)
/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/basic_string.h:2610: note: std::string std::to_string(long long unsigned int)
/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/basic_string.h:2616: note: std::string std::to_string(long double)
The source code looks like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string value = to_string(static_cast<int>(1234));
int precision = value.length();
int scale = value.length()-value.find('.')-1;
cout << precision << " " << scale;
return 0;
}
What is causing this error?
The first error is because std::to_string is a C++11 feature, and GCC by default compiles in C++03 mode.
The second error, when you are using the correct flag, is probably because the support for C++11 in GCC 4.4 (which you seem to be using) is quite minimal. As you can see by the error messages, the compiler shows you the alternatives it have.
By the way, you don't need to cast integer literals to int, they are of type int by default. You might want to cast it to long double though, as that's one of the valid overloads and you seems to want to find the decimal point (the code will not work as expected if there is no decimal point in the string, like when converting an integer).
I recommend to use boost::lexical_cast instead.

Code compiles in ideone but not with gcc

I wrote the following code:
#include <iostream>
using namespace std;
int main()
{
int v()
return 0;
}
I ran it in ideone, and it compiled successfully. I have the same code in file test1.cpp on my computer, I ran g++ test1.cpp and I got the following error:
./test1.cpp: In function ‘int main()’:
./test1.cpp:7:2: error: a function-definition is not allowed here before ‘return’
Why dose this happen? is this a bug?
I'm using linux mint, gcc version 4.7.
You are missing a semi-colon here:
int v()
^
should be:
int v() ;
which is a function declaration, not clear that was what was intended though. If you want to initialize v then the following would work:
int v(0) ;
or in C++11:
int v{0} ;
This is commonly known as C++'s most vexing parse. When you do something like
int f();
the compiler reads this as a function prototype, declaring a function f that returns an int. If you're using C++11, you should instead do
int f{}; // f initialized to 0
if you're not using C++11, make sure to initialize the variable right away.
You forgot the semicolon after
int v();
Ideone is using gcc 4.8.1 for your code (as you can see in your own link) while you are using 4.7
There are several difference regarding C++ 11 implementation, and apparently it is affected by the line that looks like a function delcaration.