Need help here about array, hope someone can help

I been trying here and there, but still I can't find what's the problem about the array calling. hope anyone of you can help thanks.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
void sort_names(string [ ]);
void names_result(string [ ]);
using namespace std;
int main()
{
const int m=15;
string all_names_noteP;
string names[m];
ifstream mfile;
mfile.open("Name.txt");

if(mfile.is_open())
{
for(int i=0; i<m;i++)
{
getline(mfile,all_names_noteP);
names[i]=all_names_noteP;
}
sort_names(names);
names_result(names);
}

else
cout<<"Please Open the Name.txt file"<<endl;

system("pause");
return 0;

}


void names_result(string names[])
{
int m=15;
cout<<"Unsort List"<<endl;
for(int i=0;i<m; i++)
cout<<names[i]<<endl;
cout<<endl<<endl;

cout<<"Sorting List"<<endl;
for (int k=0; k<m; k++)
cout<<names[k]<<endl;

}
void sort_names(string names[])
{
const int m=15;
int testing;
string testing_names;
for(int a=0; a<m; a++)
{
testing = a;
testing_names = names[a];

for(int k=a+1; k<m; k++)
{
if(names[k] < testing_names)
{
testing_names = names[k];
testing = k;
}
}
names[testing] = names[a];
names[a] = testing_names;
}
}

Error:
1>e:\c++ notes\assignment 5-12\assignemnt\assignemnt\q9.cpp(5): error C2065: 'string' : undeclared identifier
1>e:\c++ notes\assignment 5-12\assignemnt\assignemnt\q9.cpp(5): error C2059: syntax error : ']'
1>e:\c++ notes\assignment 5-12\assignemnt\assignemnt\q9.cpp(6): error C2065: 'string' : undeclared identifier
1>e:\c++ notes\assignment 5-12\assignemnt\assignemnt\q9.cpp(6): error C2059: syntax error : ']'
1>e:\c++ notes\assignment 5-12\assignemnt\assignemnt\q9.cpp(23): error C3861: 'sort_names': identifier not found
1>e:\c++ notes\assignment 5-12\assignemnt\assignemnt\q9.cpp(24): error C3861: 'names_result': identifier not found

Do this and it complies, the unsort list is incorrect.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

void sort_names(string [ ]);
void names_result(string [ ]);


int main()
{	
const int m=15;
string all_names_noteP;
string names[m];
ifstream mfile;
mfile.open("Name.txt");

if(mfile.is_open())
{
for(int i=0; i<m;i++)
{
getline(mfile,all_names_noteP);
names[i]=all_names_noteP;
}
sort_names(names);
names_result(names);
}

else
cout<<"Please Open the Name.txt file"<<endl;

system("pause");
return 0;

}


void names_result(string names[])
{
int m=15;
cout<<"Unsort List"<<endl;
for(int i=0;i<m; i++)
cout<<names[i]<<endl;
cout<<endl<<endl;

cout<<"Sorting List"<<endl;
for (int k=0; k<m; k++)
cout<<names[k]<<endl;

}
void sort_names(string names[])
{
const int m=15;
int testing;
string testing_names;
for(int a=0; a<m; a++)
{
testing = a;
testing_names = names[a];

for(int k=a+1; k<m; k++)
{
if(names[k] < testing_names)
{
testing_names = names[k];
testing = k;
}
}
names[testing] = names[a];
names[a] = testing_names;
}
}
Last edited on
Topic archived. No new replies allowed.