So i have a hw problem for my class where i have to store ten names, capitalize them and then sort them alphabetically. We just started using strings and there are some things that are still a little confusing to me. This code can store all the names in a string array, but the UpperCase seems to not be working. I dont know for sure but i think it is because I have second for loop running cap amount of times, which would be 10. And since not every string will have 10 elements, i'm running into problems?..Is that it or is it something else? Well i tried to fix this by using the .length (function?), to find the length of each name in the array, but I always get errors. Any help is appreciated, thanks!
#include<iostream>
#include<string>
using namespace std;
void UpperCase(string names[],int cap);
void print(string names[],int cap);
void swap(string names[],int &x,int &y);
string names[10];
int main(){
char a;
cout<<sizeof(a);
for(int i=0;i<10;i++){
cout<<"Enter a name for student "<<i+1<<" : ";
cin>>names[i];
cout<<endl;
}
UpperCase(names,10);
cout<<endl;
print(names,10);
cout<<endl;
print(names,10);
return 0;
}
void print(string names[],int cap){
for(int i=0;i<cap;i++)
cout<<names[i]<<endl;
}
void UpperCase(string names[],int cap){
for(int student=0;student<cap;student++){
for(int letter=0;letter<names[student].length();letter++){
if(names[student][letter]>='a')
names[student][letter]-=('a'-'A');
}
}
}