Array Problem

I am currently having issues with getting user input from a separate file, and using that input to put into an array into a class in a separate file. (Sorry if I confused you there) So, yeah. If anybody could help me with gutting user input and putting it into a class containing an array, that would be great!

Thanks,
Ryan :)

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
176
177
178
179
180
181
182
183
184
185
186
//  Rolodex.h

#include<string>
#include<iostream>
using namespace std;

const int NAMES_CAPACITY = 30;
const int NUMBERS_CAPACITY = 30;

#ifndef ROLODEX_LOCK
#define ROLODEX_LOCK

class Rolodex
{
public:
    // Constructors
    Rolodex(void);
    Rolodex( int newIndex );
    
    // Mutator Functions
    bool isEmpty( void );
    void resetList( void);
    void addingCard( string inputNames, int inputNumber );
    void deletingCard( int, string );
    void obtainCurrentCard( void );
    void changingCurrentCard( void );
    void searchForCard( void );
private:
    string names[ NAMES_CAPACITY ];
    int numbers[ NUMBERS_CAPACITY ];
    int sizeOfNames;
    int sizeOfNumbers;
    int numOfElements;
    
    
};


#endif

  //  Rolodex.cpp


#include "Rolodex.h"
#include "Card.h"
//#include <string>
//#include <iostream>
using namespace std;

Rolodex::Rolodex(void)
{
    names[ NAMES_CAPACITY ] = {""};
    numbers[ NUMBERS_CAPACITY ] = {0};
    sizeOfNames = 0;
    sizeOfNumbers = 0;
    numOfElements = 0;
    

}

bool Rolodex::isEmpty( void )
{
    if ( sizeOfNumbers == 0 && sizeOfNames == 0 ) return true;
    else                                          return false;
}

void Rolodex::resetList( void )
{
    names[ NAMES_CAPACITY ] = {""};
    numbers[ NUMBERS_CAPACITY ] = {0};
    sizeOfNames = 0;
    sizeOfNumbers = 0;
}


void Rolodex::addingCard( string inputNames, int inputNumber )
{
    Card myCard;
    
    for ( int j = 0; j < numOfElements; j++)
    {
        sizeOfNames++;
        numOfElements++;
        myCard.getName( inputNames );
        
    }
    
    for ( int i = 0; i < numOfElements; i++ )
    {
        sizeOfNumbers++;
        numOfElements++;
        myCard.getPhoneNumber( inputNumber );

    }
}

//  workerBee.cpp


#include "workerBee.h"
#include "Card.h"
#include "Tools.h"
#include "Rolodex.h"
#include<iostream>
#include<string>
#include<array>
using namespace std;

const int CAPACITY = 30;


void workerBees(void)
{
    workerBee();
}

void workerBee(void)
{
    char userChoice;
    do
    {
        Rolodex myRolodex;
        Card myCard;
        
        if ( myRolodex.isEmpty() == true )
        {
            cout << "No current contacts." << endl;
        }
        
        userChoice = getChar("Select operation: a)dd, d)elete, r)eset, n)ext, f)ind, q)uit: ");
     
        switch ( userChoice )
        {
            case 'a':
            case 'A':
            {
                string inputNames;
                int inputNumber;
                
                inputNames = getString("Enter name for Rolodex card (0 to quit): "); // Names to be entered by user
                inputNumber = getInt("Enter phone number for Rolodex card (0 to quit): "); // Numbers to be entered by user
                
                while ( inputNames != "0" && inputNumber != 0 )
                {
                    myRolodex.addingCard( inputNames, inputNumber );
                }

            }
                break;
            
            case 'd':
            case 'D':
            {
            
            }
                break;
            
            case 'r':
            case 'R':
            {
                myRolodex.resetList(); // done
            }
                break;
            
            case 'n':
            case 'N':
            {
            
            }
                break;
            
            case 'f':
            case 'F':
            {
            
            }
                break;
            
            case 'q':
            case 'Q':
            {
                break; // done
            }
        }
    } while ( ! (( userChoice == 'q' ) || (userChoice == 'Q')) );
}
Not sure where you want to read the file or populate the array, so I'll give some generic advice.

Create a function that takes a string for the input file and populates the array.

Inside that function, use ifstream to open the file as seen here (look at the example on this site): http://www.cplusplus.com/reference/fstream/ifstream/open/

Once you have the file open, you can probably loop on ifstream::good(), which will return false if the file isn't open or you have read all the data in the file.
http://www.cplusplus.com/reference/ios/ios/good/

Inside your loop, you can use the get function to get data from the file:
http://www.cplusplus.com/reference/istream/istream/get/

Set the value from the get function to different members in your array.

You will want to close the ifstream when you are done:
http://www.cplusplus.com/reference/fstream/ifstream/close/

Let us know when you try this and if you have more questions.
i want to close my program saying thank you .is there any method plz help
tscott8706,

It looks like it would work, but the only problem is my teacher would not let me use that method in trying to store data in an array, which is in a class. I am really stuck on this.
Which method specifically are you not allowed to use? Are you saying you cannot use the ifstream class at all?
tscott,

Exactly. My professor wants us to use classes and arrays for this program. I am just having issues with getting that data into the class.
Hold on... when you say "user input from a separate file", do you mean from a separate C++ header file?
yeah, for example, in the header file "workerBee.h" under switch statement 'A', i need to get int inputNumber and string inputName, and use it in the class "Rolodex.h".
When you do myRolodex.addingCard on line 145, you should be putting data into the Rolodex class.

I think you need to put the statements where you get the names and number into your loop though, otherwise I think you'll loop forever.
Okay, I created another class for just one single card. And I created "get" functions. maybe I could use these?

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
//  Card.cpp


#include "Card.h"

Card::Card(void)
{
    string name = "";
    int phoneNumber = 0;
}

Card::Card( string inputNames, int inputNumbers )
{
    name = inputNames;
    phoneNumber = inputNumbers;
}

string Card::getName( string inputNames )
{
    return inputNames;
}

int Card::getPhoneNumber( int inputNumbers )
{
    return inputNumbers;
}

void Card::setCurrentName( string inputNames )
{
    name = inputNames;
}

void Card::setCurrentNumber( int inputNumbers )
{
    phoneNumber = inputNumbers;
}

//  Card.h

#include<string> 
#include<iostream> 
using namespace std;

#ifndef CARD_LOCK
#define CARD_LOCK

class Card
{
public:
    // Constructors
    Card(void);
    Card( string inputNames, int inputNumbers );
    
    // Accessor functions
    string getName( string inputNames );
    int getPhoneNumber( int inputNumbers );
    
    // Mutator functions
    void setCurrentName( string inputNames );
    void setCurrentNumber( int inputNumbers );
    
private:
    string name;
    int phoneNumber;
};


#endif 
Your arrays in Rolodex are string names and int numbers. If you want to use this card class (you can if you want), your array in Rolodex needs to be of type "Card", and your addingCard function needs to add "Card" rather than a string name and integer number.
Topic archived. No new replies allowed.