My cout seems to be cutting first character

Pages: 12
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
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;
}
@shaefayejem,
Your "<<" (on line 19) morphed into a "+" on line 4.

You might get away with it in javascript, but not c++.
i.e. std::cout << "You entered "<< x << std::endl;
> 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.
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.
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
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
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.
So are statics
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.
Welcome back @Peter87, glad you started posting again.
@mbozzi, thank you. Nice to see familiar names. :)
Likewise, @Peter87. You've taught me quite a lot in the past.
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." ;)
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.
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?
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
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
Pages: 12