This question already has answers here:
Restore the state of std::cout after manipulating it
(9 answers)
Closed last year.
I have a simple converting function however the variable is changing.
void convert_print(std::string type,int num)
{
if(type=="oct"){
std::cout << num << " oct value is: " << std::oct << num << endl;
}
else if(type == "hex"){
std::cout << num << " hex value is: " << std::hex << num << endl;
}
}
int main()
{
int num = 30;
convert_print("hex",num);
convert_print("oct",num);
}
Prints
30 hex value is: 1e
1e value is: 30
Why is num getting changed to 1e when I call the function again. I never reassigned num
You haven't told cout to reset the formatting after your last call. This question describes how to restore it. Restore the state of std::cout after manipulating it
Related
This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Closed 1 year ago.
Here is the code:
int convert(int* a) {
return (*a)++;
}
int main(){
int m = 56;
int n = convert(&m);
cout << m << endl;
m = convert(&m);
cout << m << endl;
return 0;
}
Why is the answer m=57 instead of m=58 after m=convert(&m)?
The second call increments m to 58, but it returns the original value (57) due to the use of the post-increment ++ operator. That original value is then assigned back to m, overwriting the incremented value. The net effect is that m is unchanged.
You can verify this by adding some printouts to see the exact values in play:
int convert(int* a) {
std::cout << "*a was " << *a << std::endl;
int ret = (*a)++;
std::cout << "*a is now " << *a << std::endl;
std::cout << "return value is " << ret << std::endl;
return ret;
}
m = convert(&m); prints:
*a was 57
*a is now 58
return value is 57
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 5 years ago.
When using a single cout to print the same variable updated multiple times, I am getting a weird order of updates. Can anybody explain how such updates are done?
int value = 2;
cout << value << value++ << ++value << endl; // 434
value = 2;
cout << ++value << value++ << value << endl; // 424
value = 2;
cout << value++ << value++ << ++value << endl; // 435
value = 2;
cout << ++value << value++ << value++ << endl; // 532
The order in which expressions in a single statement are executed is undefined. Obviously unless specified via parenthesis or rules of order of execution. For example:
int a[3]{};
int i=1;
a[i] = i++; //undefined if a[1] or a[2]
Behaviour of such code is not defined and depends on compiler and platform in use. Needless to say you should not rely on a certain behaviour of this code.
This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 6 years ago.
After printing a pointer to an int, I print a pointer to a char:
#include <iostream>
using namespace std;
int main() {
int i;
cout << "&i: " << &i << endl;
char q = 'q';
cout << "&q: " << &q << endl;
return 0;
}
I get the following output as expected:
&i: 0xffffcc0c
&q: q
However, if I comment out cout << "&i: " << &i << endl;, and run the program again, I get the following unexplained output:
&q: q����
Does anyone know why this is happening?
If it has to do with operator<< inserting into the stream until it finds a null character, then why do I get the expected output when I include cout << "&i: " << &i << endl;?
NOTE: I am not expecting to get the address of q from cout. I am expecting to get the C string pointed to by &q. What bugs me is how the output just prints the 'q' if I include the line cout << "&i: " << &i << endl; beforehand. However, if I comment that line out, there is garbage data in the output. Why is there not garbage data in my output when I include the line cout << "&i: " << &i << endl;?
The bit &q thinks it is a string.
Therefore will print up to the null character. hence the extra output
This question already has answers here:
Is there any way to access a local variable in outer scope in C++?
(4 answers)
Closed 5 years ago.
In the program below, how to retrieve value of aa from Block A in Block C?
We can only access the outermost global aa using scope resolution operator.
Known solutoins:
1. Use different pointers in different blocks
2. Use the variable in parent block before using the current block's variable
I am looking for any other alternative solution
I know that this is not good code. Just curious in understanding how to achieve it.
#include <iostream>
#include <cstdio>
using namespace std;
// Global
int aa = 10;
int main()
{
// Main
int aa = 20;
{
// Block A
int aa = 30;
{
// Block B
int aa = 40;
{
// Block C
int aa = 50;
cout << "block C " << aa << endl;
cout << "block A " << ????? << endl;
cout << "global" << ::aa << endl;
}
cout << "block B " << aa << endl;
}
cout << "block A " << aa << endl;
}
cout << "main " << aa << endl;
return 0;
}
I know that this is not good code. Just curious in understanding how to achieve it.
You can't.
Undecorated use of the variable aa will use the one that is in the nearest scope.
Decorated use of the variable, ::aa, will use the one that is in the scope outside the function.
Decorated use of the variable, <NS>::aa, will use the one that is in the scope of the namespace <NS>.
Use references.
// Block A
int aa = 30;
auto& ref_aa = aa;
{
// Block B
int aa = 40;
{
// Block C
int aa = 50;
cout << "block C " << aa << endl;
cout << "block A " << ref_aa << endl;
cout << "global" << ::aa << endl;
}
cout << "block B " << aa << endl;
}
Of course, that's quite meaningless; if you can modify the code, then just change the variable names, or refactor it such that fewer blocks are needed (using more functions, for example).
This question already has answers here:
What is a reference variable in C++?
(12 answers)
Closed 8 years ago.
Hi I'm currently learning C++ and I'm trying to pass the value by reference, but I'm having issues with getting the correct output. What seems to be the problem??
void ref(int a)
{
cout << "a = " << a << endl;
a = 1;
cout << "a = " << a << endl;
}
int main()
{
int b = 10;
cout << "b = " << b << endl;
ref(b);
cout << "b = " << b << endl;
return 0;
}
Unless you put void ref(int &a){} you are not actually changing the value of a.
For pass by reference you have to use:-
void ref(int& a) {}