Someone help!

C

csshine

I composed below code but can not pass compiling. Could some one help
this?
------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

using namespace std;

struct Entry{
char name[15];
int number;
};

Entry phone_book[1000];

inline void print_entry(int i) ;

int main()
{
const char phone_book[3].name[15]="I Love You!"; //I want to
assign strings to a char array but fail :(

print_entry(3);

system("PAUSE");
return 0;
}

void print_entry(int i) //simple use
{
cout<<phone_book.name<<' '<<phone_book.number<<'\n'<<"I
Passed";
}
 
T

Thomas Tutone

I composed below code but can not pass compiling. Could some one help
this?

Make that:

#include said:
#include <stdio.h>

Make that:

#include said:
#include <string.h>

Make that:

#include said:
using namespace std;

struct Entry{
char name[15];

change that to:

string name;
int number;
};

Entry phone_book[1000];

inline void print_entry(int i) ;

Delete the word "inline" from the above - it serves no purpose unless
you actually define the function here, which you don't.

int main()
{
const char phone_book[3].name[15]="I Love You!"; //I want to

Change that to:

phone_book[3].name="I Love You!";

(BTW, that's a very strange name.)
assign strings to a char array but fail :(

print_entry(3);

system("PAUSE");
return 0;
}

void print_entry(int i) //simple use
{
cout<<phone_book.name<<' '<<phone_book.number<<'\n'<<"I
Passed";
}


Best regards,

Tom
 
P

Phlip

Thomas said:
Make that:

#include <cstdlib>

That advice is not automatically correct, for all the real-world compilers
and situations out there.
Make that:

#include <iostream>

Always. <stdio.h> truly has nothing left in it that anyone should use in
C++!
 
C

csshine

seems not hit the problem!
That advice is not automatically correct, for all the real-world compilers
and situations out there.


Always. <stdio.h> truly has nothing left in it that anyone should use in
C++!
 
A

Andy Terrel

try:

#include <stdlib.h>
#include <iostream>
#include <string.h>

using namespace std;

struct Entry{
char name[15];
int number;

};

Entry phone_book[1000];
inline void print_entry(int i) ;


int main()
{
strcpy(phone_book[3].name, "I Love You!"); //I want to assign
strings to a char array but fail :(

print_entry(3);

system("PAUSE");
return 0;

}

void print_entry(int i) //simple use
{
cout << phone_book.name<<' '<<phone_book.number<<'\n'<<"I
Passed";
}
 
M

Mike Wahler

I composed below code but can not pass compiling. Could some one help
this?
------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

using namespace std;

struct Entry{
char name[15];
int number;
};

Entry phone_book[1000];

inline void print_entry(int i) ;

int main()
{
const char phone_book[3].name[15]="I Love You!"; //I want to
assign strings to a char array but fail :(

Several problems here. First you have a syntax error. Second,
arrays cannot be assigned to. Use 'strcpy()' or 'strncpy()'
from the C library or 'std::copy' from the C++ library.
Third, 'name[15]' is out of range for the array. Its valid
indices are from zero to 14. Also, name[index] is not an
array or a string, it's a single character.


strcpy(phone_book[3].name, "I Love You!");

Finally, since this is C++ why not use the C++ library's
std::string and std::vector types? Then you needn't worry
about overrunning arrays, and you can use an assignment operator
with a string.

#include <string>
#include <vector>

struct Entry
{
std::string name;
int number;
};

int main()
{
std::vector<Entry> phone_book(1000);

phone_book[3].name = "I Love You!";
return 0;
}


/* (not compiled or tested) */

-Mike
print_entry(3);

system("PAUSE");
return 0;
}

void print_entry(int i) //simple use
{
cout<<phone_book.name<<' '<<phone_book.number<<'\n'<<"I
Passed";
}
 
A

Alan Johnson

Phlip said:
That advice is not automatically correct, for all the real-world compilers
and situations out there.

Perhaps not, but I would still call it good advice. The use of
stdlib.h has been deprecated by the standard for nearly eight years.
 
A

Andy Terrel

Yeah I agree with using the stl (a more C++ not C approach) and all but
it if you really want to control the memory yourself. The char array
is nice. Just to complete the code so it actually prints still.

#include <string>
#include <vector>
#include <iostream>

struct Entry
{
std::string name;
int number;

};

inline void print_entry(int i, std::vector<Entry> phone_book)
//simple use
{ std::cout << phone_book.name<<' '<< phone_book.number<<'\n'<<"I
Passed\n"; }

int main()
{
std::vector<Entry> phone_book(1000);

phone_book[3].name = "I Love You!";
print_entry(3,phone_book);
return 0;
}


Mike said:
I composed below code but can not pass compiling. Could some one help
this?
------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

using namespace std;

struct Entry{
char name[15];
int number;
};

Entry phone_book[1000];

inline void print_entry(int i) ;

int main()
{
const char phone_book[3].name[15]="I Love You!"; //I want to
assign strings to a char array but fail :(

Several problems here. First you have a syntax error. Second,
arrays cannot be assigned to. Use 'strcpy()' or 'strncpy()'
from the C library or 'std::copy' from the C++ library.
Third, 'name[15]' is out of range for the array. Its valid
indices are from zero to 14. Also, name[index] is not an
array or a string, it's a single character.


strcpy(phone_book[3].name, "I Love You!");

Finally, since this is C++ why not use the C++ library's
std::string and std::vector types? Then you needn't worry
about overrunning arrays, and you can use an assignment operator
with a string.

#include <string>
#include <vector>

struct Entry
{
std::string name;
int number;
};

int main()
{
std::vector<Entry> phone_book(1000);

phone_book[3].name = "I Love You!";
return 0;
}


/* (not compiled or tested) */

-Mike
print_entry(3);

system("PAUSE");
return 0;
}

void print_entry(int i) //simple use
{
cout<<phone_book.name<<' '<<phone_book.number<<'\n'<<"I
Passed";
}
 
G

Gavin Deane

Alan said:
Perhaps not, but I would still call it good advice. The use of
stdlib.h has been deprecated by the standard for nearly eight years.

Unfortunately, many popular compilers incorrectly implement the <cxxx>
headers to put C library names in _both_ the std namespace and the
global namespace. As well as adding nothing in terms of namespace
protection, it means that this program usually compiles

#include <cstdio>
int main()
{
printf("Hello world\n"); // missing std:: prefix
}

The compiler doesn't catch the error and in a less trivial program I
might not catch it either.

Unless and until compilers start to widely implement <cxxx> headers
correctly, I prefer to use <xxx.h>. Though technically deprecated, this
program will compile and do what I intend on any conforming compiler.

#include <stdio.h>
int main()
{
printf("Hello world\n");
}

As a practical matter, I can't believe <xxx.h> headers will move from
deprecated to excluded from the language while correct implementation
of <cxxx> headers is so sparse.

Also see P J Plauger's comments in this thread
http://groups.google.co.uk/group/co...+and+standard+headers&rnum=1#327e988ec38576f6

or, while the link lasts
http://tinyurl.co.uk/o2im

Gavin Deane
 
J

Jack Klein

That advice is not automatically correct, for all the real-world compilers
and situations out there.


Always. <stdio.h> truly has nothing left in it that anyone should use in
C++!

You think so? What else does the C++ library provide to replace:

rename()?
remove()?
tmpfile()?
tmpnam()?
FILENAME_MAX?

Some of the other types and macros are arguable, but the four
functions and one macro above can be very, very useful in C++
programs.
 
P

Phlip

Jack said:
You think so? What else does the C++ library provide to replace:

rename()?
remove()?
tmpfile()?
tmpnam()?
FILENAME_MAX?

Okay. Everything that should have been in <stdlib.h> is still useful. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top