header file problems

program keeps telling me progam has stopped working. I think its the header file but i have no idea.
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef tsuPod_h
#define tsuPod_h

#include <cstring>
#include <cstdlib>
#include <iostream>
#include <fstream>


using namespace std;

struct TsuPod{
           
    string title;
    string artist;
    int size;
    };
    
const int MEMORY_SIZE = 25;
const int NUM_SONGS = 8;
TsuPod tsuPod_memory[NUM_SONGS];

////////////////////////////////
/* FUNCTION - void initTsuPod

 Initialize all the slots to blank and 0 size memory
 
 input parms - none
 
 output parms - none
*/

void initTsuPod ()
{
     int i = 0;
     while(i <= NUM_SONGS)
     {
             tsuPod_memory[i].title = "     ";
             tsuPod_memory[i].artist = "     ";     
             tsuPod_memory[i].size = 0;
             i++;
     }
}



///////////////////////////////
/* FUNCTION - int addSong
 * attempts to add a new song to the tsuPod
          o returns a 0 if successful
          o returns -1 if not enough memory to add the song
          o returns -2 for any other error (such as a blank title or artist)
 
 input parms - /*add nessesary parameters here*/
 
 //output parms - /*add nessesary parameters here*/


int addSong ( string newTitle, string newArtist, int size)
{
    int i = 0;
    
    if(size >= 28)
    return -1;
    
    while( i <= NUM_SONGS)
    {
           if (tsuPod_memory[i].size > 0)
              i++;
           else
               tsuPod_memory[i].artist = newArtist;
               tsuPod_memory[i].title = newTitle;
               tsuPod_memory[i].size = size;
               return 0;
    }
    
    if (i > 8)
       return -2;
}
    
    



////////////////////////////
/* FUNCTION - int removeSong
    * attempts to remove a song from the tsuPod
          o returns 0 if successful
          o returns -1 if nothing is removed
    
          
input parms - /*add nessesary parameters here*/
 
//output parms - /*add nessesary parameters here*/
  

int removeSong(string title)
{
    int i = 0;
    
    while ( i <= NUM_SONGS )
    { 
          if(tsuPod_memory[i].title == title)
          {
              tsuPod_memory[i].title = "     ";
              tsuPod_memory[i].artist = "     ";
              tsuPod_memory[i].size = 0;
              return 0;
              }
          else
              i++;
    }
    
    if ( i > 8 )
       return -1;
}
          





////////////////////////////
/* FUNCTION - void clearMemory
* clears all the songs from memory

input parms - /*add nessesary parameters here*/
 
//output parms - /*add nessesary parameters here*/

void clearMemory()
{
     int i = 0;
     while(i <= NUM_SONGS)
     {
             tsuPod_memory[i].title = "     ";
             tsuPod_memory[i].artist = "     ";     
             tsuPod_memory[i].size = 0;
             i++;
     }
}





/////////////////////////////
/* FUNCTION - void showSongList   
    * prints the current list of songs in order from first to last to standard output
    * format - slot #, Title, Artist, size in MB (one song per line)
    * print "Empty" for any slots that do not contain a song

input parms - /*add nessesary parameters here*/
 
//output parms - /*add nessesary parameters here*/

    
void showSongList ()
{
     int i = 0;
     
     cout << "Title \t Artist \t Size" << endl;
     while ( i == NUM_SONGS )
     {
           cout << tsuPod_memory[i].title << " " << tsuPod_memory[i].artist;
           cout << " " << tsuPod_memory[i].size << endl;
           i++;
     }
}





#endif 


and i was just trying to test the functions with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "tsupod.h"

using namespace std;

int main()
{
    initTsuPod();
    
    showSongList();


   
        
    system("PAUSE");
    return 0;
}


all of them are in the same project file too.
Okay, several things.

One, a header is pretty much copied-and-pasted into a C++ file with the #include directive. That means that not only do you have a lot of redundant #includes, but the using namespace std in the header is also useless. :)

Second, check line 101. Remember: arrays go from 0 to their size -1, so if you have an array of size 8 and you try to access the 8th element, you're stepping out of bounds. The same goes for line 134. Also, check line 163... doesn't something look odd there?

A final tiny thing to note: headers were meant to be "bridges" between C++ files by declaring stuff that would be used in multiple C++ files. While I have seen and written some unorthodox exceptions (I'm evil), that's generally what you're supposed to use them for. I'd suggest moving your code into main.cpp. ;)

-Albatross
Well i changed it up a bit here is where I am at now.

tsuPod.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <cstring>
#include <fstream>
#include "tsuPod.h"

using namespace std;
      


TsuPod tsuPod_memory[NUM_SONGS];





////////////////////////////////
/* FUNCTION - void initTsuPod

 Initialize all the slots to blank and 0 size memory
 
 input parms - none
 
 output parms - none
*/

void initTsuPod ()
{
     int i = 0;
     while(i <= NUM_SONGS - 1)
     {
             tsuPod_memory[i].title = "     ";
             tsuPod_memory[i].artist = "     ";     
             tsuPod_memory[i].size = 0;
             i++;
     }
}



///////////////////////////////
/* FUNCTION - int addSong
 * attempts to add a new song to the tsuPod
          o returns a 0 if successful
          o returns -1 if not enough memory to add the song
          o returns -2 for any other error (such as a blank title or artist)
 
 input parms - /*add nessesary parameters here*/
 
 //output parms - /*add nessesary parameters here*/


