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 #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

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 #community:Codecademy-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 …

1 Like

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.

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”.