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.
#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";
}
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.
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";
elseif (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).
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.