strcmp help

ok I wrote the code I was to do and my teacher wants us to use a strcmp quit and my program was working great but if I add this function I get some errors. I'm not asking for anybody to do my home work I'm just needing some help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include <iostream>
#include <string.h>

using namespace std;

struct  Person {
	string name;
	int age;
	float salary;


};

int main(){



	Person record[10];
    int n;
    int i;
    n=i;
	for ( i = 0; i < 10; i++)
    {
        cout << "Enter  name: ";
		cin >> record[i].name;

		if( strncmp(record[i].name, "quit" ) == 0)

                   break;
		cout << " Enter salary: ";
		cin >> record[i].salary;
		cout << "\n";
    }

     n = i;
	for (i = 0; i < (10-1); i++)
	{
	    for ( n = (i +1); n < (10); n++)
        {
            if (record[i].salary < record[n].salary)
            {

                Person temp;
                temp=record[i];
                record[i]=record[n];
                record[n]=temp;
            }
        }
    }
        for (int i = 0; i < 10; i++)
	{
        cout << "Name: " << record[i].name;
		cout << "\nSalary: " << record[i].salary << endl << endl;
    }


	return 0;

}





**** the error code I keep getting is:
C:\Users\April\Documents\PROG02.cpp:28:38: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int strncmp(const char*, const char*, size_t)'
strcmp() operates on C-style strings; if you really must use it instead of the standard string comparison operators, you will need to use the c_str() method.
http://www.cplusplus.com/reference/string/string/c_str/
ok so now I am really confused because my teacher why would my teacher tell me to do that when I'm in c++ I have no clue about c-style strings. he just wants us to use to quit the program before I added all that my program worked fine. So there no way I could use this for my program that I have now
The simplest way to code that would be


if ( record[i].name == "quit" )

Perhaps your teacher got mixed up - sometimes if one is used to using several different coding languages, one may absent-mindedly drop something from one language into another where it really doesn't belong. I'd suggest maybe it was the intention, rather than the low-level detail which was important.
Ok i did what you said but now when i run my code its doing some crazy output.
Is the idea to sort the number of records that you input? If that's true then you need to remember that number somewhere. If I run the program and give it 10 names and salaries it works for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string.h>

using namespace std;

struct Person
{
    string name;
    int age;
    float salary;
};

int
main()
{

    Person record[10];
    int n;
    int i;

    for (i = 0; i < 10; i++) {
        cout << "Enter  name: ";
        cin >> record[i].name;

        if (record[i].name == "quit")
            break;
        cout << " Enter salary: ";
        cin >> record[i].salary;
        cout << "\n";
    }

    for (i = 0; i < (10 - 1); i++) {
        for (n = (i + 1); n < (10); n++) {
            if (record[i].salary < record[n].salary) {

                Person temp;
                temp = record[i];
                record[i] = record[n];
                record[n] = temp;
            }
        }
    }
    for (int i = 0; i < 10; i++) {
        cout << "Name: " << record[i].name;
        cout << "\nSalary: " << record[i].salary << endl << endl;
    }

    return 0;

}

That's because you are printing all 10 elements of array but what if user entered data for e. g. 2 persons only? Same thing goes for sorting, you don't need (want) to sort all 10 elements if there are only 2 persons in array... You need to add some counter to count for how many persons you have data.
Also I think that there is no need for n = i; lines, especially for the first one because i is uninitialized at that moment.
Last edited on
How would i add a counter and the reson i have n=i is because that i was my instructor told me to do let me get to my computer so i can upload the code that i had before i put that quit function in
This is my code before I did what my teacher told me to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string.h>

using namespace std;

struct  Person {
	string name;
	int age;
	float salary;
};

int main(){


	Person record[10];

	for (int i = 0; i < 10; i++)
	{
		cout << "Enter  name: ";
		cin >> record[i].name;
		cout << " Enter salary: ";
		cin >> record[i].salary;
		cout << "\n";

	}

	for (int i = 0; i < (10-1); i++)
	{
	    for (int j = (i +1); j < (10); j++)
        {
            if (record[i].salary < record[j].salary)

                {

                Person temp;
                temp=record[i];
                record[i]=record[j];
                record[j]=temp;
                }
            }
        }
      for (int i = 0; i < 10; i++)
	{
        cout << "Name: " << record[i].name;
		cout << "\nSalary: " << record[i].salary << endl << endl;


	}

	return 0;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>

using namespace std;

struct  Person {
	string name;
	int age;
	float salary;
};

int main(){
	Person record[10];
    int n;
    int i;
    //n=i;

    int counter = 0;
    for (i = 0; i < 10; i++)
    {
        cout << "Enter  name: ";
	cin >> record[i].name;
	if(record[i].name == "quit")
            break;
	cout << " Enter salary: ";
	cin >> record[i].salary;
	cout << "\n";
        counter++;
    }
    //n = i;
    for (i = 0; i < counter - 1; i++)
    {
        for (n = (i + 1); n < counter; n++)
        {
            if (record[i].salary < record[n].salary)
            {
                Person temp;
                temp=record[i];
                record[i]=record[n];
                record[n]=temp;
            }
        }
    }
    for (int i = 0; i < counter; i++)
    {
        cout << "Name: " << record[i].name;
	cout << "\nSalary: " << record[i].salary << endl << endl;
    }
    return 0;
}


Btw you should avoid magic numbers (in bigger programs).
I. e. it's always better to write something like numOfElem - 1 than (10 - 1).
<string.h> (<cstring>) is header for C-strings and functions like strcmp, strcpy...
For C++ strings is <string>.
Last edited on
I understand the magic numbers thing I just wish I understood the strcmp better. My program now works thank yall so much!!! I really do appreciate all of yalls help!!
Topic archived. No new replies allowed.