Help with Objects, and Assignment

I reached the end of another chapter in a book I've been learning out of, and it gives me a step by step drill to test out. I thought I was doing good until the third step. It states to add an introductory line to a "letter" to a friend, and then prompt the user to input the name of another friend, so that the program can automatically add the question "Have you seen (friends name) lately?"

I was certain I was doing everything right with this code here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	string addressee;
	string letter_text;
	string friend_name;
	cout << "Enter the name of the person you would like to write to. \n";
	cin >> addressee;
	cout << "Dear, " << addressee << "\n";
	cin >> letter_text; // Problem is here. Cannot input text without crash.
	cout << "Please enter another friends name. \n";
	cin >> friend_name;
	cout << "Have you seen " << friend_name << " lately? \n";
	keep_window_open();
	return 0;
}


However, after I input the string "letter_text" the program seems to crash. If I remove the input for the letter text, it works as it should, but I am not able to enter any text in as the drill suggests I should be able to. How do I create an input space between the time it asks for the second name, and the output of the "Dear, addressee" line?


What makes you think it crashes? Does it show an error message? Are each input one word only?
closed account (48T7M4Gy)
Everything runs OK provide you add the relevant #includes
I input the text for the letter_text string, and then the program just closes. I assume thats a crash.
closed account (48T7M4Gy)
Put your complete code, with code tabs, and I'll have a look.
What are you typing in?
That is the complete code. I have all the necessary includes. Not trying to type anything in particular, just anything at all.
closed account (48T7M4Gy)
Yeah pls put the whole code up so I can run it here by pressing the gear wheel.
Header file contains: ( I did not write this, this was provided with the book )

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
std_lib_facilities.h
*/

/*
simple "Programming: Principles and Practice using C++ (second edition)" course header to
be used for the first few weeks.
It provides the most common standard headers (in the global namespace)
and minimal exception/error support.

Students: please don't try to understand the details of headers just yet.
All will be explained. This header is primarily used so that you don't have
to understand every concept all at once.

By Chapter 10, you don't need this file and after Chapter 21, you'll understand it

Revised April 25, 2010: simple_error() added

Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: <chrono>
Revised November 28 2013: add a few container algorithms
Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness
*/

#ifndef H112
#define H112 251113L


#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include <forward_list>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include <array>
#include <regex>
#include<random>
#include<stdexcept>

//------------------------------------------------------------------------------


//------------------------------------------------------------------------------

typedef long Unicode;

//------------------------------------------------------------------------------

using namespace std;

template<class T> string to_string(const T& t)
{
	ostringstream os;
	os << t;
	return os.str();
}

struct Range_error : out_of_range {	// enhanced vector range error reporting
	int index;
	Range_error(int i) :out_of_range("Range error: " + to_string(i)), index(i) { }
};


// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
	using size_type = typename std::vector<T>::size_type;

#ifdef _MSC_VER
	// microsoft doesn't yet support C++11 inheriting constructors
	Vector() { }
	explicit Vector(size_type n) :std::vector<T>(n) {}
	Vector(size_type n, const T& v) :std::vector<T>(n, v) {}
	template <class I>
	Vector(I first, I last) : std::vector<T>(first, last) {}
	Vector(initializer_list<T> list) : std::vector<T>(list) {}
#else
	using std::vector<T>::vector;	// inheriting constructor
#endif

	T& operator[](unsigned int i) // rather than return at(i);
	{
		if (i<0 || this->size() <= i) throw Range_error(i);
		return std::vector<T>::operator[](i);
	}
	const T& operator[](unsigned int i) const
	{
		if (i<0 || this->size() <= i) throw Range_error(i);
		return std::vector<T>::operator[](i);
	}
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {
	using size_type = std::string::size_type;
	//	using string::string;

	char& operator[](unsigned int i) // rather than return at(i);
	{
		if (i<0 || size() <= i) throw Range_error(i);
		return std::string::operator[](i);
	}

	const char& operator[](unsigned int i) const
	{
		if (i<0 || size() <= i) throw Range_error(i);
		return std::string::operator[](i);
	}
};


namespace std {

	template<> struct hash<String>
	{
		size_t operator()(const String& s) const
		{
			return hash<std::string>()(s);
		}
	};

} // of namespace std


struct Exit : runtime_error {
	Exit() : runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
	throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
	error(s + s2);
}

inline void error(const string& s, int i)
{
	ostringstream os;
	os << s << ": " << i;
	error(os.str());
}


template<class T> char* as_bytes(T& i)	// needed for binary I/O
{
	void* addr = &i;	// get the address of the first byte
	// of memory used to store the object
	return static_cast<char*>(addr); // treat that memory as bytes
}


inline void keep_window_open()
{
	cin.clear();
	cout << "Please enter a character to exit\n";
	char ch;
	cin >> ch;
	return;
}

