PDA

View Full Version : C++ Question


mscags
12-06-2005, 03:58 PM
Hi,
I'm trying to make a program and am having some difficulty with arrays. I need to let the user decide how many entries he wants to imput and then use that number as the size of the array. This is what my code looks like but I keep getting an error saying that I can't define my array that way. Is thee any other way to do this or am I just missing something very simple? Thanks for the help.

const int Size = 13;
int Entries;
int Numbers[Entries], Names[Entries];

cout << "How many entries do you have?";
cin >> Entries;
cout << "Please enter all of the names.";
for (int i=0; i<Entries; i++)
cin >> Names[i];
cout << "Please enter the phone numbers.";
for (int i=0; i<Entries; i++)
cin >> setw(Size) >> Numbers[i];

OrcaDK
12-06-2005, 04:12 PM
int Entries;
int Numbers[], Names[];

cout << "How many entries do you have?";
cin >> Entries;
Numbers = new int[Entries];
Names = new int[Entries];
cout << "Please enter all of the names.";
for (int i=0; i<Entries; i++)
cin >> Names[i];
cout << "Please enter the phone numbers.";
for (int i=0; i<Entries; i++)
cin >> setw(Size) >> Numbers[i];

mscags
12-06-2005, 04:21 PM
Thanks /images/graemlins/smile.gif

mscags
12-06-2005, 04:27 PM
You wouldn't know an easy search function would you? I need to be able to let the user seach for a name and output the matching results. I know you can use a bubble sort for numbers, but is there anything similar for searches? I also need a way to verify that the user imputs the phone numbers in the format XXX-XXX-XXXX. I was thinking some type of if then statement, but I'm not real sure how to compare numbers withing an integer like that. ANy help would be appreciated.

OrcaDK
12-06-2005, 04:38 PM
[ QUOTE ]
You wouldn't know an easy search function would you? I need to be able to let the user seach for a name and output the matching results. I know you can use a bubble sort for numbers, but is there anything similar for searches? I also need a way to verify that the user imputs the phone numbers in the format XXX-XXX-XXXX. I was thinking some type of if then statement, but I'm not real sure how to compare numbers withing an integer like that. ANy help would be appreciated.

[/ QUOTE ]

For the input validation, check up on regular expressions. I have no experience using regexep in C++, so unfortunately i can't help you. Regarding the search, you could actually use regexp for this also, depending on what kind of search you're doing. Should the search return partial results ("es" matching "test" and so on), or should it only return exact matches? If it's exact matches you're going for, sort the list and make a binary search.

mscags
12-06-2005, 04:44 PM
[ QUOTE ]
[ QUOTE ]
You wouldn't know an easy search function would you? I need to be able to let the user seach for a name and output the matching results. I know you can use a bubble sort for numbers, but is there anything similar for searches? I also need a way to verify that the user imputs the phone numbers in the format XXX-XXX-XXXX. I was thinking some type of if then statement, but I'm not real sure how to compare numbers withing an integer like that. ANy help would be appreciated.

[/ QUOTE ]

For the input validation, check up on regular expressions. I have no experience using regexep in C++, so unfortunately i can't help you. Regarding the search, you could actually use regexp for this also, depending on what kind of search you're doing. Should the search return partial results ("es" matching "test" and so on), or should it only return exact matches? If it's exact matches you're going for, sort the list and make a binary search.

[/ QUOTE ]

Thanks, I think that should get me started.

mscags
12-06-2005, 05:51 PM
Ok so I talked to my teacher and she said for the iimput validation all I need to do is to use a char array, but I thought that char[5] meant a char with 5 spaces not a char array. Is there anyway to make a char array and set the spaces? Would it be similar to the code that you showed me? Thanks for the help.

Mike

OrcaDK
12-06-2005, 06:31 PM
Ok, you can make the validation using char arrays also, here's a very crude untested example:

public bool validate(char[] input)
{
for(int i=0; i<12; i++)
if(i==3 || i==7)
{
if(input[i] != '-')
return false;
}
else
if(input[i] < 48 || input[i] > 57)
return false;

return true;
}

Edit: Looks kinda weird without the tabs/spaces, but it works anyway, just gotta put in the padding.

mscags
12-06-2005, 06:54 PM
sweet, is there anyway that I can have it do all of my chars at once or do I have to repeat the code for all of them.

EDIT: I tried that code that you gave me for the arrays, but it still gave me an unkown size error. ANy ideas?

TomCollins
12-07-2005, 12:47 AM
[ QUOTE ]
int Entries;
int Numbers[], Names[];

cout << "How many entries do you have?";
cin >> Entries;
Numbers = new int[Entries];
Names = new int[Entries];
cout << "Please enter all of the names.";
for (int i=0; i<Entries; i++)
cin >> Names[i];
cout << "Please enter the phone numbers.";
for (int i=0; i<Entries; i++)
cin >> setw(Size) >> Numbers[i];
delete [] Numbers;
delete [] Names;


[/ QUOTE ]

FYC. Memory leaks bad.