strings

Im looking for a bit of code that could take an imputed name (first and last) and rearrange it (see example) before being saved in the array which can be recalled in the menu. I don't know where to put it, or even what to write for that matter. Helps appreciated. :)

Example:

User Input: Firstname Lastname
Output: Lastname, Firstname

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: ";  //this is where I enter names
         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 not cross-post your messages.
like vlad said, i think you should've moved the other posts to this forum, as this is actually a beginner's issue, and you shouldn't post the same problem in multiple forums.

i didn't actually read your code, i think there's an easy way to do it:
1
2
3
4
5
6
	char temp[20] = {'\0'};
	string firstName, lastName;
	cin.getline(temp , 19 , static_cast<char>(0x20));    //0x20 is ASCII for white space.
	firstName = temp;
	cin.getline(temp , 19 );
	lastName = temp;

after this code you can manipulate the name as you see fit.
Thank you for your reply, and apologies for the double post, I am just desperate to find the answer for this. *Finals coming soon*. So I tried adding that code into my program to no avail, I tried formatting it into my void getNames function.

My teacher left a note saying:

"Upgrade your names program from lab 06 so that you can enter names like this “John Doe” at the keyboard but are converted to this “Doe, John” before being added to the storage list (array or vector). HINT: using str.find(substr, pos) function to find the ‘ ’ between the first name and last name entered; and then using str.substr(pos, num) function to extract the first name and last name; finally, concatenate the last name, ‘,’, and the first name to form the required string."

This is what is really giving me the issues, I don't know how to (or where to) incorporate str.find or str.substr into my code.

BTW. The code works, it takes names and everything, its just an issue of rearranging them.
Last edited on
you know what, me too have an asshole professor who needs an exact code.
i think he should accept any program that is:
1- readable.
2- working correctly on any input and outputs the correct demanded result.

the code i posted satisfies the two conditions, and if i were you, i'd take this code and ignore all about this ( str.find and str.substr ), this is an easy, short, and efficient code, without the use of these functions.
After all, he did say it was a hint, my problem now is in how to incorporate it into the getNames module. If you can help me with that, I am home free for this.

I am aware that after I can then cout lastname + ',' + firstname to get the result!
Last edited on
you know what, me too have an asshole professor who needs an exact code.
i think he should accept any program that is:
1- readable.
2- working correctly on any input and outputs the correct demanded result.

the code i posted satisfies the two conditions, and if i were you, i'd take this code and ignore all about this ( str.find and str.substr ), this is an easy, short, and efficient code, without the use of these functions.


The professor's job is to teach you how to program C++ as well as possible. Learning about STL objects like strings is an important part of modern C++ programming. If your professor is encouraging you to learn about them, then he's doing a good job.

i'm not gonna actually solve this for you, it's an assignment after all, i can give you some pointers:

after the code i posted gets executed, you will have the first name stored in firstName and the last name stored in lastName, if you want to output it as:
firstname lastname
you can go like:
cout<<firstName<<lastName;
otherwise you can write:
cout<<lastName<<", "<<firstName;

furthermore, if you want to save the whole name in a string, you can use this:
1
2
3
4
string fullName;
fullName = lastName;
fullName += ", ";
fullName += firstName;


that's all the help i can give you.

edit: i should apologise for the professor, if he really ment to give you a hint, not a restriction.
Last edited on
Alright, ill try to take it from here. Thanks to those who contributed. Very much appreciated!
well MikeyBoy, i'm sure you don't know anything about my specific professor.

first of all, he doesn't know shit about anything newer than standard C++ 1998, even this one, he doesn't know what the hell does STL stand for.
he demonstrated everything about stacks in 30 seconds and moved to another subject.
he gave one quick example about recursion and didn't even explain it enough, no one student understood it.
he encouraged us to use the C style headers (.h).
and this my friend is really so little of his madness, i don't even know how he got this job.

i really give all my respect to other professors, i just hate mine.
Topic archived. No new replies allowed.