Runtime Error #3?

I'm using Visual Studio 2015 and I'm getting a Runtime Error #3 in a program I'm writing.

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
// chapter_3_drill.cpp : Defines the entry point for the console application.
// Osman Zakir
// 5 / 21 / 2016
// Programming: Principles and Practice using C++ 2nd Edition, Chapter 3 Drill
// This programs takes input for a letter being written to a friend
// It is a simple form letter based on user-input

#include "std_lib_facilities.h"

int main()
{
	cout << "Enter the name of the person you want to write to:\n";
	string first_name;
	cin >> first_name;
	cout << "Dear " << first_name << ",\n";

	cout << "\tHow are you doing?  I'm doing great.  I miss you.  We should probably get together with\n"
		<< "our friends sometime.  Really been a while.  It'd also be great to catch up on old times a bit.\n";
	cout << "Enter the name of another friend:\n";
	string friend_name;
	cin >> friend_name;
	cout << "Have you seen " << friend_name << " lately?\n";

	cout << "Enter an \'m\' if the recipient is male and \'f\' if the recipient is a female:";
	char friend_sex = 0;
	cin >> friend_sex;    // This is where the program crashes
	if (friend_sex == 'm')
	{
		cout << "If you see " << friend_name << " please ask him to call me.\n";
	}
	else if (friend_sex == 'f')
	{
		cout << "If you see " << friend_name << " please ask her to call me.\n";
	}

	cout << "Enter the recipient's age: ";
	int age;
	cout << "I hear you just had a birthday and you are " << age << " years old.\n";
	if (age <= 0 || age >= 110)
	{
		simple_error("you're kidding!");
	}

	keep_window_open();
}


These are the specs I'm following:
1. This drill is to write a program that produces a simple form letter based on
user input. Begin by typing the code from §3.1 prompting a user to enter
his or her first name and writing “Hello, first_name” where first_name is
the name entered by the user. Then modify your code as follows: change
the prompt to “Enter the name of the person you want to write to” and
change the output to “Dear first_name,”. Don’t forget the comma.
2. Add an introductory line or two, like “How are you? I am fine. I miss
you.” Be sure to indent the first line. Add a few more lines of your choosing
— it’s your letter.
3. Now prompt the user for the name of another friend, and store it in friend_
name. Add a line to your letter: “Have you seen friend_name lately?”
4. Declare a char variable called friend_sex and initialize its value to 0.
Prompt the user to enter an m if the friend is male and an f if the friend is
female. Assign the value entered to the variable friend_sex. Then use two
if-statements to write the following:
If the friend is male, write “If you see friend_name please ask him to
call me.”
If the friend is female, write “If you see friend_name please ask her to
call me.”
5. Prompt the user to enter the age of the recipient and assign it to an int
variable age. Have your program write “I hear you just had a birthday
and you are age years old.” If age is 0 or less or 110 or more, call simple_
error("you're kidding!") using simple_error() from std_lib_facilities.h.
6. Add this to your letter:
If your friend is under 12, write “Next year you will be age+1.”
If your friend is 17, write “Next year you will be able to vote.”
If your friend is over 70, write “I hope you are enjoying retirement.”
Check your program to make sure it responds appropriately to each kind
of value.
7. Add “Yours sincerely,” followed by two blank lines for a signature, followed
by your name.


Any help would be greatly appreciated. Thanks in advance.
Last edited on
You forgot to std::cin >> age. The runtime environment is complaining that you're trying to read the value of an uninitialized variable.
Actually, wait. My bad on the error name. It's Run-Time Check Failure #3 - T.

I just fixed the problem you two pointed out about variable age, but it's still crashing with the same error right when asking for the age.
Post the updated code.
Here:
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
// chapter_3_drill.cpp : Defines the entry point for the console application.
// Osman Zakir
// 5 / 21 / 2016
// Programming: Principles and Practice using C++ 2nd Edition, Chapter 3 Drill
// This programs takes input for a letter being written to a friend
// It is a simple form letter based on user-input

#include "std_lib_facilities.h"

int main()
{
	cout << "Enter the name of the person you want to write to:\n";
	string first_name;
	cin >> first_name;
	cout << "Dear " << first_name << ",\n";

	cout << "\tHow are you doing?  I'm doing great.  I miss you.  We should probably get together with\n"
		<< "our friends sometime.  Really been a while.  It'd also be great to catch up on old times a bit.\n";
	cout << "Enter the name of another friend:\n";
	string friend_name;
	cin >> friend_name;
	cout << "Have you seen " << friend_name << " lately?\n";

	cout << "Enter an \'m\' if the recipient is male and \'f\' if the recipient is a female: ";
	char friend_sex = 0;
	cin >> friend_sex;
	if (friend_sex == 'm')
	{
		cout << "If you see " << friend_name << " please ask him to call me.\n";
	}
	else if (friend_sex == 'f')
	{
		cout << "If you see " << friend_name << " please ask her to call me.\n";
	}

	cout << "Enter the recipient's age: ";
	int age;
	cout << "I hear you just had a birthday and you are " << age << " years old.\n";
	cin >> age;
	if (age <= 0 || age >= 110)
	{
		simple_error("you're kidding!");
	}

	keep_window_open();


For some reason, when I tried initializing age to 0, the value taken in by cin >> age also became 0. I took that part out before posting this.

But yeah, why did it do that?
C++ doesn't have the capacity to change the past. Input is not applied to variables retroactively.

This program will always output 42, regardless of what the user enters:
1
2
3
4
5
6
7
#include <iostream>

int main(){
    int x = 42;
    std::cout << x << std::endl;
    std::cin >> x;
}

This program echoes back whatever number the user enters:
1
2
3
4
5
6
7
#include <iostream>

int main(){
    int x = 42;
    std::cin >> x;
    std::cout << x << std::endl;
}


EDIT: To understand why the program couldn't work the way you think, you need to understand that std::cout doesn't necessarily point to a screen. For example, the environment could be configured to send standard output to a line printer. In fact, at one point this was the default, and the console is a virtual version of a printer. This is why the word "printing" is still used when sending to standard output.
How could the printed text change after the program gets user input?
Last edited on
Yeah, I get it all that. But then why did it not take input from the user for the age? Didn't it crash because of an undefined value being put into age?
Look again at your code:
37
38
    int age;
    cout << "I hear you just had a birthday and you are " << age << " years old.\n";

Line 37. age is defined, but is not initialised.
Line 38. age is printed out - still not initialised.

Think carefully about what you're saying and what you wrote.
1
2
3
4
5
6
int age;
// The value of age is undefined at this point.
cout << age; // An attempt is made to read an undefined value.
// Control flow never reaches this point.
cin >> age;
// The value of age is now defined (if the user entered something coherent). 
I suspect that it's actually crashing in simple_error() or keep_window_open(). The streams library buffers output so just because you aren't seeing some output, that doesn't mean that the code creating it hasn't executed.

Try adding cout.flush() just before line 40 of you latest posted code. This will flush the output.

And of course you still need to swap lines 38 and 39 so you get the value of age before printing it.
Yeah, I see now. Thanks. It worked with that.
Topic archived. No new replies allowed.