forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex12_24.cpp
30 lines (28 loc) · 853 Bytes
/
ex12_24.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
// ex12_24.cpp
// Exercise 12.24
//
// Created by pezy on 12/30/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Write a program that reads a string from the standard input into a
// dynamically allocated character array.
// Describe how your program handles varying size inputs.
// Test your program by giving it a string of data that is longer than the
// array size you've allocated.
#include <iostream>
int main()
{
// need to tell the size.
std::cout << "How long do you want the string? ";
int size{0};
std::cin >> size;
char* input = new char[size + 1]();
std::cin.ignore();
std::cout << "input the string: ";
std::cin.get(input, size + 1);
std::cout << input;
delete[] input;
// Test: if longer than the array size, we will lost the characters which
// are out of range.
}