| | |
Array limit
![]() |
•
•
Join Date: Sep 2003
Posts: 3
Reputation:
Solved Threads: 0
#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?
#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?
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
Check out this reference site: http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
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();
}
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();
}
•
•
Join Date: Feb 2003
Posts: 129
Reputation:
Solved Threads: 1
•
•
•
•
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();
}
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.
•
•
•
•
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
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 1:16 pm.
•
•
Join Date: Sep 2003
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Infinite array (C++)
- series of 1 + 1/2 + 1/3 ...etc (C++)
Other Threads in the C Forum
- Previous Thread: wsaData : undeclared identifier
- Next Thread: C Help!!!
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic fflush file floatingpointvalidation forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi







