I am having trouble with this program. I'm not so familiar with the functions strcpy
and strcmp
.
Can any professionals help me or give me some advice?

- 43
- 2
- 6

- 1
- 4
-
7Since you tagged this C++, my advice would be to avoid all the problems by using `std::string`. – juanchopanza Nov 25 '13 at 16:02
-
2What are you trying to accomplish? These functions are rarely used in C++, is there any particular reason you feel you should use them? `std::string` is way easier to work with ... – unwind Nov 25 '13 at 16:03
-
1What's the issue? What's driving you crazy? – theAlias Nov 25 '13 at 16:03
-
All parameter are highlighted by c++ and i hv no idea to deal with them.Before this i set two arrays with some data for testing, it can work.However, when i use them in this case,it doesnt work,so i think its the problem of strcpy – user3018702 Nov 25 '13 at 16:06
-
1@user3018702 I think you are almost certainly wrong. – john Nov 25 '13 at 16:06
-
1Where is inputinput defined (is it global)? Are you sure that name and sid which are passed in will fit in your allocated buffers? – Dweeberly Nov 25 '13 at 16:06
-
1Why do you keep redeclaring `strcpy`? If you want to call it, then call it: `strcpy(id, sid.c_str());`. (Of course, you actually want to use `std::string` and not call it at all.) – Mike Seymour Nov 25 '13 at 16:07
-
@user3018702 For instance, why is `name` an array of strings? Surely a name is a string not an array of strings. Until you sort out what should be an array and what should not the program is never oging to work. – john Nov 25 '13 at 16:07
-
i hv not idea when i facing it TT coz i m not familiar with c++ – user3018702 Nov 25 '13 at 16:08
-
string sid[] and string name[] are the arrays that for storing a list of name and a list of student id – user3018702 Nov 25 '13 at 16:08
-
@user3018702 OK fair enough, plurals would have made easier to understand. – john Nov 25 '13 at 16:09
-
still cant work when using std::string... – user3018702 Nov 25 '13 at 16:13
-
1@user3018702 std:string is the easy way, if you can't get it to work then post the code using std::string, don't give up and try the harder way. – john Nov 25 '13 at 16:17
-
5Judging by all the failed attempts in the code, you need to do more reading and thinking, but less guessing and typing. – molbdnilo Nov 25 '13 at 16:21
-
If you're "mad on strcmp and strcpy", stop using them and start making a new function little by little, while checking that the code actual does what you think by looking at the documentation, printing out enough and by using a debugger. Also, choose names that actually make sense so they are clear for you and to those who are trying to help. – stefaanv Nov 25 '13 at 16:34
2 Answers
As others have stated, choose either C-style strings or C++ std::string
.
I highly recommend not creating a separate function for searching the string array because passing arrays to functions is difficult for beginners.
Try something like:
#include <string>
#include <iostream>
#define MAX_NAMES 16
int main(void)
{
std::string name_container[MAX_NAMES];
unsigned int names_in_container = 0;
while (1)
{
std::string name_from_user;
std::cout << "Enter name: ";
if (!getline(std::cin, name_from_user))
{
break; // Exit from the "while" loop
}
// Search the name container for the name.
unsigned int array_slot = 0;
bool name_found = false;
for (array_slot = 0; array_slot < names_in_container; ++array_slot)
{
if (name_from_user == name_container[array_slot])
{
std::cout << "\nName exists in slot " << array_slot << "\n";
name_found = true;
}
}
if (!name_found)
{
if (array_slot >= MAX_NAMES)
{
std::cout << "Name container full, cannot add name.\n";
break;
}
else
{
name_container[names_in_container] = name_from_user;
++names_in_container;
}
}
}
}

- 14,072
- 2
- 31
- 53

- 56,849
- 17
- 98
- 154
-
-
@user3018702: ironically written in a peculiar language. Anyway, the code has 3 parts: getting input from the user, looking in the container of names and adding the name if it's not in there yet. So it's similar but not the same as what you tried to accomplish. – stefaanv Nov 26 '13 at 08:05
Please recognize that you are comingling C++ string (std::string) and the C library strcpy and strcmp functions. They are two different things. You probably need to look at some of the methods available under the std::string class (.c_str, =, ==),
Here are a few comments, and suggestions,
void search (string sid[], //do you intend to pass a string sid, or an array of srings?
string name[], //do you intend to pass a string name, or an array of strings?
int No_of_data_input)
{
char id[100];
string input;
char namename[100];
//comments ignored/removed
//these two lines declare the function strcpy
char * strcpy ( char * namename, const char * name );
char * strcpy ( char * id, const char * sid );
//do you intend to copy name and sid instead?
//do you want: strcpy(namename, name[i].c_str() );
//do you want: strcpy(id, sid[i].c_str() );
//if so, you want to do this inside your loop on i, below,
int sameName=0;
int j=0;
cout<<"enter id or name";
getline(cin,input); //you probably want: cin >> input;
//do you intend to declare the function strcpy yet again?
char * strcpy ( char * inputinput, const char * input );
//or do you intend to copy input to some char[]?
//you probably want: strcpy( inputinput, input.c_str() );
//you probably want number of elements in sid[] or name[]?
for(int i=0;i<4;i++){
//you probably want sid[i] here
if ((strcmp(inputinput,id[i])==0) || (strcmp(inputinput,name[i])==0)){
//you could rewrite this as:
//if( (input == id[i]) || (input == name[i]) ){
sameName++;
j=i;
} //do us a favor
} //make the ending braces clearer
cout<<sameName;
if (sameName==0)
cout<<"No student found: ";
else if (sameName==1)
cout<<"student found: "<<name[j]<<endl<<id[j];
else if (sameName>1)
cout<<"More than one student found,please enter id";
}
Do you want (or need) to comingle std::string and strcpy, strcmp?
Editing your code, you might want to just use std::string,
void search (string sid[], //do you intend to pass an array of srings?
string name[], //do you intend to pass an array of strings?
int count)
{
string input;
int same=0;
int j=0;
cout<<"enter id or name";
cin >> input;
//you probably want number of elements in sid[] or name[]?
for(int i=0;i<count;++i){
if( (input == sid[i]) || (input == name[i]) ){
++same;
j=i;
}
}
cout<<"same: "<<same<<endl;
if (same==0)
cout<<"No student found: "<<endl;
else if (same==1)
cout<<"student found: "<<name[j]<<","<<id[j]<<endl;
else if (same>1)
cout<<"More than one student found,please enter id"<<endl;
}
Maybe you could use a vector of strings? Then you could use an iterator on the vector.
You may have more success if you define a student record (containing sid and name), and pass a vector of student records. Then use an iterator over that vector -- read about vector and iterators.

- 4,360
- 2
- 24
- 42