Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge | Conditional Statements
Introduction to C++

book
Challenge

Let's practice!

Aufgabe

Swipe to start coding

Your code accepts the sentence from the user. You should define if it is short, medium, or large.

  1. Accept the text message from the user as input and store it in the variable sentence.
  2. Using the statement if and function length() we check if our statement is large. If the sentence’s length is greater than 30, we print the message "The sentence is large!".
  3. Using the statement else if and function length() we check if our statement is medium. If the sentence’s length is greater than 10 (but less than 30), we print the message `"The sentence is medium!".
  4. Using the statement else and function length() we check if our statement is short. If the sentence’s length is less than 10, we print the message "The sentence is medium!".

Please, don’t forget to type the semicolon at the end of the lines.

Lösung

#include <iostream>
using namespace std;

int main() {
// Declare variables
string sentence;

// User input
cout << "Type the sentence: ";
cin >> sentence;

// Check the length
if (sentence.length() > 30) {
cout << "The sentence is large!";
} else if (sentence.length() > 10) {
cout << "The sentence is medium!";
} else {
cout << "The sentence is short!";
}
}

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 7
#include <iostream>
using namespace std;

int main() {
// Declare variables
string sentence

// User input
cout << "Type the sentence: ";
_ _ _

// Check the length
_ _ _
_ _ _
_ _ _
_ _ _
_ _ _
_ _ _
_ _ _
}
toggle bottom row
some-alt