Simplify: How Do I Make This Simpler?

Ok, so I'm trying to create a sort of letter-generator program. I find myself having to copy paste results over and over.

I want to have input to the paragraph be taken, and then spit out the entire paragraph with the added material. Is there any way to do this without having to copy paste, copy paste, copy paste?

It's fairly annoying.

ALSO,
I'm having some issues with the IF statement. I need to return an error if the age is less than or equal to 0 or greater than 110.

Thanks in advance!

-Incline

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
#include "std_lib_facilities.h"

int main()
{
	cout<<"***Please enter the name of the person you wish to write to.***\n\n\n";
		string first_name;	// variable of type string
		cin>>first_name;	// read first_name
	cout<<"\n\n\nDear "<<first_name<<",\n";
	cout<<"    Hi, how are you? I miss you dearly. I am doing fine.\n\n\n";
	
	cout<<"***Please provide the name of another friend.***\n";
		string friend_name;
		cin>>friend_name;	// read friend_name
		cout<<"\n\n\nDear "<<first_name<<",\n";
	cout<<"    Hi, how are you? I miss you dearly. I am doing fine. ";
	cout<<"Have you seen "<<friend_name<<" lately?\n\n\n";
	
	char friend_sex = 0;
	cout<<"***Please enter 'm' for male friend or 'f' for female friend.***\n";
	cin>>friend_sex;
		if (friend_sex == 'm'){
			cout<<"\n\n\nDear "<<first_name<<",\n";
			cout<<"    Hi, how are you? I miss you dearly. I am doing fine. ";
			cout<<"Have you seen "<<friend_name<<" lately? ";
				cout<<"If you see "<<friend_name<<" please ask him to call me.\n";
		}
		if (friend_sex == 'f'){
			cout<<"\n\n\nDear "<<first_name<<",\n";
			cout<<"    Hi, how are you? I miss you dearly. I am doing fine. ";
			cout<<"Have you seen "<<friend_name<<" lately? ";
				cout<<"If you see "<<friend_name<<" please ask her to call me.\n";
		}
	int age;
	cout<<"\n\n\n ***How old is "<<first_name<<"?***\n\n\n";
	cin>>age;
	cout<<"\n\n\n";
		if (age=0,age>110){
			simple_error("you're kidding!");
		}			
		cout<<"I hear you just had a birthday and you're "<<age<<" years old!\n";
		
}
Well a better way to write the IF statement would me to write it as if(0<age>110) or even if(age<0 | age>110) that is an or
Last edited on
None of the methods suggested will work. The correct way to write "if age is less than or equal to 0 or greater than or equal to 110" is age <= 0 || age >= 110.

@TC: Your current code uses the comma operator, which does combine the results of the two expressions; it just returns the last one.

@twiggystardust: Your first example will evaluate 0<age and get true or false, then see if true of false is greater than 110, which is not what you want. Your second example uses |, the bitwise or operator, not the logical one. The latter will give the desired result.
@Zhuge, thanks for your clarification. You're right, it worked.

BUT

I was still curious about finding a way to tie all of my results together without having to copy paste them all in the code.

For example:

I ask for input A.
Puts out input A.
I ask for input B.
Puts out input AB.

This is for building the letter. Any ideas?
Last edited on
You could avoid repetition by limiting how much of the code is dependent upon the if/else.
For example, this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    if (friend_sex == 'm')
    {
        cout << "\n\n\nDear " << first_name << ",\n";
        cout << "    Hi, how are you? I miss you dearly. I am doing fine. ";
        cout << "Have you seen " << friend_name << " lately? ";
        cout << "If you see " << friend_name << " please ask him to call me.\n";
    }
    
    if (friend_sex == 'f')
    {
        cout << "\n\n\nDear " << first_name << ",\n";
        cout << "    Hi, how are you? I miss you dearly. I am doing fine. ";
        cout << "Have you seen " << friend_name << " lately? ";
        cout << "If you see " << friend_name << " please ask her to call me.\n";
    }


could be replaced by this:
1
2
3
4
5
6
7
8
9
10
11
    string pronoun = "it";
    
    if (friend_sex == 'm')
        pronoun = "him";
    else if (friend_sex == 'f')
        pronoun = "her";
        
    cout << "\n\n\nDear " << first_name << ",\n";
    cout << "    Hi, how are you? I miss you dearly. I am doing fine. ";
    cout << "Have you seen " << friend_name << " lately? ";
    cout << "If you see " << friend_name << " please ask " << pronoun << " to call me.\n";

(well, the code isn't strictly identical as I added a third option).
Last edited on
@Chervil, great idea for shortening that part of the code!

I still need to know if there is a way to build as I have mentioned.
One thing that should be considered is splitting the code into separate functions. For example in the original post you said this:
I want to have input to the paragraph be taken, and then spit out the entire paragraph with the added material. Is there any way to do this without having to copy paste, copy paste, copy paste?

There are one or two possible functions you could create here.
First, getting the input to the paragraph - this could be done as now, in main or you might make that a separate function.

Secondly, output of the actual paragraph. I would definitely make this a separate function, with the required information passed as parameter strings.

There are so many options here, depending on how flexible you want the text to be, but one function for each paragraph seems a reasonable starting point.
Thank you Chervil, greatly appreciated.

-Incline
Topic archived. No new replies allowed.