string problem

Hi guys i m facing a problem and i need your kind help. I m using Borland Turbo C++ IDE version 3.0. While i try to compile the following code its shows an error massage like EXPRESSION SYNTAX.
1
2
3
4
#include<iostream.h>
void main()
{ char name[]=''Albert Einstein'';
cout<<name[]; }
Last edited on
probably because you have '' instead of " there is a difference you know...
Also it should be a const char and you should also put a size + 1 for the size of the char since the string ends in a null terminating character.
const char name[ 16 ] = "Albert Einstein";

You notice the difference between a quotation mark ( " ) and an apostrophe ( ' ) now or in your case two apostrophe's ( '' ).

Also there is this neat thing called the tab key you should use it to indent.
Or just leave the code how it was when you hit the enter key after { :P
By the way Borland is a very outdated compiler imo

*Edit
by the way ALL c/c++ programs MUST contain int main() You have { } but not int main try adding that then compiling also I don't think you can output like that in c although I have never tried it. I think the correct form would be name not name[] or are you trying to output the first character? that would be name[ 0 ];

*edit and btw you are not creating a string you are using c not c++ it is just an array of characters or a list of pointers that point to different characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//c
#include <iostream.h>
int main()
{
    const char name[ 16 ] = "Albert Einstein";
    std::cout << name << std::endl;
}

//c++
#include <iostream>
int main()
{
    const std::string name = "Albert Einstein"; //this one you don't need to put const if you don't want I suppose if you want to edit it...
    std::cout << name << std::endl;
}
Last edited on
Thanks a lot @Giblit. Ya I made a little mistake. Square braces should not be there with (cout<<name[]) I removed it, now the code compiled successfully. I even experiment with different options you suggest. All these works well. I also discovered that std is not required with every stream whether i mention 'using namespace std' or not(at least its works in my compiler).
1
2
3
4
5
6
7
 #include<iostream.h>
void main()
{
char name[]=''Albert Einstein'';
cout<<name<<endl;
cout<<name[0];
} 
Probably because you have a really bad compiler.
a) it lets you compile with void main. It is int main.
b) lets you compile a const char array only being declared as char and not const.
c) lets you create a "c-string" with two apostrophe's instead of a quotation mark. I have no idea why you would even want to do that..

*c) unless for some really weird reason the inventor of c thought it'd be cool to use ' ' vs ".

I would still recommend std::cout / std::endl even if it compiles without it.

Just because you can jump off a bridge doesn't mean you should...
Just because you can shoot yourself doesn't mean you should...
I appreciate what u mean. I know my compiler is outdated n m going to change it with code::blocks. I need your opinion.
Well code::blocks is an ide which is made up of a debugger , text editor, and compiler. The actual compiler that comes with code::blocks is g++( mingw if windows) gcc which is made by GNU. I like code::blocks that is what I use majority of the time.
@giblit

Also it should be a const char and you should also put a size + 1 for the size of the char since the string ends in a null terminating character.
const char name[ 16 ] = "Albert Einstein";


You are wrong. There is no any need to define the array as having qulifier const. it can be non-const array. Also there is no need to specify explicitly the size of the array because the compiler will deduce the size of the array itself.
So it si enough to enclose tsimply he string literal in double quotes.

char name[] = "Albert Einstein";

And it si C++ not C because C has no header <iostream.h> and such streams as cout.

SOrry I was thinking it said char* for some reason.
*edit also I thought the .h headers were c not c++? Are they just older headers or what?
Last edited on
The C++ Standard

For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, as shown in Table 154.

Table 154 — C headers
<assert.h> <inttypes.h> <signal.h> <stdio.h> <wchar.h>
<complex.h> <iso646.h> <stdalign.h> <stdlib.h> <wctype.h>
<ctype.h> <limits.h> <stdarg.h> <string.h>
<errno.h> <locale.h> <stdbool.h> <tgmath.h>
<fenv.h> <math.h> <stddef.h> <time.h>
<float.h> <setjmp.h> <stdint.h> <uchar.h>
Topic archived. No new replies allowed.