-1

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.

Röyal
  • 87
  • 9
  • Look at the time stamp on the executable. Verify that the new code was built. Perhaps disable your antivirus. In short you likely need to debug the situation before getting help so that you can answer the what causes this question. Also check the build output for error messages. Perhaps your previous executable was still running so the build failed. – drescherjm Jan 19 '21 at 19:36
  • I checked the time stamp and verified that the new code has been built. I also don't get any error messages in my both build outputs. The task is built successfully. @drescherjm – Röyal Jan 19 '21 at 19:43
  • As a double-check, add `std::cout << "Compiled " << __DATE__ << " " << __TIME__ << std::endl;` before you do the input to verify the date & time the object files in the running binary were actually compiled. – John D Jan 20 '21 at 01:21
  • I checked it, and it compiles without any problems. I also checked it by changing the first output, and it changes every time I compile. The only problem is that the age variable won't output -1 when given a non-integer value. I still don't know the reason behind it. @JohnD – Röyal Jan 20 '21 at 18:53

1 Answers1

0

Try to include directive instead of std_lib_facilities.h as it might cause errors as mentioned in the accepted answer in the attached link: "std_lib_facilities.h" showing error

  • The problem is, no errors occur while compiling and running the program. Everything goes smoothly except that I can’t run the last executable for some reason. It keeps running the previous one. – Röyal Jan 19 '21 at 20:04
  • You're expecting the output to be -1 however you are asking the user to enter the value of age if you want to print -1 then remove age from the cin statement. – Youssef Beshir Jan 19 '21 at 20:30