Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-08-2003, 12:41 PM   #1 (permalink)
FragO'Matic
Registered User
 
FragO'Matic's Avatar
 
Join Date: Apr 2003
Location: collierville, TN.
Posts: 3
FragO'Matic is on a distinguished road
Send a message via AIM to FragO'Matic Send a message via Yahoo to FragO'Matic
Question Integer array help

Hello, I am a pretty basic C++ programmer, just a few high school classes, but I am sttempting to write a coding program, I want to make a interger array of a user specified amout of units.


Segment of Code

_________________

cout << "Input Your Code " << endl << endl;

getline (cin, code);

int length = code.length();

int location[length] = {0};

____________________


It is giving me the error that there must be a constant expression in the brackets to create the array. I remember in my class makeing many arrays without having constant varibles to decide the length. My entire code is below, if you see a problem or know an alternate way to make an interger array the same length as the string "code" your help would be very thankful.


*note* Code not complete, and I know the way I'm includeing apstring.h is not very effecient, I am just being lazy for such a small program.
____________________

#include<conio.h>
#include<iostream.h>
#include<apstring.h>
#include<apstring.cpp>

void main(){


int count=0;
int x=0;
int y=0;
apstring code;
char key[5][26] = { {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'},
{'M', 'B', 'C', 'Z', 'L', 'J', 'G', 'D', 'A', 'P', 'I', 'Y', 'R', 'W', 'N', 'V', 'X', 'K', 'H', 'F', 'S', 'O', 'U', 'T', 'E', 'Q'},
{'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'},
{'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A'},
{'F', 'Q', 'J', 'A', 'D', 'W', 'T', 'M', 'H', 'B', 'Z', 'R', 'X', 'O', 'Y', 'P', 'K', 'G', 'C', 'L', 'N', 'S', 'U', 'I', 'V', 'E'}};



cout << "Input Your Code " << endl << endl;

getline (cin, code);

int length = code.length();

cout << endl << length << endl;

int location[length] = {0};

while(count<length){

y=0;
while(y<26){
if(code[count]==key[0][y]){
location[count]=y;

}

y++;
}


count++;
}




return;
}


______________________

Thank You!
FragO'Matic is offline   Reply With Quote
Old 04-08-2003, 12:54 PM   #2 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
You are looking at doing something that looks dynamic. You are most likely going to need pointers.

int *array_of_int;

array_of_int = new int[SIZE];

you should also check to make sure that the memory was allocated

if( !array_of_int )
memory was not allocated.

don't forget to delete it later

delete array_of_int;

probably would be best if you read up on pointers
palin is offline   Reply With Quote
Old 04-08-2003, 03:25 PM   #3 (permalink)
FragO'Matic
Registered User
 
FragO'Matic's Avatar
 
Join Date: Apr 2003
Location: collierville, TN.
Posts: 3
FragO'Matic is on a distinguished road
Send a message via AIM to FragO'Matic Send a message via Yahoo to FragO'Matic
Smile Thanks

Thank you for your advice, it is all good advice, but I am still having the same problem when creating my array
FragO'Matic is offline   Reply With Quote
Old 04-08-2003, 03:39 PM   #4 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
I would suggest declaring everything at the start of the main function and then using the pointer method.
palin is offline   Reply With Quote
Old 04-08-2003, 04:09 PM   #5 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
this line is wrong:

int location[length] = {0};

you cannot initialize a stack-dynamic array. try

int location[length];
joe_bruin is offline   Reply With Quote
Old 04-09-2003, 11:55 AM   #6 (permalink)
FragO'Matic
Registered User
 
FragO'Matic's Avatar
 
Join Date: Apr 2003
Location: collierville, TN.
Posts: 3
FragO'Matic is on a distinguished road
Send a message via AIM to FragO'Matic Send a message via Yahoo to FragO'Matic
Talking Thank You

Thank you all very much, the program works fine now!
FragO'Matic is offline   Reply With Quote
Old 04-10-2003, 01:56 AM   #7 (permalink)
Unicorn
Registered User
 
Unicorn's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 11
Unicorn is on a distinguished road
Quote:
Originally posted by palin

don't forget to delete it later

delete array_of_int;
Also, don't forget it's an array.

delete [] array_of_int;
Unicorn is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
working with Array Goong MS Technologies ( ASP, VB, C#, .NET ) 3 07-22-2004 12:14 PM
breaking down an array from a form metazai PHP 12 07-09-2004 06:18 AM
array of characters in a structure TheDreamless Standard C, C++ 3 04-18-2003 04:30 PM
Array of chars of chars of, wait, uh... anon Standard C, C++ 15 01-11-2003 04:15 PM
adding to an array in php sde PHP 1 06-12-2002 11:04 AM


All times are GMT -8. The time now is 08:33 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting