Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
30 views
in C++ by (111k points)
How do I limit the length of the user input string?

Please log in or register to answer this question.

1 Answer

0 votes
by (111k points)

If you want to limit the length of the user input string, you can manually check the length and take appropriate action. 

Here's an example code snippet:

#include <iostream>
#include <string>

int main() {
    std::string name;
    
    std::cout << "Enter your name (up to 10 characters): ";
    std::getline(std::cin, name);
    
    if (name.length() > 10) {
        std::cout << "Name exceeds the limit." << std::endl;
    } else {
        std::cout << "Hello, " << name << "!" << std::endl;
    }
    
    return 0;
}
 

In this example, the code checks if the length of the input string is greater than 10 characters and displays an error message if it is.

Related questions

0 votes
1 answer
0 votes
2 answers
asked May 30, 2023 in C++ by kvdevika (111k points)
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...