My cout seems to be cutting first character

Pages: 12
Feb 14, 2022 at 1:56am
Hi, I just picked up c++ and just running through some basic tutorial.

Code is using cout and cin and the main difference that I did was put the printing in a separate function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>

void printResult(int x){
    std::cout << "You entered " + x << std::endl;
}

int main()
{
    std::cout << "Please input number: ";

    int x{};
    std::cin >> x;
    printResult(x);
    return 0;

    // std::cout << "Please input number: "; 
    // int x{}; 
    // std::cin >> x; 
    // std::cout << "You entered " << x << '\n';
    // return 0;
}


Output goes like this:

c:\cpp-workspace\atm>main.exe
Please input number: 1
ou entered

c:\cpp-workspace\atm>main.exe
Please input number: 2
u entered

c:\cpp-workspace\atm>main.exe
Please input number: 3
entered

c:\cpp-workspace\atm>main.exe
Please input number: 4
entered

The more I run the compiled main.exe with different input number, I see that it cuts off the first character even more from my print message.

When I put everything in the main function, everything seems fine and working.

What could be the cause?
Last edited on Feb 14, 2022 at 2:01am
Feb 14, 2022 at 2:18am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

void printResult(int x){
    std::cout << "You entered " + std::to_string(x) << std::endl; // <--
}

int main()
{
    std::cout << "Please input number: ";

    int x{};
    std::cin >> x;
    printResult(x);
    return 0;

    // std::cout << "Please input number: ";
    // int x{};
    // std::cin >> x;
    // std::cout << "You entered " << x << '\n';
    // return 0;
}
Feb 14, 2022 at 7:57am
@shaefayejem,
Your "<<" (on line 19) morphed into a "+" on line 4.

You might get away with it in javascript, but not c++.
Feb 14, 2022 at 11:44am
i.e. std::cout << "You entered "<< x << std::endl;
Feb 14, 2022 at 12:26pm
> std::cout << "You entered " + x << std::endl;
What you have here is C style pointer arithmetic in disguise.
A string literal has a type of 'const char *'.

A normal string could be considered as being equivalent to
std::cout << &"You entered "[0]

But with a + there, what you now have is
std::cout << &"You entered "[x]

So if x is 1, then you lose the first character.
Feb 15, 2022 at 9:54am
Hi Guys,
I think I got the point that I made a mistake about using the "+" operator. I learned so much just by making this silly mistakes.

Based on the suggestion, I could do either of these two item

1. std::cout << "You entered " + std::to_string(x) << std::endl;
2. std::cout << "You entered "<< x << std::endl;

which would solve my initial problem.

Now, what I would like to understand is, how come that I am running my main.exe several times but does it remembers my previous run?

I thought after I exit main() and return 0, everything should be re-initialized again back to the start but it looks to me that it is remembering my previous run.

Thank you all for helping me in my previous problem and I just would like to understand this further.
Feb 15, 2022 at 9:57am
I thought after I exit main() and return 0, everything should be re-initialized again back to the start but it looks to me that it is remembering my previous run.


No, the results of your previous run are long gone.

It isn't remembering anything - you are simply putting different input in each time (1, 2, 3, 4 ... from your original post).

Read @salem c's explanation of what is actually happening.
1
2
3
4
5
#include <iostream>
int main()
{
   for ( int x = 0; x < 10; x++ ) std::cout << "You entered " + x << '\n';    // Unintended use of '+'
}
Last edited on Feb 15, 2022 at 10:04am
Feb 15, 2022 at 10:01am
I thought after I exit main() and return 0, everything should be re-initialized again back to the start but it looks to me that it is remembering my previous run.


Everything except memory contents is re-set at the start. So un-initialised variables have an initial value of whatever happened to be in memory at their location - which could be their value from the last run or something different.
Last edited on Feb 15, 2022 at 11:40am
Feb 15, 2022 at 10:23am
Modern operating systems normally zero out all the memory before handing it to the program. Global variables are automatically zero initialized. In the original program that the OP posted there is no use of uninitialized variables.

Note that reading from uninitialized variables are technically undefined behaviour. What seeplus says is often what ends up happening but there are no such guarantees in the language specification so it's nothing to rely on.
Feb 15, 2022 at 10:32am
So are statics
Feb 15, 2022 at 10:49am
Time for Peter87 to disappear I think. Unsolicited evaluation and arguing about other people's (in this case @seeplus') contributions is arrogant and has fortunately been a thing of the past. Unfortunately it is now reappearing. Address the @OP, Peter87, or bugger off. You're not adding value with your nonsense.
Feb 15, 2022 at 3:19pm
Welcome back @Peter87, glad you started posting again.
Feb 15, 2022 at 3:26pm
@mbozzi, thank you. Nice to see familiar names. :)
Feb 15, 2022 at 3:45pm
Likewise, @Peter87. You've taught me quite a lot in the past.
Feb 15, 2022 at 4:11pm
Whoa, a blast from the past, Peter87! Nice to see you again.

I second lastchance's comment, I've learned a lot from you.

Some of it even "stuck." ;)
Feb 16, 2022 at 1:08am
Thanks all I think I understood your point. I just happen to read about pointer topics last night and from what I understood and what the answers that I have read here it looks like I am doing pointer arithmetic such that

ptr + 1; //next address
ptr + 2; //next address after ptr + 1
ptr + 3; //next address after ptr + 2

Thanks ALL for helping me understood this concept.
Feb 16, 2022 at 4:13am
Feb 16, 2022 at 8:43am
So welcome back Peter87.

Modern operating systems normally zero out all the memory before handing it to the program.
No, I don't think so. That would have too much of an performance impact. If at all the compiler would generate code that does it. But this happens rather in debug mode.

Global variables are automatically zero initialized.
Yes. And I find it kind of surprising. Why are global variables handled different than local variables?
Feb 16, 2022 at 10:26am
Thanks.

Peter87 wrote:
Modern operating systems normally zero out all the memory before handing it to the program.
coder777 wrote:
No, I don't think so. That would have too much of an performance impact.

I'm talking about memory that is handed to the program from the OS when the program starts or needs more memory. Not when the memory is reused within the program. The OS zeroes the memory in order to prevent programs from reading the data that is left over in memory from other programs because that could be a security problem.


coder777 wrote:
Yes. And I find it kind of surprising. Why are global variables handled different than local variables?

All variables with static storage duration (which includes "globals") are zero initialized before normal initialization takes place. I think the reason the standard guarantees this is because it normally happens anyway due to what I said earlier.
Last edited on Feb 16, 2022 at 10:31am
Feb 16, 2022 at 10:42am
I'm talking about memory that is handed to the program from the OS when the program starts or needs more memory.
Ok, that makes sense. Only the global data is initialized to zero, which then has an impact on the starting time. So the globals (which don't have a constructor) aren't initialized by the language...
Last edited on Feb 16, 2022 at 10:43am
Pages: 12