0
Can't get this to work
Hey gang. I have been coding for a few days now and been reading Bjarne Stroustrup's "Programming principles and practice using C++". I'm stuck at the drill, where you have to make an interactive letter. I got through a little bit but the end part just doesn't seem to want to work, even though I apply the knowlege I acquired here and in the book as well. I would really appriciate if some one oculd point out the mistakes I made. Cheers. I uploaded the script on my profile since I can't seem to post it here.
11 Respuestas
+ 5
#1: `keep_window_open()` should be removed here, on SL's compiler. you can commented it out like this
// inline void keep_window_open(){ char ch; cin>>ch;}
and also line 69, 64, and 65.
~~~~~~
#2: Error @ line 48
if(age<==0||==110||>110) --> if ( age <= 0 || age >= 110)
~~~~~~
#3: Logical error @ line 39
if(friend_sex !=m||f) --> if (friend_sex != m || friend_sex != f)
And restructuring like so
if (friend_sex == m) {
cout<< "\nIf you see "<< friend_name<< " tell him I said hello.";
}
else if (friend_sex == f) {
cout<< "\nIf you see "<< friend_name<< " tell her I said hello.";
}
else if (friend_sex != m || friend_sex != f) {
cout<< "\nPlease use either 'm' or 'f' and try again.";
}
~~~~~~~~
Note: Now you should try some input like the below
Dawid
Mary
Rose
f
18
and get the output like so
Enter your first name: Now enter the name of the person you want to write to: Dear, Mary
How are you? Long time no see.
Have you seen lately?
What sex does your friend have? Specify with 'm/f':
If you see Rose tell her I said hello.
Enter the age of the recipient: 18 is a good age, enjoy it while you still can!
Yours sincerely
Important: As you can see the output isn't ideal because of the online compiler limitations. But the output on `cpp.sh` would be like this
Enter your first name: Dawid
Now enter the name of the person you want to write to: Mary
Dear, Mary
How are you? Long time no see.
Have you seen Rose
lately?
What sex does your friend have? Specify with 'm/f': f
If you see Rose tell her I said hello.
Enter the age of the recipient: 18
18 is a good age, enjoy it while you still can!
Yours sincerely
_____
http://cpp.sh/97eec
+ 3
"Programming principles and practice using C++"
Ah, uncle Bjarne! I love him.
I can't see any code in your profile. If you haven't created it, make a C++ project here
https://code.sololearn.com/#cpp
and then copy/paste your code there and save it.
+ 3
Glad to hear. No, it's harmless/useful in case of you run it in an IDE where you get prompted what to do next (or prevent the console from termination when you run the executable directly -- not debug mode), but it's really a pain in the ass to input multiple values with different types without any hint/prompt in online compilers. Uncle Bjarne did a great job! 8D
+ 2
Drill on Page 52, right?
+ 2
The program still has some logical error but it's getting late and I get lazy to pin all of them down! 8D
+ 2
Change
if ( age <= 0 || age >= 110 )
to
if ( age <= 0 && age >= 110 )
+ 1
Ah, right! Found that
+ 1
thanks for the quick answer i will look into the code in more derail tomorrow and update you. Have a good night!!!
+ 1
First of all thank you so much for looking over the code. I learned alot from my mistakes. It now works fine and I have updated it on my profile.
But besides the few minor errors, was the keep window open command wrong at this point or is it unnecessary to have in this example? because the keep window open command actually helped me out with my calculator which was closing before it could declare the answer.
Cheers,
Dawid
0
Was on private. Should be up now.
0
Nope, page 83.