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.
#include <iostream>
#include <string.h>
usingnamespace 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)'
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
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.
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:
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.
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
#include <iostream>
#include <string>
usingnamespace 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>.
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!!