I am new to C++.I am following freeCodeCamp.org tutorial on C++.
The program looks like this-
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int luckyNums[] = {4,8,15,16,23,42};
cout << luckyNums[2];
return 0;
}
since I had VS code already installed I did not install code blocks.
Every time I run the above program I get an error "Access is denied". Also at the same time McAfee pops up saying "we just stopped a virus". I couldn't find a solution to this on Google anywhere.
When I was trying to do something with the code, in line 6 I did
cout << luckyNums[2] << endl;
and it worked and returned 15 but now the problem was I didn't understand why it worked because Mike got the result without doing this in code blocks.
Mike is the person giving the tutorial on freeCodeCamp.org
Related
I got an error while debuging a program i wrote down from a tutorial online, it is a sound synth made by the one lone coder on youtube. I'm not sure what caused it or how to fix it. I disabled pre-compiled headers witch was the first problem i encounterd.
I have tried searching online, leading me here to awnsers about code of others, i have tried going on multiple discord servers and asking there.
No luck so far. Also this is just as a little exercise to help me get back on track.
Main.cpp
#include <iostream>
#include "olcNoiseMaker.h"
using namespace std;
double MakeNoise(double dTime) {
return 0.5 * sin(540.0 * 2 * 3.14159 * dTime);
}
int main() {
wcout << "onelonecoder.com - synth part 1" << endl;
//Get all sound hardware
vector<wstring> devices = olcNoiseMaker<short>::Enumerate();
//Display finding
for (auto d : devices) wcout << "Found output device:" << d << endl;
//Create sound machine
olcNoiseMaker<short> sound(devices[0], 44100, 1, 8, 512);
//Link noise function whit machine
sound.SetUserFunction(MakeNoise);
return 0;
}
olcNoiseMaker.h is just to get acces to the sound chip and bla bla.
you can see his video here: https://www.youtube.com/watch?v=tgamhuQnOkM&t=134s
Running the program should result in a contenious sine wave of 540 KHz
instead it plays it only once and gives me this error:
Unhandled exception thrown: write access violation.
_Tgt was 0xA4EFF82C. occurred
Late to answer, but I had this issue (Visual Studio 2019). When I built and ran the program, there was just one short beep before the program exited. The issue was that I was in the x64 Debug configuration, and that was causing the error. Once I changed to x86 Debug, it was working perfectly.
Edit: Also, in your code, you are missing the while(1) {} after the sound.SetUserFunction(MakeNoise); statement which would be needed to get the code working according to the video.
I'm a complete begginer when it comes to programming. I'm trying to learn using the book "Programming: Principles and Practice Using C++" and it recommended downloading Visual Studio to code (I was previously using Codeblocks).
I downloaded the newest version (2017), got the classic applications packet for C++ and created a project.
But when I wrote my first code
#include "stdafx.h"
#include "../../std_lib_facilities.h"
int main()
{
cout << "Hello world!";
keep_window_open();
return 0;
}
Visual Studio started to freak out. When I click the local debugger button, the console app goes to the taskbar, and when I click on it, I can't do anything with it. It jsut displays "Hello World!" but no "Please enter character to exit" as it should. It doesn't react to keyboard input. I can't close it (even if I use task manager and try to close the process manually). Only solution I found is clicking "continue" button in Visual Studio, but it's not very convenient to use as the app still doesn't quite work the way I want to. Is there a way to make it work like it does in codeblocks - I just put in code, click a button and I have a fully functional console app?
One thing that could cause it - I was getting an error about using , so following advice from the internet I added
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS 1
at the beggining of the header std_lib_facilities.h
Any way someone could help me out? (Maybe I should just go back to Codeblocks and ignore Visual Studio? That doesn't seem like a good way though)
If you only want to have the behaviour of "Please enter character to exit", you could just include the cstdio header and use std::getchar() at the end. This will make sure the program does not exit until you press any key on the keyboard.
However, this method does not print out the message "Please enter a character to exit" so you would need to print it yourself using cout.
The main.cpp would end up looking something like this:
#include "stdafx.h"
#include <cstdio>
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
std::cout << "Please enter character to exit" << std::endl;
std::getchar();
return 0;
}
that's my first use to code block but it hasn't gone fine ,, i face a really weird problem
i cant even describe it so i will just tell you what's happened.
the problem is that the ide dont compile my project even if the code were correct
its just open a new tab that called "iostream" and the console window appears but empty
why do that happens ?
look to the code which the ide face a problem while compiling it ,, simplest code ever
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
and this is the compiling results...
thats all..
will codeblocks stop annoying me ?
This line is not valid syntax
usingnamespace std;
Those are two separate keywords
using namespace std;
And since you are just starting C++, Lesson 1: Don't do that.
I am new in programming. but i know turbo C & i can work with it. But how to work on Dev C++?
i means 1.How to Compile & 2. How to Run. I have Windows so if anyone help me with proper example than it will be great.
Dev c++ is very simple! To do the basic, Open you file, compile and run using the menu Execute!
However, if u are starting something new now, I would suggest you consider using eclipse. It is not so easy in the first days but the long term gain is giant!
I would like to improve my answer... I suggested that you should consider using eclipse. I still think that eclipse is great. Furthermore, I had had some problems with the old version of dev c++. After reading the comment of #Orwell (which provided facts that I didn't know), I checked the new version and the issues that I had had before disappeared. So, the big picture of dev c++ is that it is a program that works well and it is very simple. There is a cost to start using eclipse!
Example:
#include <iostream>
using namespace std;
int fibonacci(int n){
if(n==1)return 0;
if(n==2)return 1;
if(n>=3)return fibonacci(n-1)+fibonacci(n-2);
}
int main(){
//
int n =10;
cout << "The element of the Fibonacci series is " << fibonacci(n) << "\n";
return 0;
}
The program does compile but it is not running (the black screen doesn't show up). I have tried the following steps:
Reinstall the program after deleting it (didn't work)
Uninstall the program using YourUninstaller.exe, which is a program that deletes everything even the reg files (didn't work either)
So what happened?
Here is the code that I have tried:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
cout<<"hello You~~"<< endl;
system("PAUSE");
return 0;
}
conio.h and at the end of the program getch();.
This helped me with the same problem in Borland C++ installed on win xp on a virtual machine.
The problem is with your anti-virus, its blocking your binaries.
Reinstall dev c++ when your antivirus is disabled.
After installation enable anti-virus if it blocks again, go add an exception in anti-virus program
Better use sleep(1) instead of calling subprocess.
cout<<"hello You~~"<< endl;
sleep(1);
Edit: sleep(1) works fine under Linux but as well-suggested
by #user786653 use of cin.get() is recommended, and is closer than PAUSE:
#include <iostream>
using namespace std;
int main() {
cout << "hello You~~" << endl;
cin.get();
return 0;
}
The same problem is also faced by me in newest version of Dev
i just uses the header file #include <bits/stdc++.h> in initial
and with this use getchar() just before return 0 statement in last of program.
it works definitely please remember getchar() is just before returen statement , not after return statement.