Cout << string; = memory error?

Hi i have been coding in C for a couple of months and recently moved to c++

here is where the fun starts

if i code something like:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

using namespace std;


int main()
{
    string word1  = "Game" ;
    cout << "string" << word1;
  return 0;
}


it gives me the typical memory error... like everytime i try to cout a string on my monitor...!

but if i code it in good old c code like

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

using namespace std;


int main()
{
    char word1[20];
    printf("Insert a string\n");scanf("%s", word1);
    printf("The string is ---> %s", word1);
}


it works....
wierd no?? is it normal??

i compile the first code with both borland c++ compiler and MingW and guess what.. both give me the memory fault thing..

i know this is a noob question and i try not to ask too many unless i need to.. help me please
It works fine for me.

Are you sure you are compiling with the C++ complier and not C?
Yes i am i compile it , and i have run succefully other small apps ive made in c++ however when i use cout << followed by a string it always give me error when i run the .exe file
Show me the command you use to compile it with GCC.

I'm using GCC 4.3 and compiled it with:
g++ -Wall -pedantic a.cpp


I haven't tested it against Borland yet, but if you like I can.

It sounds to me like something is mis-configured on your end, because the example you posted is correct in every sense. (Though in your second example I think you meant to #include <cstdio> ;-) .)

[edit] Also, tell me how and where you installed the GCC, and if you are using an IDE, which one, and if not, check that the MinGW base directory (probably C:\MinGW) is in your PATH.
Last edited on
YAY youre right Douas i tried to compile it manually (trho command line) like you did and the program worked..

Well the IDE im using is codeblocks (prolly the latest version of it) dont know what is wrong thers nothing i can tell from the compiler/debugger settings that may be causing this errors..

any ideas? or suggestions? (like give up on that IDE it sucks... im always open to suggestions :P)

if not ill try to find whats wrong when i get home later..

thank you
Ok i reseted the compiler settings (i froggot how mutch i messed with it befoure) and it works 100% well now :) thanks again
Topic archived. No new replies allowed.