User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,582 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,664 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 6115 | Replies: 7
Reply
Join Date: Sep 2003
Posts: 3
Reputation: rknight is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rknight rknight is offline Offline
Newbie Poster

Array limit

  #1  
Sep 12th, 2003
#include <iostream>
#include <conio.h>

void input(char *str) {
bool quit = 0;
unsigned char key;
for(int i = 0;quit != 1;) {
key = getch();
switch(key) {
case 8: // Backspace
if (i > 0) {
i--;
*str--;
std::cout << "\b \b";
}
else
putch('\a');
break;
case 13: // Enter
*str = '\0';
putch('\n');
quit = 1;
break;
default:
*str = key;
putch(key);
i++;
*str++;
break;
}
}
}

int main() {
char string[40];
std::cout << ">";
input(string);
std::cout << string;
getch();
return 0;
}

I am working on a function that will take input from the user and store it in a string. One problem I face is when the user enters more than the size of the char array. I could set up the input function to take two arguments, the second being the size of the array, but I want to find a more simple way. I want the program to work out how big the array is without me telling it. Does anyone have any suggestions?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 106
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Array limit

  #2  
Sep 12th, 2003
I haven't done work with strings in a long while, but could u by any chance use string.h with getline?

Check out this reference site: http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
Reply With Quote  
Join Date: Sep 2003
Location: San Diego, CA, USA
Posts: 75
Reputation: Valmian is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Re: Array limit

  #3  
Sep 15th, 2003
Use dynamic memory allocation.
In C++ (didn't test):

char * getstring()
{
fflush (stdin); //don't have to do it
char * string=NULL;
char * temp=NULL;
int size =2
scanf(""); //to get buffer input
char ch;
do
{
delete [] string;
string = new char[size];
memcpy (string, temp, sizeof(temp));
delete[]temp;
ch = getc(stdin);
string[size-2] =ch;
temp = new char [size];
memcpy(temp, string, sizeof (string));
size++;
} while (ch!='\n'); //change the quiting value if you want to whatever
return string;
}
void main (void)
{
char * string = getstring();
}
Reply With Quote  
Join Date: Aug 2003
Location: Austin, TX
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Re: Re: Array limit

  #4  
Sep 16th, 2003
You're using C++ but you are using the std:string? #include <string>

If you want to do strings C-style which is a pointer to an array of chars like you have you first want to malloc() and then when you want to allocate more space you want to realloc().
Reply With Quote  
Join Date: Sep 2003
Location: San Diego, CA, USA
Posts: 75
Reputation: Valmian is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Re: Re: Re: Array limit

  #5  
Sep 16th, 2003
I know, I just had like 1 min so I wrote what came first to my mind.. anyways, I am known to use classes, new/delete and all else in C. (i never took time to learn iostream etc.. but I like objects.. my first though is C)
Ilya
Reply With Quote  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
Colleague
Bob Bob is offline Offline
Team Member

Re: Array limit

  #6  
Oct 4th, 2003
Originally Posted by Valmian
Use dynamic memory allocation.
In C++ (didn't test):

char * getstring()
{
fflush (stdin); //don't have to do it
char * string=NULL;
char * temp=NULL;
int size =2
scanf(""); //to get buffer input
char ch;
do
{
delete [] string;
string = new char[size];
memcpy (string, temp, sizeof(temp));
delete[]temp;
ch = getc(stdin);
string[size-2] =ch;
temp = new char [size];
memcpy(temp, string, sizeof (string));
size++;
} while (ch!='\n'); //change the quiting value if you want to whatever
return string;
}
void main (void)
{
char * string = getstring();
}

Valmian, I really don't wish to be unkind, but it has to be said, the code you posted is horrible. I can't begin to imagine how someone trying to seek advice here would manage with such code, but the problems it introduces would be beyond most of the beginners that come here seeking help, for sure.

No offence, but at the very least I would suggest that if you post code here for people you ought to try compiling it first to check that it'll at least get past your compiler. Failing to do that can leave a poor noob struggling for hours, or days even, trying to overcome the problems in your code.

On top of that, it's clear from your code that you're not sufficiently experienced in C++ to offer advice of the nature you attempted to provide, you're punching above your weight.

All credit to you for coming here and trying to help people, I think that's great. With respect, it might be better if you gained more experience before trying to help further, otherwise you can end up doing more harm than good.

No offence intended.
Reply With Quote  
Join Date: Aug 2003
Location: Austin, TX
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Array limit

  #7  
Oct 6th, 2003
Originally Posted by Valmian
I know, I just had like 1 min so I wrote what came first to my mind.. anyways, I am known to use classes, new/delete and all else in C. (i never took time to learn iostream etc.. but I like objects.. my first though is C)
Ilya

In all honesty, I'm using C++ with C too right now because I'm writing a binary file reader to read and load Lightwave objects into a C++ class/object representation (so I can start using my Lightwave models in various applications). In fact, because most all APIs are written in C, like the Python C API and the MySQL C API (not to mention OpenGL), when you're wanting to write a psuedo-OO program (as C++ isn't completely OO) and don't want to bother learning Objective-C which is a pain in the ass, it's probably the only viable way to go (unless you switch programmin languages) -- In my years of working at the Los Alamos National Labs, you'd be so suprised to see how ugly, yet operational most code is. When writing a game engine, its been my experience (not that I have a lot of experience writing game engines), that OO is the best way to go and C++ really lends itself nicely to important OO concepts like inheritance and polymorphism and object message passing (although error handling is less than desirable). For example, there are some things that I need to do with polymorphic STL maps (with dynamic casting) for run-time "type reconstruction".. On the other side though, I hate using cout/cin (because I know for a fact they're just sitting on top of printf which is a far more superior function adding overhead (but you're right, I'm using the STL and C++, why should I be concerned about saving on overhead!!! Hehe)...

So its OK to be a code-******* as long as you have a clear objective and a purpose for doing so. I think the fact that you're being lazy about your code design will kick you in the ass repeatedly for projects to come. Memory management is something a lot of programmers just don't get and I really think we can attribute a lot of the confusion to deceptively simply commands like new/delete and garbage collectors like the one in Java.

Learn about what each type means in terms of memory usage, where it exists in user space, etc. For instance, writing a loader for binary 3d files requires me to work closely with a spec which involves bitmasking out flags, reading in numbers that could either be either sizeof(char) (which is a constant across all C compilers thank god), sizeof(short), sizeof(int), or sizeof(float)..... I have to take into consideration bit-ordering, since the file is Big-Endian, I have to do byte_swaps and byte_swapping a float is a little tricky because I'm using a x86 processor which is (stupidly) Little-Endian. Good exercise in understanding bytes/bits handling and memory allocation.
Last edited by subtronic : Oct 6th, 2003 at 12:16 pm.
Reply With Quote  
Join Date: Sep 2003
Posts: 3
Reputation: rknight is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rknight rknight is offline Offline
Newbie Poster

Re: Array limit

  #8  
Oct 7th, 2003
Thank you everyone for your help. By the sounds of things, dynamic memory allocation sounds like the best choice. I think one of the main things I wanted to know was if it was possible to find out how much an array can hold by using a pointer; and it looks as if you can’t.

Now about the mixture of c and c++ in my code…………..I must confess that my knowledge in c++ is very small. At the moment I am putting bits of c++ into my c programs to get a taste of what it’s like.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 10:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC