0

I deleted the contents of my settings.json file by mistake and after that, all my C++ programs are not working correctly.

Here is a code snippet for reference:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define EPS 1e-9

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cout << "enter a number";
    int n;
    cin >> n;

    return 0;
}

Ideally, while running the program, it should print the cout statement "enter a number" and then ask for user input. But it is printing the statement in the end and asking for input first.

Here is the screenshot for the same:

enter image description here

Also, I tried running a C code and a Java code as well and they seem to work fine. The problem lies with just C++ programs.

Dipanshu
  • 15
  • 4
  • 3
    Try: `cout << "enter a number" << std::endl;` – drescherjm Apr 18 '22 at 15:43
  • 5
    `ios_base::sync_with_stdio(false);`, `cin.tie(NULL);` and `cout.tie(NULL);` are probably not helping, what are they in there for? Also, since we're talking, those `typedef`s are not good practise, and neither are `#include ` and `using namespace std;`, especially when used in combination. You should take all that out, code to the types you're actually using (so that other people have a chance of understanding your code), include (only) the headers you need and explicitly prefix things like `cin` and `cout` with `std::`, to avoid name collisions. Good habits start early. – Paul Sanders Apr 18 '22 at 15:43
  • 3
    All of the things pointed out are tell-tale signs you are attempting to learn C++ from one of those "online competitive coding" sites. Those sites are not designed for learning C++. – PaulMcKenzie Apr 18 '22 at 15:55
  • @PaulSanders Thank you for your help. Removing ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); helped solve my problem. Sorry for the cluttered code. Actually, I am trying to do some competitive programming, so this was my default template for C++. – Dipanshu Apr 18 '22 at 15:55
  • 2
    @Dipanshu -- Those "competitive programming" sites assume you know the computer language you are using to solve their problems well enough to never have to ask a question here on StackOverflow concerning fundamental C++. Those questions are just random puzzles designed for the *experienced* user of the language to attempt to solve. For example, stuff like this: `typedef long long int ll;` could have been replaced with `uint64_t`, but you wouldn't know this if you were not experienced in C++ already. – PaulMcKenzie Apr 18 '22 at 15:57
  • @PaulMcKenzie To be honest, i really didn't know this. Can you mention some good resource for learning C++? – Dipanshu Apr 18 '22 at 16:03
  • 2
    See if I can get to this first: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Paul Sanders Apr 18 '22 at 16:04
  • 1
    FYI, the abbreviations actually take more time to build your program. You are invoking the preprocessor to perform substitutions before compilation. Without the abbreviations, the preprocessor doesn't take as long and the compiler already has the keywords in its dictionary. If you need to gain time by using abbreviations, take a keyboarding class. Typing is one of the least amounts of time saving. The time you save by using abbreviations, is wasted by debugging. – Thomas Matthews Apr 18 '22 at 16:22
  • 1
    Using abbreviations makes your program more difficult to read. For example, since `ll` is not a C++ keyboard, I have search your code for the definition, then go back to where it was used. On that note, the compilers allow for at least 32 characters in a variable or function name. Use them wisely. – Thomas Matthews Apr 18 '22 at 16:24

0 Answers0