Techiio-author
Started by Anik AdhikaryMay 7, 2022

Open
How can I include strings in conditional statements...in C++ ?

7 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 7 VIEWES SHARE

Example ..

string name;
getline (cin, name)
if (name != Anik)
cout << " Invalid /n";

But when I compile and run, the compiler says " Anik was not declared in this scope"....

Can anyone help me?

2 Replies

Techiio-commentatorPOULAMI SARKAR replied a month ago0 likes0 dislikes

C++ if Statement

The syntax of the if statement is:

if (condition) {
// body of if statement
}

The if statement evaluates the condition inside the parentheses ( ).

  • If the condition evaluates to true, the code inside the body of if is executed.
  • If the condition evaluates to false, the code inside the body of if is skipped.

Example 1: C++ if Statement

// Program to print positive number entered by the user
// If the user enters a negative number, it is skipped

#include <iostream>
using namespace std;

int main() {

int number;

cout << "Enter an integer: ";
cin >> number;

// checks if the number is positive
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}

cout << "This statement is always executed.";

return 0;
}

Output 1

Enter an integer: 5
You entered a positive number: 5
This statement is always executed.

When the user enters 5, the condition number > 0 is evaluated to true and the statement inside the body of if is executed.

Output 2

Enter a number: -5
This statement is always executed.

When the user enters -5, the condition number > 0 is evaluated to false and the statement inside the body of if is not executed.

Techiio-commentatorPOULAMI SARKAR replied a month ago0 likes0 dislikes
#include <iostream>
using namespace std;
int main() {
  int number;
  cout << "Enter an integer: ";
  cin >> number;
  // checks if the number is positive
  if (number > 0) {
    cout << "You entered a positive integer: " << number << endl;
  }
  cout << "This statement is always executed.";
  return 0;
}
You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance