FAQ: Errors - Review

This community-built FAQ covers the “Review” exercise from the lesson “Errors”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hey
Can anyone explain what’s error here??

#include
#include <stdlib.h>
#include

int main() {

srand (time(NULL));
int fortune = rand / 10;

if (fortune == 0) {

std::cout << "Flattery will go far tonight.\n";

} else if (fortune == 1) {

std::cout << "Don't behave with cold manners.\n";

} else if (fortune == 2) {

std::cout << "May you someday be carbon neutral\n";

} else if (fortune == 3) {

std::cout << "You have rice in your teeth.\n";

} else if (fortune == 4) {

std::cout << "A conclusion is simply the place where you got tired of thinking.\n";

} else if (fortune == 5) {

std::cout << "No snowflake feels responsible in an avalanche.\n";

} else if (fortune == 6) {

std::cout << "He who laughs last is laughing at you.\n";

} else if (fortune == 7) {

std::cout << "If you look back, you'll soon be going that way.\n";

} else if (fortune == 8) {

std::cout << "You will die alone and poorly dressed.\n";

} else if (fortune == 9) {

std::cout << "The fortune you seek is in another cookie.\n";

}

}

1 Like

This should be the end result

#include
#include <stdlib.h>
#include

int main() {

srand (time(NULL));
int fortune = rand() % 10;

if (fortune == 0) {

std::cout << “Flattery will go far tonight.\n”;

} else if (fortune == 1) {

std::cout << "Don't behave with cold manners.\n";

} else if (fortune == 2) {

std::cout << "May you someday be carbon neutral\n";

} else if (fortune == 3) {

std::cout << "You have rice in your teeth.\n";

} else if (fortune == 4) {

std::cout << "A conclusion is simply the place where you got tired of thinking.\n";

} else if (fortune == 5) {

std::cout << "No snowflake feels responsible in an avalanche.\n";

} else if (fortune == 6) {

std::cout << "He who laughs last is laughing at you.\n";

} else if (fortune == 7) {

std::cout << "If you look back, you'll soon be going that way.\n";

} else if (fortune == 8) {

std::cout << "You will die alone and poorly dressed.\n";

} else if (fortune == 9) {

std::cout << "The fortune you seek is in another cookie.\n";

}

}

std::rand() gives you any digit number like 54856415,654681,etc, so dividing by 10 wont give you a single digit …
which results in failure of all if-else test cases …

ps : u must try printing std::rand() 10 times to get an idea about how it works , dont forget to add srand(time(NULL)) before it …

2 Likes

Thank you so much …

I’d hope there’s an explanation of the srand and rand() before throwing them at you.

Or at least a hint that you should Google them.

2 Likes

Hi guys I have a quick question, I am trying to make a program, that mimics a bank. You have to put in the correct pin and it gives you an “access granted” message. In the console I keep getting Access denied 10 times if I put in the wrong digit! Can anyone figure out what I keep doing wrong? Thanks. If you have any questions just let me know.

#include <iostream>

int main() {
  int x;

  
  std::cout << "Please enter your bank pin!";
  std::cin >> x;
  
  for (int y = 0; y < 10;) {
    if (x==4526) {
      std::cout << "Access Granted\n";
    }
    else {
      std::cout << "Access denied, please try again." << y+1 << " tries remaining\n";
y++;
    }
}
}

You need to find a keyword to break out of the loop if you successfully guessed the pin number.

i think its the last curly brace but if your getting an output then i think it should be that your variables or program logic is flawed.

1 Like

hey guys, please a quick question.
i was able to find the bugs and remove them but i am not getting exactly the “Flattery will go far tonight” fortune, i am getting other output put not the supposed output. so my question now is : is it part of a bug or is that how the program is meant to behave?

Thank you, I have already figured it out

Hey, I think that’s how program is meant to behave, but you could also add some conditions to print out only “Flattery will go far tonight”.

Thank you, this is EXACTLY why I’m checking the forums!!!

At this point, I can read some code & have a decent idea of what it will do, but “srand (time(NULL)” was completely unexpected. I can KIND OF see its purpose when it’s at the beginning of an example exercise, but I will most certainly be Googling it when the time comes for me to call on my knowledge of it.

Other responses with copied & pasted code, saying to print std::rand 10 times to get an idea about how it works (??!!?WHAT???!?) were a hair South of constructive.

Thanks again for saying this.

I don’t quite under stand what the srand(time(NULL)) line is for. My program is executing fine without it. Can someone explain what the significance of this is?

srand() function purpose

This is what is for, supposedly.

1 Like

I’m not sure that doing the exercise for you will help you in any way, you didn’t fix a single error here. Maybe you should redo the whole lesson to understand what needs to be done. If you have any real question, it will be a pleasure to help you out, but we can’t give the “answers” here, that is not the point of having a forum. Sorry…

Below is the code that I de-bugged for the exercise, when I output the code in the terminal it shoots out two of the fortunes instead of one. Can someone help explain? I had changed the “int fortune = rand() % 10” to 20 and got it to only output one fortune once but after that it went back to two fortunes.

#include
#include <stdlib.h>
#include

int main() {

srand (time(NULL));
int fortune = rand() % 20;

if (fortune == 0) {

std::cout << "Flattery will go far tonight.\n";

} else if (fortune == 1) {

std::cout << "Don't behave with cold manners.\n";

} else if (fortune == 2) {

std::cout << "May you someday be carbon neutral.\n";

} else if (fortune == 3) {

std::cout << "You have rice in your teeth.\n";

} else if (fortune == 4) {

std::cout << "A conclusion is simply the place where you got tired of thinking.\n";

} else if (fortune == 5) {

std::cout << "No snowflake feels responsible in an avalanche.\n";

} else if (fortune == 6) {

std::cout << "He who laughs last is laughing at you.\n";

} else if (fortune == 7) {

std::cout << "If you look back, you'll soon be going that way.\n";

} else if (fortune == 8) {

std::cout << "You will die alone and poorly dressed.\n";

} else if (fortune == 9); {

std::cout << "The fortune you seek is in another cookie.\n";

}

}

See anything odd here?

1 Like

I was getting no output and didn’t know why then I figured it out if (fortune = 0) apparently that isn’t an error for the compiler and the linker

1 Like

well the ; before the {