int addSong ( string newTitle, string newArtist, int size)
{
    int i = 0;
    int count = 0;
    int total_mem = 0;
    
    
    while ( count <= NUM_SONGS - 1 )
    {
          total_mem += tsuPod_memory[count].size;
          count++;
    }
    
    if(total_mem > 28)
    return -1;
    
    while( i <= NUM_SONGS - 1)
    {
           if (tsuPod_memory[i].size > 0)
              i++;
           else
               tsuPod_memory[i].artist = newArtist;
               tsuPod_memory[i].title = newTitle;
               tsuPod_memory[i].size = size;
               return 0;
    }
    
    if (i > NUM_SONGS - 1)
       return -2;
}
    
    



////////////////////////////
/* FUNCTION - int removeSong
    * attempts to remove a song from the tsuPod
          o returns 0 if successful
          o returns -1 if nothing is removed
    
          
input parms - /*add nessesary parameters here*/
 
//output parms - /*add nessesary parameters here*/
  

int removeSong(string title)
{
    int i = 0;
    
    while ( i <= NUM_SONGS - 1 )
    { 
          if(tsuPod_memory[i].title == title)
          {
              tsuPod_memory[i].title = "     ";
              tsuPod_memory[i].artist = "     ";
              tsuPod_memory[i].size = 0;
              return 0;
              }
          else
              i++;
    }
    
    if ( i > NUM_SONGS - 1 )
       return -1;
}
          





////////////////////////////
/* FUNCTION - void clearMemory
* clears all the songs from memory

input parms - /*add nessesary parameters here*/
 
//output parms - /*add nessesary parameters here*/

void clearMemory()
{
     int i = 0;
     while(i <= NUM_SONGS - 1)
     {
             tsuPod_memory[i].title = "     ";
             tsuPod_memory[i].artist = "     ";     
             tsuPod_memory[i].size = 0;
             i++;
     }
}





/////////////////////////////
/* FUNCTION - void showSongList   
    * prints the current list of songs in order from first to last to standard output
    * format - slot #, Title, Artist, size in MB (one song per line)
    * print "Empty" for any slots that do not contain a song

input parms - /*add nessesary parameters here*/
 
//output parms - /*add nessesary parameters here*/

    
void showSongList ()
{
     int i = 0;
     
     cout << "Title \t Artist \t Size" << endl;
     while ( i <= NUM_SONGS - 1 )
     {
           cout << tsuPod_memory[i].title << " " << tsuPod_memory[i].artist;
           cout << " " << tsuPod_memory[i].size << endl;
           i++;
     }
}


tsuPod.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef tsuPod_h
#define tsuPod_h
#include <cstring>

#include <string>


struct TsuPod{
           
    string title;
    string artist;
    int size;
    };
    
const int MEMORY_SIZE = 25;
const int NUM_SONGS = 8;



////////////////////////////////
/* FUNCTION - void initTsuPod

 Initialize all the slots to blank and 0 size memory
 

*/

void initTsuPod ();



///////////////////////////////
/* FUNCTION - int addSong
 * attempts to add a new song to the tsuPod
          o returns a 0 if successful
          o returns -1 if not enough memory to add the song
          o returns -2 for any other error (such as a blank title or artist)
 
 */
 



int addSong ( string newTitle, string newArtist, int size);




////////////////////////////
/* FUNCTION - int removeSong
    * attempts to remove a song from the tsuPod
          o returns 0 if successful
          o returns -1 if nothing is removed
    
          
*/


int removeSong ( string title);





////////////////////////////
/* FUNCTION - void clearMemory
 clears all the songs from memory

*/

void clearMemory();




/////////////////////////////
/* FUNCTION - void showSongList   
    * prints the current list of songs in order from first to last to standard output
    * format - slot #, Title, Artist, size in MB (one song per line)
    * print "Empty" for any slots that do not contain a song

*/

    
void showSongList ();








#endif 


test_tsuPod.cpp
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
#include <iostream>
#include <fstream>

#include "tsuPod.h"

using namespace std;

int main() 
{
     
    // Following code initializes the tsuPod 
    initTsuPod();


    
    // Following code lists all songs located in tsuPod memeory 
    showSongList();
    cout << endl;
    


    // Following code tests tsuPod input functionality 
    int retCode = addSong("Runaway1", "Bon Jovi1", 1);
    retCode = addSong("Runaway2", "Bon Jovi2", 2);
    retCode = addSong( "Runaway3", "Bon Jovi3", 3);
    retCode = addSong( "Runaway4", "Bon Jovi4", 1);
    retCode = addSong( "Runaway5", "Bon Jovi5", 5);
    retCode = addSong( "Runaway6", "Bon Jovi6", 6);
    retCode = addSong( "Runaway7", "Bon Jovi7", 1);
    retCode = addSong( "Runaway8", "Bon Jovi8", 1);
    retCode = addSong( "Runaway8", "Bon Jovi8", 1);
    retCode = addSong( "Runaway8", "Bon Jovi8", 1);
    retCode = addSong( "Runaway8", "Bon Jovi8", 1);
    cout << retCode << endl;   
    showSongList();
    cout << endl;



    // Following code tests tsuPod song removal functionality         
    retCode = removeSong("Runaway100");
    cout << retCode << endl;    
    showSongList();
    cout << endl;



    
    
    system("PAUSE");
    
}


I cant figure out why they wont compile. I get a long list of errors, and I'm thinking its because of the header file.

first error on the list is: 'string' does not name a type (reffering to the tsuPod.cpp file)
I cant remember what i did but I had it changed up somehow and the only error was multiple definition error, now it's a lot more than just that.
Topic archived. No new replies allowed.