A programme that doesn't get compiled in C++

  • C/C++
  • Thread starter MathematicalPhysicist
  • Start date
  • Tags
    C++
In summary: I can use some kind of template code for it.In summary, the conversation is about a code for operations on polynomials in C++ that is causing errors. The code is copied from a book on solving PDEs in C++ and also includes a template class for list. The code appears scattered in the book and the person is unsure about how to fix the errors, including a declaration error for 'class <T>'. The person also added the math library thinking it would include max and min functions, but they are actually in <algorithm>.
  • #36
MathematicalPhysicist said:
@Mark44 thanks.
Btw, do you happen to know why when I am executing a source file in visual C++ 2015 community the window of the .exe file appears and disappears without me pressing any key to exit from the programme?

The Debug gives me the following lines:
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Symbols loaded.
The program '[6836] Project6.exe' has exited with code 0 (0x0).

I picked an empty project file and as a source file a cpp file, what am I do wrong here that the window exits automatically without me pressing any key?

Drives me nuts...
When you start the program by clicking the exe's icon, your program opens a command window, runs, and then closes that window. There are a couple of things you can do to keep that window open:
1. Open a separate command window, and CD (change directory) to the directory that contains your executable. Then type the name of the executable, which starts to run in the command window you have opened.
2. Add in input statement at the end of your main() function, such as cin >> ch; (You need to also declare ch as type char.) The command window will stay open until you type a character, and will then close.

Edit: the above should be std::cin >> ch;

The lines you show above aren't produced by the debug window, I don't believe. They are produced by the Output window. You can ignore all of them.
 
Last edited:
Technology news on Phys.org
  • #37
Mark44 said:
When you start the program by clicking the exe's icon, your program opens a command window, runs, and then closes that window. There are a couple of things you can do to keep that window open:
1. Open a separate command window, and CD (change directory) to the directory that contains your executable. Then type the name of the executable, which starts to run in the command window you have opened.
2. Add in input statement at the end of your main() function, such as cin >> ch; (You need to also declare ch as type char.) The command window will stay open until you type a character, and will then close.

The lines you show above aren't produced by the debug window, I don't believe. They are produced by the Output window. You can ignore all of them.
@Mark44 I get an error "'cin' undeclared identifier" on line 25.
C++:
#include <iostream>
#define N 3
void main()
{
    char ch;
    int i;
    int *u, *v, w;
    u = new int[N + 1];
    u[1] = 4; u[2] = -5; u[3] = 3;
    std::cout << std::endl << "Vector u:" << std::endl;
    for (i = 1; i <= N; i++)
        std::cout << u[i] << " ";
    std::cout << std::endl;
    v = new int[N + 1];
    v[1] = -2; v[2] = 3; v[3] = 7;
    std::cout << "Vector v:" << std::endl;
    for (i = 1; i <= N; i++)
        std::cout << v[i] << " ";
    std::cout << std::endl;
    w = 0;
    for (i = 1; i <= N; i++)
        w += u[i] * v[i];
    std::cout << "the product w=u.v is " << w << std::endl;
    delete u, v;
    cin >> ch;
}
 
  • #38
BTW @Mark44 what exactly should I do in your first option you've given me? (I remembered how to do it in java but forgotten).
 
  • #39
MathematicalPhysicist said:
@Mark44 I get an error "'cin' undeclared identifier" on line 25.
Replacing "cin" with "std::cin" should fix that.
 
  • #40
