简体   繁体   中英

How to use new & delete operators?

New & Delete Operators

On the Visual Studio Code editor, I tried to use the new and delete operators but the editor throws an error which I am unable to figure out. I tried out everything to remove the error but all in vain!!!

#include <iostream>
using namespace std;

int main()
{
int avg, *ptr1, *ptr2, *ptr3;

ptr1 = new int;
ptr2 = new int;
ptr3 = new int;


cout << "Enter the first number : ";

cin >> *ptr1;

cout << "Enter the second number : ";

cin >> *ptr2;

cout << "Enter the third number : ";
cin >> *ptr3;

avg = (*ptr1+*ptr2+*ptr3)/3;
cout << "Average is : " << avg << endl;

delete ptr1;
delete ptr2;
delete ptr3;

return 0;
}

This is the Error that is displayed: 在此处输入图像描述

The error message has been misinterpreted by the asker and has nothing to do the asker's use of with new and delete or any of the asker's C++ code (which could use some error checking on the IO but otherwise seems correct). Visual Studio Code couldn't start compiling the code because the shell used to build the program could not consume the filename n&d.cpp because of the & in it.

Change the name to n_and_d.cpp or any other name that avoids using characters commonly used in programming as operators or delimiters. There will be similar problems in other build systems because of the spaces in the path leading to the source file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM