EDIT: The problem is due to an error in the book, since it is a bit old now.
I am writing a simple program in C++, and when I recompile my code, terminal runs the previous version of my compiled file. The name of my source code is exercise.cpp. Here is my source code before refactoring it:
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter your first name and age\n";
string first_name;
int age;
cin >> first_name;
cin >> age;
cout << "Hello, " << first_name << " (age " << age << ")\n";
}
And here is the slightly refactored version:
#include "std_lib_facilities.h"
int main()
{
cout<<"Please enter your first name and age:\n";
string first_name = "???";
int age = -1;
cin>>first_name>>age;
cout<<"Hello, "<<first_name<<" (age "<<age<<")\n";
}
When I compile the refactored file, and run ./exercise in terminal, it gives the output of the first source code. For example, if your run the first compiled file and your input is "22 Mark" in the first source code, the output is "Hello, 22 (age 0)". (Or instead of 0 whatever garbage value was in the memory). If you run the newly compiled file and your input is the same, I expect the output to be "Hello, 22 (age -1)" since I have already initialized my variable in the refactored code. However, the output is still "Hello, 22 (age 0)". What causes this, and how can I fix it?
PS. I use Visual Studio Code as my editor and GNU compiler as my compiler.