When I click to compile and run my code in c++, using dev c++, the code takes a while to run in the console, even though it's something very basic. The console screen opens and goes black, with the cursor blinking, the program only starts after a few seconds. How can I solve this problem? Can someone help me, please?
#include <iostream>
using namespace std;
int main() {
int valor[5] ;
int i ;
for(i = 0 ; i < 5 ; i++) {
cout << "digite valor[" << i << "]" << endl ;
cin >> valor[i] ;
}
for( i = 0 ; i < 5 ; i++) {
cout << "valor[" << i << "]: "<< valor[i] << endl ;
}
return 0;
}
Got a similar issue some years ago at work, with the imposed antivirus. All compiled executables, even the smaller one, took several seconds to really be launched.
We were forced to request some special rights to IT in order to be able to exclude from scanning our development folders. It solved the problem instantly.
You should be able to tell your antivirus to exclude your base development folder (let's say "C:\Users\malkmim\Projects" and all its subfolders) from scan, and then test again if you still have this issue.
Related
I have to print some ASCII characters (extended version, code page 437) in console application as an exercise. Now, instead of writing cout << char(196) << char(196) << char(196);, I'd like to write cout << "───" to make a line. I have found that I can save files with the correct encoding by clicking "Save as" and choosing "save with encoding" (in this example - OER United States - Codepage 437). After doing that, the following code compiles and runs without any problems:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "\n *** Multiplication Table ***\n\n";
for (int i = 1; i < 11; i++)
{
if (i == 2) cout << "────┼────────────────────────────────────\n";
for (int j = 1; j < 11; j++)
{
if (j == 2) cout << "│";
cout << setw(4) << i * j;
}
cout << "\n";
}
}
However, when I close Visual Studio and reopen the project, suddenly all special ASCII characters are changed. For example, this bit "────┼────────────────────────────────────\n"; becomes "ÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n"; in the code editor. However, the program still runs correctly and displays ────┼────────────────────────────────────.
If I try to save the project with encoding again, I am warned that there are characters that I cannot save with selected encoding and asked to use Unicode instead, which then messes up the characters again and also stops the program from showing the correct characters.
Question: How do I make Visual Studio 2022 use the correct encoding when opening projects?
I am currently trying to make the Conway's game of life in c++ for a school project. The problem is I don't know how to make the board more "dynamic". How can I return several lines to change the output I made before? Right now I have to print various boards which isn't exactly aesthetic.
Looking this up on previous questions I could only find "/r" which helps if the board is only one line, but in my case it isn't.
Edit: Added code sample (if it helps).
for (int temp = 0; temp < iterations; temp++){
for(long long i = 0; i < height; ++i){
for(long long j = 0; j < width; ++j){
if (CurrentGen[i][j]){
cout << "■" << " ";
}
else{
cout << "." << " ";
}
}
cout << "\n";
}
cout << "---------------------------------------" << endl;
CurrentGen = NextGen(CurrentGen, height, width);
sleep(1);
}
This will end up being more than complicated solution for the system dependent code.
However the easy and quick solution is for visually having the same line being updated is to create many empty lines for every loop iteration.
for (int n = 0; n < 50; n++)
{
std::cout << "\n";
}
As I said earlier this is the best C++ could do when there is no system information available, using only the standard commands.
There are other ways to do it such as:
system call to clear the terminal
but this method not safe
The standard way to print end lines
Using the ncurses library #include <curses.h>
Advantage: it is a cross-platform
Disadvantage: cannot be mixed with standard I/O
I wanted to start learning how to program, so I asked my math professor if he had a book that I could borrow. He did and so I have been reading a C++ book from ~1994 (it still has a floppy disk :P). Anyway, I made it to a point in it and it sets up a program that calculates y in y=mx+b. Pretty simple, but I decided to try it out and it is not working. I would really like to figure out why it is not working and fix it.
Here is the code for it:
#include <iostream>
using namespace std; //not in the book: added by me after some Googling
int main() {
cout << "Input m: " << flush;
int m;
cin >> m;
cout << "Y-intercept: " << flush;
int b;
cin >> b;
cout << "X coordinate of interest: " << flush;
int x;
cin >> x;
int y;
y = m * x + b;
cout << "y = " << y << "when m = " << m << "; " << "b = " << b << "; x = " << x << endl;
}
edit: Sorry. Forgot to describe what was going on. lol. The program executes properly until it it comes to displaying the final line. After submitting "X coordinate of interest: " the program simply exits. I mean I am no expert in C++, but should the final cout write to the console?
And I know it is really outdated, but I really just want a platform to stand on when I begin to look at the newage languages. The book itself is only about 700 pages, and there is a LOT of explaining in it, so it is not very much code wise. I have probably 10 to 20 700 page pdfs on Java and C#/C++/C all written within the past six years. So I'll be good. Just want a starting point. :) Plus this book explains a lot about how a computer works and certain jargon that some of the newer books just don't.
This is a common windows cmd problem. Either run the program through cmd, type in the executable name, or add getchar or cin >> variable to the end of the program.
Assuming this is in Visual Studio when you run a program with debugging (F5) the console instance is closed automatically. You can either add a input line to the end of the program as others have mentioned or run the program without debugging (Ctrl+F5) and the console window will pause and let you see the output at the end of the programs execution.
I have a for loop, a very simple one, in my program, and I want it to loop through and do something for some minimum number of times. However, the loop simply...stops. But does not move on to the next thing in the program. For instance, when min is 9, it runs for i=0 to i=8, then freezes. It should exit the for loop, but it does not execute the next print instruction, nor does it execute the loop again. It just stops. The program hangs, doing absolutely nothing as far as I can tell. I don't understand why this is.
The merged.put() function I want to execute just puts x or y in merged, depending on the condition. That part works. This is just a small part of a much larger program. sp1, sp2, and merged are all defined elsewhere.
int i;
int x;
int y;
for(i=0; i < min; i++)
{
cout << " here " + convert(i);
x = sp1.get_num(i);
y = sp2.get_num(i);
if(x >= y) {
merged.put(x);
}
else {
merged.put(y);
}
cout << " end" << endl;
}
cout << "out";
EDIT: I'm not posting the entire code, it's several hundred lines long. Type of min is int. The reply down there was helpful, when << endl was added to the last print statement, it printed. My problem now appears to be here, getting stuck on the second while, because I was not incrementing i. Shame on me...thanks for the help. (This comes directly after the above code)
if (sp_large == 2) {
cout << "1" << endl;;
while (i < sp2.get_size()) {
merged.put(sp2.get_num(i));
}
}
else {
while (i < sp1.get_size()) {
merged.put(sp1.get_num(i));
}
cout << "2" << endl;
}
EDIT: Problem solved, thanks for the help.
I'm betting that it's actually a later part of the program that is hanging.
This line:
cout << "out";
just puts "out" on the output-buffer, and won't actually print "out" until the output-buffer gets flushed. (Which could happen immediately, but is not likely to.) Change that line to this:
cout << "out" << endl;
and "out" will be printed as soon as that line is run. This will help you figure out if the program is hanging before it gets to that line, or somewhere later on.
I've created a variable length array in one function, however I need to refer to this array in a second function. The problem occurs when I put the declaration above main() seeing as its length hasn't been defined yet, my compiler gets angry.
How does one typically go about this?
EDIT:
Here is my code so far.
I need to make the array's name[] midterm[] and final[] global. They're all in student_input().
#include <iostream>
using namespace std ;
void student_input();
void student_output();
int i , ns ;
main(){
int width,height,mult;
cout << "Enter the number of students:" << endl;
cin >> ns;
i = 0 ;
while( i < ns){
i = i + 1 ;
student_input();
}
i = 0 ;
while( i < ns){
i = i + 1 ;
student_output();
}
system("pause");
}
void student_input() {
int si_i,si_midterm,si_final, midterm[ns + 1], final[ns + 1];
string si_name, name[ns + 1];
cout << endl << endl << "\t----- Student " << i << " -----" << endl << endl << endl;
cout << "Enter name for student " << i << ":\t"<< endl;
cin >> si_name;
name[i] = si_name ;
cout << "Enter midterm score for student " << i << ":\t"<< endl;
cin >> si_midterm;
midterm[i] = si_midterm ;
cout << "Enter final exam score for student " << i << ":\t"<< endl ;
cin >> si_final;
final[i] = si_final ;
cout << endl << endl;
si_i = 0 ;
while (si_i < 7){
si_i = si_i + 1;
cout << "Enter lab " << si_i <<" for student " << i << ":\t"<< endl;
}
cout << name[i] << endl << midterm[i] << endl<<final[i] << endl;
return;
}
void student_output() {
cout <<"hello! "<< endl;
return;
}
C++ does not support variable length arrays; either you are not using C++ or you are using an implementation-specific language extension.
In C++ you should use std::vector for a dynamically sized array.
If you need to access it from multiple functions you can:
have the functions that need access to the vector take a reference to it as an argument, or
make the vector a class member variable and make all the functions that need to access it member functions of the class.
Which one makes more sense depends on what, exactly, you are trying to do.
What's wrong with std::vector? You cannot have a VLA in C++ (g++ provides it as an extension though).
Look into vectors, because although you can in c, in standard c++ theres no way to use variable length arrays.
Also, please look into a new IDE other than bloodshed, such as Code::Blocks, or if necessary, at least a more updated version of Dev c++ than bloodshed.
Dev-C++ has not been updated since 2005, and is not currently
maintained. The software is very buggy. At the time of my writing
there are 340 known bugs that will never be fixed.
It’s hard to get help, because the programming community have moved on to newer software.
Dev-C++ lacks features that are present in more modern solutions. Code
completion, intellisense, and proper debugging facilities (among
others) are not provided. These tools can greatly improve the
workflow and efficiency of an experienced programmer, and may aid the
learning of beginners.
Error messages and the steps required to solve
them are poorly documented compared to more modern solutions, and
because most programmers have moved on from Dev-C++ it can be
difficult (if not impossible) to find anyone who is able to help you.
Some problems may not be able to be solved at all. The compiler
included with Dev-C++ is very out-dated, and buggy. An out-dated
compiler can result in buggy and inefficient code, and may be damaging
to the learning process for a beginner.
The provided “devpack” system
is no longer supported by modern libraries. Using external libraries
in Dev-C++ can be a confusing and difficult process for beginners who
are expecting this simple system to handle it for them.
Note: the beginner things aren't directly pointed at you, this was taken from here: http://clicktobegin.net/programming/why-you-shouldnt-use-dev-c/