Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Someone help!
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 26th October 13:41
External User
 
Posts: 1
Default Someone help!



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[i].name<<' '<<phone_book[i].number<<'\n'<<"I
Passed";
}
  Reply With Quote


 


2 26th October 13:41
thomas tutone
External User
 
Posts: 1
Default Someone help!



#include <cstdlib>

#include <iostream>

#include <string>


change that to:
string name;

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

Change that to:

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

(BTW, that's a very strange name.)


Best regards,

Tom
  Reply With Quote
3 26th October 13:41
phlip
External User
 
Posts: 1
Default Someone help!


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++!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
  Reply With Quote
4 26th October 13:41
External User
 
Posts: 1
Default Someone help!


seems not hit the problem!
  Reply With Quote
5 26th October 13:44
andy terrel
External User
 
Posts: 1
Default Someone help!


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[i].name<<' '<<phone_book[i].number<<'\n'<<"I
Passed";
}

------------------

You can't copy strings like that, once they have been initialized.
Also there is no reason to call define the phonebook[3]. It's already
been defined.
  Reply With Quote
6 26th October 13:44
mike wahler
External User
 
Posts: 1
Default Someone help!


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
  Reply With Quote
7 26th October 13:44
alan johnson
External User
 
Posts: 1
Default Someone help!


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.

--
Alan Johnson
  Reply With Quote
8 26th October 13:44
andy terrel
External User
 
Posts: 1
Default Someone help!


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[i].name<<' '<< phone_book[i].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;
}
  Reply With Quote
9 26th October 13:45
gavin deane
External User
 
Posts: 1
Default Someone help!


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/com...7e988ec38576f6

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

Gavin Deane
  Reply With Quote
10 30th October 14:32
jack klein
External User
 
Posts: 1
Default Someone help!


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
  Reply With Quote
Reply


Thread Tools
Display Modes




666