Reputation: 1308
I'm getting a crash on runtime when trying to execute a simple program that cout's a string object, i'm using Borland C++ Compiler version 5.5 with the following code:
#include <iostream>
#include <string> // Usaremos as funcoes mais modernas de String em C++
#include <conio.h>
using namespace std;
// <Prototipos >
int MenuPrincipal(void);
void DesenharCentralizado(string String, int CoordY);
// </Prototipos>
int main() {
while(MenuPrincipal() != 0); // Para sair, ele deve retornar 0
return 0;
}
int MenuPrincipal(void) {
string Titulo = "Agenda";
clrscr();
DesenharCentralizado(Titulo, 4);
getch();
return 0;
}
void DesenharCentralizado(string Frase, int CoordY) {
int PosX=wherex(), PosY=wherey();
gotoxy((80-Frase.length())/2, CoordY);
cout << Frase; // XXX CRASH
gotoxy(PosX, PosY);
}
PS: Please don't complain about using old conio.h and these things, these are for my C++ class and my teacher is teaching us to use it first…
Upvotes: 1
Views: 3367
Reputation: 145279
Borland 5.5. only had partial support for std::string
.
It may well be that your code is to blame in this particular case, but even so, you will not get anywhere (and you will get nowhere very fast) trying to use std::string
with that compiler.
If your teacher requires you to use Borland 5.5, then your teacher is trying to teach himself or herself as he/she is teaching you. That approach is sometimes OK, but send your teacher here. Now, to wingleader’s teacher:
Borland 5.5 is a broken tool. It is not just pre-standard: it is broken. Students will not learn anything positive from using it, just like trying to learn to play the piano on a piano that is grossly out of tune.
If you must support fifteen year old computers (or older), then use maybe g++ 2.95 or Visual C++ 6.0. Otherwise, use free modern tools such as (as of 2012) MinGW g++ 4.6 or newer, or Visual C++ 10.0 or newer. The new compilers (although not the Visual Studio Express IDE) run well even on a computer with just 265 MB RAM, which as of 2012 includes some ten years old PCs. Code::Blocks is a good IDE for old Windows computers.
Upvotes: 1
Reputation: 137830
If you comment out everything in the file and replace it with
#include <iostream>
int main() { std::cout << "Hello, world!"; }
does that work? If yes, then try
#include <iostream>
#include <string>
int main() { std::cout << std::string( "Hello, world!" ); }
With the removal of <conio.h>
and calls to its functions, your program is simple enough to indicate a broken toolchain, and adding features one at a time might help track down what's broken. It could be an incorrect runtime library version, or some kind of corruption in the installation of Borland or the project files.
By re-creating the project one step at a time, either you will track down the cause or you will end up with a working project, at which point you can forget about the problem.
Upvotes: 2