SUBSTRINGS!!!

When I run my program i get weird things, my file reads correctly I just need help with the substring so the file outputs correctly. I just need help


this is what the file contains
A Samuel Spade Fido
A John Jones Rover
A Betsy Ann Smithville Jenny
H
H
A Lou Ann deVille Kitty
H
A Tom Smythe Fifi
A Marie Longfellow Jack
H
H
H





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 <fstream>
#include <iomanip>
#include <string>

using namespace std;

#include "qhead.h"



const int MaxNameLen = 20;



struct WaitingList {

 char        ownerName[MaxNameLen];
 char        petName[MaxNameLen];
 int         emeCode;
};


int main () 
 {
  Queue waitingList;
  string filename;
 ifstream in;
in.open ("f:\\patient.txt");
 if (!in) {
  cerr << "Error: cannot open the file " << filename << endl;
   system("pause");
  exit (2);
 }
 char type;
 string line;
 while (in >> type)
 {
  if (type == 'H')
  {
  WaitingList* ptrstudent = (WaitingList*) waitingList.Dequeue ();
   if (ptrstudent) {
    cout << "Treated: " << ptrstudent->ownerName << ptrstudent->petName  << endl;
    delete ptrstudent;
	  
   }
  }
  else  if (type == 'A')
  {

   getline (in, line);
    WaitingList* ptrnew = new (std::nothrow) WaitingList;
	 if (!ptrnew) {
		 cerr << "Error - out of memory\n";
		 system("pause");
		 exit (1);
		 }

 // break line into substrings
	 
      
   waitingList.Enqueue (ptrnew);
   cout << "Added patient: " << ptrnew->ownerName << ptrnew->petName << endl;
  }
  else 
   cerr << "Error: invalid transaction code encountered.\n"
        << "It was: " << type << endl;

 }
 in.close ();
 system("pause");
 return 0;
}
 
Can you provide your output?

String parsing is fun, basically it goes as follow.
parsed = str.substr(int i, int j)

Where str is the whole string, and parsed is the section of the string you want.

int i is the start position, and int j is how long you want to parse.

So if you had the str = "balloon";

and you had parsed = str.substr(0, 4);
parsed would be the section of "ball"

You can combine this with delimiters to help parse out a string. For example, if you have a string "abcdefghijk" and want to stop at 'e' use
int found = str.find('e') and it will return the position of 'e' which would be 4. Then entering in str.substr(0,found) would get "abcd"

Hope this helps.

here is my output
Added: Samuel Spade Fido
Added: John Jones Rover
Added: Betsy Ann Smithville Jenny
Treated: Samuel Spade Fido
Treated: John Jones Rover
Added: Lou Ann deVille Kitty
Treated: Lou Ann deVille Kitty
Added: Tom Smythe Fifi
Added: Marie Longfellow Jack
Treated: Marie Longfellow Jack
Treated: Betsy Ann Smithville Jenny
Treated: Tom Smythe Fifi
Topic archived. No new replies allowed.