Two Plus Two Older Archives  

Go Back   Two Plus Two Older Archives > General Gambling > Computer Technical Help
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-06-2005, 03:58 PM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default C++ Question

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];
Reply With Quote
  #2  
Old 12-06-2005, 04:12 PM
OrcaDK OrcaDK is offline
Member
 
Join Date: Nov 2004
Posts: 42
Default Re: C++ Question

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];
Reply With Quote
  #3  
Old 12-06-2005, 04:21 PM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default Re: C++ Question

Thanks [img]/images/graemlins/smile.gif[/img]
Reply With Quote
  #4  
Old 12-06-2005, 04:27 PM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default Re: C++ Question

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.
Reply With Quote
  #5  
Old 12-06-2005, 04:38 PM
OrcaDK OrcaDK is offline
Member
 
Join Date: Nov 2004
Posts: 42
Default Re: C++ Question

[ 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.
Reply With Quote
  #6  
Old 12-06-2005, 04:44 PM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default Re: C++ Question

[ 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.
Reply With Quote
  #7  
Old 12-06-2005, 05:51 PM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default Re: C++ Question

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
Reply With Quote
  #8  
Old 12-06-2005, 06:31 PM
OrcaDK OrcaDK is offline
Member
 
Join Date: Nov 2004
Posts: 42
Default Re: C++ Question

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.
Reply With Quote
  #9  
Old 12-06-2005, 06:54 PM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default Re: C++ Question

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?
Reply With Quote
  #10  
Old 12-07-2005, 12:47 AM
TomCollins TomCollins is offline
Senior Member
 
Join Date: Jul 2003
Location: Austin, TX
Posts: 172
Default Re: C++ Question

[ 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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:46 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.