Jump to content

Why is Visual Studio doing this!?

mattonfire
Go to solution Solved by trag1c,

Move the cin.get() function call to the line before the end of your main and it will halt closing the program so that you can view your input.

1 minute ago, Enderman said:

you need to have


using namespace std;

at the top of your code

and


system("pause");

where you want it to stop 

 

http://www.cplusplus.com/forum/beginner/1873/

He has using using namespace std; at the top of of his file just below the includes. Anything to do with system() should be avoided as it introduces many vulnerabilities to the application and system.

There are no errors showing for this but for some reason just after I enter the integer from the `cin` the window closes ignoring my answer, which should determine if it says "No that isn't my fav number" or continue to a function.

Here is my code:

http://hastebin.com/rujakujuje.tex


Here is the output:

http://hastebin.com/atezutohum.tex

Link to comment
Share on other sites

Link to post
Share on other sites

you need to have

using namespace std;

at the top of your code

and

system("pause");

where you want it to stop 

 

http://www.cplusplus.com/forum/beginner/1873/

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

Move the cin.get() function call to the line before the end of your main and it will halt closing the program so that you can view your input.

1 minute ago, Enderman said:

you need to have


using namespace std;

at the top of your code

and


system("pause");

where you want it to stop 

 

http://www.cplusplus.com/forum/beginner/1873/

He has using using namespace std; at the top of of his file just below the includes. Anything to do with system() should be avoided as it introduces many vulnerabilities to the application and system.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, trag1c said:

Move the cin.get() function call to the line before the end of your main and it will halt closing the program so that you can view your input.

He has using using namespace std; at the top of of his file just below the includes. Anything to do with system() should be avoided as it introduces many vulnerabilities to the application and system.

system pause just makes the window hold until you press any key

how is that a vulnerability

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Enderman said:

system pause just makes the window hold until you press any key

how is that a vulnerability

Because system() can be replaced with malicious code as its an external executable that is called. So someone could replace the pause exe with a malicious one and totally screw you or the user over. 

 

Edit: There also other reasons to not use system() that can be derived from the text above. Its resource heavy because you're opening another executable and waiting for it to return. You also may not execute the command you were intending to execute. 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, trag1c said:

Because system() can be replaced with malicious code as its an external executable that is called. So someone could replace the pause exe with a malicious one and totally screw you or the user over. 

 

Edit: There also other reasons to not use system() that can be derived from the text above. Its resource heavy because you're opening another executable and waiting for it to return. You also may not execute the command you were intending to execute. 

This negates a lot of C++'s benefits. 

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, AluminiumTech said:

This negates a lot of C++'s benefits. 

I don't think so simply because there are much better methods of pausing execution of a program that take little to no effort to implement. One of those being cin::get. Or simply waiting for a OS message like WM_KEYDOWN in a looped Windows application that is without a console. From my 6 years of experience with C++ I have never had to rely on system() anyways so its not at all a hindrance. There also the issue of portability with system("pause") as well since thats a windows only command so by saving your self keystrokes you're also keeping code portable and safe. 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, AluminiumTech said:

This negates a lot of C++'s benefits. 

What does C++ and its benefits have to do with system()?

Cons :

It's not portable. This works only on systems that have the PAUSE command at the system level, like DOS or Windows. But not Linux and most others...

It's a very expensive and resource heavy function call. It's like using a bulldozer to open your front door. It works, but the key is cleaner, easier, cheaper. What system() does is:

  • suspend your program
  • call the operating system
  • open an operating system shell (relaunches the O/S in a sub-process)
  • the O/S must now find the PAUSE command
  • allocate the memory to execute the command
  • execute the command and wait for a keystroke
  • deallocate the memory
  • exit the OS
  • resume your program

There are much cleaner ways included in the language itself that make all this unnessesary.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Okay, so don't use system(), the cons are just too much.

23 hours ago, Enderman said:

snip

Yes, this works. Thank you. And from some initiative I fixed "Else" too, well it might be common sense for you but anyway cheers :P

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Nineshadow said:

What does C++ and its benefits have to do with system()?

Cons :

It's not portable. This works only on systems that have the PAUSE command at the system level, like DOS or Windows. But not Linux and most others...

It's a very expensive and resource heavy function call. It's like using a bulldozer to open your front door. It works, but the key is cleaner, easier, cheaper. What system() does is:

  • suspend your program
  • call the operating system
  • open an operating system shell (relaunches the O/S in a sub-process)
  • the O/S must now find the PAUSE command
  • allocate the memory to execute the command
  • execute the command and wait for a keystroke
  • deallocate the memory
  • exit the OS
  • resume your program

There are much cleaner ways included in the language itself that make all this unnessesary.

For the OS con (system only works on DOS and Windows), if you compile C++ it turns into a exe file that can't be run from any other OS apart from Windows anyway. Or can you compile C++ into other's like .jar's and things.

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, mattonfire said:

For the OS con (system only works on DOS and Windows), if you compile C++ it turns into a exe file that can't be run from any other OS apart from Windows anyway. Or can you compile C++ into other's like .jar's and things.

You just compile the source for another OS, for example for Linux you usually use the GCC/g++ compiler. C/C++ are inherently not locked to any OS, that is introduced by the programmer using OS specific libraries and calls etc... 

Link to comment
Share on other sites

Link to post
Share on other sites

Anyone know why forms is doing this as soon as I started up, before I even started editing?

Untitled.png

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×