In function 'int main()':
4:5: error: 'string' was not declared in this scope
7:5: error: 'cin' was not declared in this scope
10:9: error: 'cout' was not declared in this scope
Your compiler does not seem to enclose the standard library into namespace.
We'll fix that with std:: in order to see the "real" problem.
In function 'int main()':
10:25: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
Q: What is the value of 'd'?
A: Nobody knows.
Q: What is the value of 'c*d'?
A: We know 'c', but the answer remains: Nobody knows.
However,
* The "cin command" works as intended.
* The compiler does not ignore anything.
I am forgot add the 'd' value. int d = 12; My test input is '2'. I am using sololearn compiler which ignored this command. (cpp.sh isn't working in my browser)
#include <iostream>
#include <string>
//using namespace std; // <--- Best not to use.
int main()
{
constexprint MULTIPLIER{ 12 };
//std::string letter; // <--- OK, but not the best choice for the type.
char letter; // <--- An alternative. You do not need to define a string to use a single letter.
int number{};
std::cout << "\n Enter a letter (a or b): "; // <--- Added.
std::cin >> letter;
if (letter == 'b')
{
std::cout << "\n Enter a number: "; // <--- Added.
std::cin >> number;
std::cout << "\n The result of " << number << " * " << MULTIPLIER << " is " << number * MULTIPLIER; // <--- Changed.
}
else // <--- Added.
std::cout << "\n if statement bypassed.\n" << std::endl; // <--- Added.
return 0;
}
First off try to avoid using a single letter for a variable name. It can be confusing and hard to follow not only for you, but for others. The biggest point is to make your code easy to read and understand.
I defined "MULTIPLIER" as a constant because the program does not change this value. You do not have to make this a constant, but it seemed proper since its value does not change in the program. if the "constexpr" gives you a problem at compile time just use "const". When defining a constant variable it is generally accepted to use capital letters for the variable name, but this is not mandatory you can use lower case letters for the variable name. It is that the capital letters set it apart from other variables and help to remind you that it can not be changed.
Lines 14 and 19 are prompts to let the user know what needs to be entered. Without this you are staring at a flashing cursor on the screen wondering what to do.
Lines 21 along with lines 23 and 24 I added to help you understand how the program works.
The prompts can be changed to whatever you would like them to say.
The "else" part can be removed if you do not want to use it.
Why do you say that? The OP clearly intends to treat the input as a string, as they compare it with a string literal at line 9. Nowhere in the OP's code do they attempt to use it as an int.