CPP Text Input
CPP
Download (.zip)
#include <iostream.h> #include <string.h>
int main() { char line[16];
/* reads in from the console until a '\n' is found. */ cout << "Enter any text: "; cin.getline(line,sizeof(line)); cout << "line = " << line << endl; /* reads in from the console until a 'A' is found. */ cout << "End text with an 'A': "; cin.getline(line,sizeof(line),'A'); cout << "line = " << line << endl; /* if you use a different dilimiter you must get rid of the newline character yourself */ cin.get();
/* unlike getline(), get() will not discard the delimiter */ cout << "End text with an 'A': "; cin.get(line,sizeof(line),'A'); cin.get(); cin.get(); cout << "line = " << line << endl; cout << "Enter any text: "; cin.get(line,sizeof(line)); cout << "line = " << line << endl; cin.get();
/* read() will not insert a '\0' at the end of the string, and does not have the delimiter option, input will be read until buffer is filled followed by an enter */ cout << "Enter any text: "; cin.read(line,sizeof(line)); line[sizeof(line)] = '\0'; cout << "line = " << line << endl; cin.clear();
return 0; }
|