A quick problem reguarding strings

Hi there, I am very new to C++ and I am encountering a problem in my work. I have a program that Adds, Removes, Sorts, Finds, and Displays character inputs, such as names. The program takes first and last names successfully. Where my issue comes in though is I am trying to add in the functionality of having it format the inputted names as per this example when displayed:

Input: FirstName LastName
Output: LastName, FirstName


Here is my code, any help would be appreciated!

Thanks,
thebawt


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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
const int SIZE=10;
void getNames(string names[], int& n)
{
     n=0;
     do
     {
         cout<<"Enter a name, quit to stop: ";
         getline(cin, names[n]);
         if(names[n]=="quit") 
         break;
             }
             while(++n<SIZE);
             
     }

 void display(string a[], int s)
{
      for(int i=0; i<s; i++)
      {
              cout<<a[i]<<endl;
      }
}


 void sort(string a[], int s)
{
      for(int i=1; i<s; i++)
      for(int j=0; j<s-i; j++)
      {
              if(a[j]>a[j+1])
              swap(a[j], a[j+1]);
      }
}


int find(string a[], int s)
{
    string name;
    cout<<"Enter the name to find: ";
    cin>>name;
    for(int i=0; i<s; i++)
    {
            if(a[i]==name)
            return i;
    }
            return s;
}


void remove(string a[], int& s)
{
     int i;
     i=find(a, s);
     if (i==s)
     cout<<"There is no such name!";
     else
     {
         for(int j=i; j<s; j++)
         {
                 a[j]=a[j+1];
         }
         s--;
     }
}
char getRes()
{
     char res;
     cout<<endl;
     cout<<" (A)dd // (S)ort // (F)ind // (R)emove // (D)isplay // (Q)uit"<<endl
     <<"Choice: ";
     cin>>res;
     return res;
}


main()
{
      string a[SIZE];
      int n;
      char res;
      do
      {
          res=getRes();
          if(res=='Q')
            break;
          switch(res)
          {
              case 'A': getNames(a, n); break;
              case 'D': display(a, n); break;
              case 'S': sort(a, n); break;
              case 'F': cout<<find(a, n); break;
              case 'R': remove(a, n); break;            
          }
      }while(true);
      system("pause");
} 
Last edited on
Do you understand what you are writing? For example what does mean line

str.substr(0, 0)

in function getNames?
Last edited on
Ahh, oops, that was me toying around and I forgot to remove it, ill edit the post.

These are the two lines (17 and 18) I accidentally left in.

str.find(' ', 0)
str.substr(0, 0)

Edit:
I mostly understand what I have wrote so far, however, as part of my current assignment, I am having trouble understanding how to format the names, or where to even put the conversion in my program.
Last edited on
The function has no the closing brace.
I'm sorry, I don't understand what you mean by that.
What I wrote is what I mean. You formated the code badly.
EDIT: As for your problem then you are entering a string of two words but in all other functions you are entering only one word and trying to compare it with a string of two words.
That is using getline you entered two words. But using operator >> you entered only one word. It is obvious that two words can not be equal to one word.
Last edited on
Perhaps an example? Keep in mind I am very new. I started a class less than a month ago.
Topic archived. No new replies allowed.