MathematicalPhysicist said:
BTW @Mark44 what exactly should I do in your first option you've given me? (I remembered how to do it in java but forgotten).
Open a command window:
Start --> Run, then type cmd
When the command window opens, change directories (CD) to the directory that contains your executable (the .exe file).
Type the name of the exe (you don't need to add the .exe file extension, and then hit Enter.

MisterX said:
Replacing "cin" with "std::cin" should fix that.
Yes, I neglected to add "std::" in what I wrote. Thanks for correcting me.
 
  • #41
MathematicalPhysicist said:
So you tell me to ditch this book, and the other book. any other recommendations on books, perhaps "numerical recipes"?

The best advice to give you depends on why you're doing this and what you're ultimately hoping to accomplish. You haven't explained that yet.

So far, this thread seems to consist of you:
  1. Copying hundreds of lines of C++ code seemingly at random from books (of questionable quality, at that).
  2. Asking others to fix even the most basic syntax errors for you when it doesn't compile.
  3. Asking all your questions here, even those that a simple Google search could answer much faster.
I'd really like to see an explanation here. What are you trying to accomplish that you think this pattern is the best way to go about it?
 
Last edited:
  • Like
Likes D H and Mark44
  • #42
@Mark44 why does the programme exits the command line so quickly? in dev C++ it didn't do that.
 
  • #43
wle said:
The best advice to give you depends on why you're doing this and what you're ultimately hoping to accomplish. You haven't explained that yet.

So far, this thread seems to consist of you:
  1. Copying hundreds of lines of C++ code seemingly at random from books (of questionable quality, at that).
  2. Asking others to fix even the most basic syntax errors for you when it doesn't compile.
  3. Asking all your questions here, even those that a simple Google search could answer much faster.
I'd really like to see an explanation here. What are you trying to accomplish that you think this pattern is the best way to go about it?
@wle I want to teach myself using C++ to write programmes to solve numerically PDEs and integral equations for my research, this is why I used such books with those titles.
Do you have a recommendation?
 
  • #44
MathematicalPhysicist said:
@Mark44 why does the programme exits the command line so quickly? in dev C++ it didn't do that.
I don't have dev C++ so can't speak to its behavior. If you run your program from Windows (and possibly from Linux), the OS opens a command window and your program starts running. When the program is finished, it returns to the caller (Windows or Linux) and closes the command window.
 
  • #45
MathematicalPhysicist said:
@Mark44 why does the programme exits the command line so quickly? in dev C++ it didn't do that.
It did, it's just doing something else that you aren't aware of that I'm surprised hasn't been mentioned.

Ok, so when your program gets run, it starts in the main() function, and it runs through line by line until it gets the the end. It then IMMEDIATELY gets completed and removed by the OS.

Dev-C++ is doing something different. It's not meant to run your program, it's meant to help you create it. When you tell Dev-C++ to run your code, what you are actually telling it to do is to DEBUG your program. It wraps your program in a gbd process, and that process is the one that stays open in Dev-C++.

I had the same question about ten years ago. :) BTW, Dev-C++ is very old and rotted. I recommend changing to Code::Blocks, it'll even import the Dev-C++ file and upgrade it for you.
 
Last edited:
  • #46
MathematicalPhysicist said:
@wle I want to teach myself using C++ to write programmes to solve numerically PDEs and integral equations for my research, this is why I used such books with those titles.
Do you have a recommendation?

You should concentrate on learning (at least) the basics of C++ first before trying to use it for numerical programming. (If you're asking questions like what cout is and where to put closing braces and std::s, you don't know the basics.) D H already pointed out some links to get you started with that.

It's reasonable that you'd want to copy existing code or use an available numerical library rather than waste time reinventing the wheel, but you'll still have to understand the features of the C++ language that such libraries use. For example, if a numerical library is provided in the form of a collection of template functions and classes, you're going to struggle to use it (at best) if you're not already familiar with C++ templates and classes. Chances are no library is going to do everything for you (otherwise it would be a complete working program and not a library), so you'll probably have to do some of your own programming anyway.

One specific recommendation I'll make: don't copy/paste code while you're learning C++ (or any other programming language or technique, for that matter). Learning C++ will probably involve copying and trying out code examples -- you should actually type these out yourself. This will slow you down enough that you can think about and better internalise what the code does as you type it, as well as get you used to the language's syntax.

NB: C++ is a very complicated language to learn and use and if you start learning it you shouldn't expect to immediately be able to use it for numerical work. If you're pressed for time you may be better off using something else (e.g. Fortran, Python, or even just Matlab) instead.
 

Similar threads

Replies
31
Views
2K
Replies
36
Views
2K
Replies
5
Views
2K
Replies
23
Views
2K
Replies
35
Views
3K
Replies
17
Views
2K
Replies
89
Views
5K
Replies
25
Views
2K
Replies
8
Views
2K
Back
Top