inline void keep_window_open(string s)
{
	if (s == "") return;
	cin.clear();
	cin.ignore(120, '\n');
	for (;;) {
		cout << "Please enter " << s << " to exit\n";
		string ss;
		while (cin >> ss && ss != s)
			cout << "Please enter " << s << " to exit\n";
		return;
	}
}



// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s)	// write ``error: s and exit program
{
	cerr << "error: " << s << '\n';
	keep_window_open();		// for some Windows environments
	exit(1);
}

// make std::min() and std::max() accessible on systems with antisocial macros:
#undef min
#undef max


// run-time checked narrowing cast (type conversion). See ???.
template<class R, class A> R narrow_cast(const A& a)
{
	R r = R(a);
	if (A(r) != a) error(string("info loss"));
	return r;
}

// random number generators. See 24.7.



inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }

inline int randint(int max) { return randint(0, max); }

//inline double sqrt(int x) { return sqrt(double(x)); }	// to match C++0x

// container algorithms. See 21.9.

template<typename C>
using Value_type = typename C::value_type;

template<typename C>
using Iterator = typename C::iterator;

template<typename C>
// requires Container<C>()
void sort(C& c)
{
	std::sort(c.begin(), c.end());
}

template<typename C, typename Pred>
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
void sort(C& c, Pred p)
{
	std::sort(c.begin(), c.end(), p);
}

template<typename C, typename Val>
// requires Container<C>() && Equality_comparable<C,Val>()
Iterator<C> find(C& c, Val v)
{
	return std::find(c.begin(), c.end(), v);
}

template<typename C, typename Pred>
// requires Container<C>() && Predicate<Pred,Value_type<C>>()
Iterator<C> find_if(C& c, Pred p)
{
	return std::find_if(c.begin(), c.end(), p);
}

#endif //H112



My project contains

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Composite Assignment Operators.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "std_lib_facilities.h"

int main()
{
	string addressee;
	string letter_text;
	string friend_name;
	cout << "Enter the name of the person you would like to write to. \n";
	cin >> addressee;
	cout << "Dear, " << addressee << "\n";
	cin >> letter_text;
	cout << "Please enter the name of another friend. \n";
	cin >> friend_name;
	cout << "Have you seen " << friend_name << " lately? \n";
	keep_window_open();
	return 0;
}
closed account (48T7M4Gy)
Sorry, I now have a clearer picture of your problem.

Your program works for input with no spaces which is why it worked for me. What you need to do is modify your program to accept lines of text

Read this, it might help;

http://www.cplusplus.com/forum/articles/6046/
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
	string addressee;
	string letter_text;
	string friend_name;
	cout << "Enter the name of the person you would like to write to. \n";
	getline(cin, addressee);
	cout << "Dear, " << addressee << "\n";
	
	getline(cin, letter_text); // Problem is here. Cannot input text without crash.

	cout << "Please enter another friends name. \n";
	getline(cin,  friend_name);


	cout << "Have you seen " << friend_name << " lately? \n";
	keep_window_open();
	return 0;
}


You can run this here if you comment out keep_window_open();
Last edited on
The thing I don't understand though, why would the book have me do an exercise with a line of code I haven't learned yet?

I feel like I have to figure out another way of doing it without getline, since that is the purpose of the learning exercise.
Correct me if I'm wrong, but this is the drill from Programming Principles and Practice Using C++ .Up too this exercise you have not been exposed to std::getline() and shouldn't be expected to take input greater than a word in length. I believe the question is just referring to the introductory line. Such that you will only need to enter the text in code, so you can experiment with this, and come up with your own personalized letter head.
Last edited on
It is from that book. The drill must just be poorly worded, since it does say "type an introduction like Hi, how are you?"

If I understand what you all are saying the cin command im working with can only deal with strings of one word in length, which would be the purpose for a getline().

If this is true then I don't see how that part of the drill fits in.
While, I was hoping there was a different way to accomplish the same task. Recalling what the book had mentioned about memory and the space different variables took up; and looking at the broader picture of things, this wouldn't be the best set up in a much larger program, but it serves its purpose. As long as the letter text line is exactly 5 words.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int main()
{
	string addressee;
	string letter1;
	string letter2;
	string letter3;
	string letter4;
	string letter5;
	string friend_name;
	cout << "Enter the name of the person you would like to write to. \n";
	cin >> addressee;
	cout << "Dear, " << addressee << "\n";
	cin >> letter1 >> letter2 >> letter3 >> letter4 >> letter5;
	cout << "Please enter the name of another friend. \n";
	cin >> friend_name;
	cout << "Have you seen " << friend_name << " lately? \n";
	keep_window_open();
	return 0;
Yes, I believe it should be part of the std::cout and you should just enter a personalised output with the friends name. No need to take input for that part. Also you will soon learn an easier method so I wouldn't stay on it too long, especially if it's badly worded.


cin >> letter1 >> letter2 >> letter3 >> letter4 >> letter5;
As long as the letter text line is exactly 5 words.


Yes, this is a assumption violation and should be avoided.

Topic archived. No new replies allowed.