Why the cin command does not working in my code?

Why the cin command doesn't working inside the 'if' statement? Compiler just ignores this command.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;
int main(){
    string a;
    int c;
    int d;
    cin >> a;
    if (a == "b") {
        cin >> c;
        cout <<  c*d;
     }
     return 0;
}
Last edited on
Compiler says:
 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.

What is your test input?
Last edited on
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)
If your input is '2', then the string 'a' will contain "2" and that does not equal "b".
Hello am1127104,

I made some adjustments to your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

//using namespace std;  // <--- Best not to use.

int main()
{
	constexpr int 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.

Hope that helps,

Andy
I think you should use int a, not string a
nakami88 wrote:
I think you should use int a, not string a

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.

Why do you think it should be an int?
Topic archived. No new replies allowed.