#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?