FAQ: Loops - Introduction to Loops

This community-built FAQ covers the “Introduction to Loops” exercise from the lesson “Loops”.

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

Learn C++

FAQs on the exercise Introduction to Loops

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!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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!

I’m stuck, I need help. I know this isn’t apart of the exercise, but my calculator is only returning the actual third number of what I inputted, not the difference. If you run this code, you’ll see exactly what I’m talking about. Note: Please input “+” as your first operator, because that’s the part I’m working on and then “-”.

#include
#include

using namespace std;

int main() {
char op;
char opS;
double num1;
double num2;
double num3;

// Title
std::cout << "-Calculator-\n";


std::cout << "Enter your first number.\n";
std::cin >> num1;

std::cout << "Enter either of these operators: +, -, /, or *\n";
std::cin >> op;

std::cout << "Enter your second number.\n";
std::cin >> num2;

double storedNum1;

 switch(op)
{
    case '+':
        std::cout << "Answer:\n"<< num1+num2;
        num1+num2==storedNum1;
        std::cout << "\n* , /, + or - this number? If not, enter 'exit'\n";
        
        std::cin >> opS;
        
        switch(opS)
        {
        case '-':
		std::cout << "Enter your third number.";
		std::cin >> num3;
		std::cout << num3-storedNum1 << " is the answer.";
        	
		}
        
     	break;

    case '-':
        std::cout << "Answer:\n" << num1-num2;
        break;

    case '*':
        std::cout << "Answer:\n" << num1*num2;
        break;

    case '/':
        std::cout << "Answer:\n" << num1/num2;
        break;

    default:

        std::cout << "Operator isn't valid. Try again.\n";
        break;
}
char c;
std::cin >> c;
return 0;

}