trying to get my remove name function to work but i just doesn\'toutput or it ou
ID: 3609766 • Letter: T
Question
trying to get my remove name function to work but i just doesn'toutput or it outputs everything.this is my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
/*
* Prototypes -
*/
void sortReverse(int n,string namelist[]);
int remove(int n, string names[], int length, string valid[]);
int main(int argc, char *argv[]){
const char SIZE = 20;
int n, length, result;
string namelist[SIZE], names[SIZE], valid[SIZE];
cout << "How many names will be entered? ";
cin >> n;
cout << endl;
cout << "Enter the strings now please:" << endl;
for(int i=0;i<n;i++){
cin >> namelist[i];
namelist[i]=names[SIZE];}
cout << "The sorted names are: ";
sortReverse (n, namelist);
cout << " What is the minimum length for your names? ";
cin >> length;
cout << endl;
result=remove (n, names, length, valid);
cout << "Removing invalid names now....." << endl<<endl;
cout << "The valid names are:";
for (int i=0;i<=result;i++){
cout << " " << valid[i];}
cout << endl;
system("PAUSE");
}
void sortReverse(int n, string namelist[])
{
char i;
string temp;
int marker;
marker=n;
while (marker>0){
i=0;
while (i<marker-1){
if (namelist[i] > namelist[i+1]){
temp=namelist[i];
namelist[i]=namelist[i+1];
namelist[i+1]=temp;}
i=i+1;}
marker=marker-1;
cout << " " << namelist[i];}
}
int remove(int n, string names[], int length, string valid[])
{
int result=0, i;
for (i=0;i<n;i++){
if (names[i].length()>=length);
valid[result++]=names[i];
}
return result;
}
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
/*
* Prototypes -
*/
void sortReverse(int n,string namelist[]);
int remove(int n, string names[], int length, stringvalid[]);
int main(int argc, char *argv[]){
const char SIZE = 20;
int n, length, result;
string namelist[SIZE], names[SIZE], valid[SIZE];
cout << "How manynames will be entered? ";
cin >> n;
cout << endl;
cout << "Enter the strings now please:" << endl;
for(int i=0;i<n;i++){
cin >> namelist[i];
}
cout << "The sorted names are: ";
sortReverse (n, namelist);
cout << " What is the minimum length for your names? ";
cin >> length;
cout << endl;
result=remove (n, namelist, length, valid);
cout << "Removing invalid names now....." << endl<<endl;
cout << "The valid names are:";
for (int i=0;i<result;i++){
cout << " " << valid[i];}
cout << endl;
system("PAUSE");
}
void sortReverse(int n,string namelist[])
{
int i,j,flag=1;
string temp;
int marker;
marker=n;
for(j=0;j<marker;j++){
for(i=0;i<marker-1;i++){
if (namelist[i] > namelist[i+1]){
temp=namelist[i];
namelist[i]=namelist[i+1];
namelist[i+1]=temp;}
}
}
for(i=0;i<n;i++)
cout << " " << namelist[i];
}
int remove(int n, string names[], int length, string valid[])
{
int result=0,i;
for (i=0;i<n;i++){
if (names[i].length()>=length)
valid[result++]=names[i];
}
return result;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.