User Input in the Terminal

Multiple Inputs on a Single Line in C++

Is it possible to input multiple values in a single line using std::cin?

Abstract art representing computer programming

Yes, it's absolutely possible to input multiple values in a single line using std::cin.

This is actually one of the convenient features of std::cin - it can handle multiple inputs separated by whitespace (spaces, tabs, or newlines). Let's explore how to do this effectively:

Basic Multiple Input

Here's a simple example of reading multiple values from a single line:

#include <iostream>
#include <string>

int main(){
  int age;
  std::string name;
  double height;

  std::cout <<
    "Enter your name, age, and height: ";
  std::cin >> name >> age >> height; 

  std::cout << "Name: " << name << "\n";
  std::cout << "Age: " << age << "\n";
  std::cout << "Height: " << height <<
    " meters\n";
}

In this example, the user can enter something like "John 25 1.75" on a single line, and std::cin will distribute these values to the appropriate variables.

Enter your name, age, and height: John 25 1.75
Name: John
Age: 25
Height: 1.75 meters

Handling Input with Spaces

The basic approach works well for simple types, but what if we want to input a full name with spaces? We can combine std::cin with std::getline():

#include <iostream>
#include <string>
#include <sstream>

int main(){
  std::string fullName;
  int age;
  double height;

  std::cout <<
    "Enter your full name, age, and height: ";
  std::getline(std::cin, fullName, ','); 
  std::cin >> age >> height;

  std::cout << "Full Name: " << fullName <<
    "\n";
  std::cout << "Age: " << age << "\n";
  std::cout << "Height: " << height <<
    " meters\n";
}

Here, we use std::getline() to read the full name up to a comma, then use std::cin for the remaining values. The user would input something like "John Doe, 25 1.75".

Enter your full name, age, and height: John Doe, 25 1.75
Full Name: John Doe
Age: 25
Height: 1.75 meters

Using a String Stream

For more complex input parsing, we can use a string stream:

#include <iostream>
#include <string>
#include <sstream>

int main(){
  std::string input;
  std::string name;
  int age;
  double height;

  std::cout <<
    "Enter name, age, and height (separated by "
    "commas): ";
  std::getline(std::cin, input);

  std::istringstream iss(input); 
  std::getline(iss, name, ',');
  iss >> age;
  iss.ignore(); // Ignore the comma
  iss >> height;

  std::cout << "Name: " << name << "\n";
  std::cout << "Age: " << age << "\n";
  std::cout << "Height: " << height <<
    " meters\n";
}
Enter name, age, and height (separated by commas): John,25,1.75
Name: John
Age: 25
Height: 1.75 meters

This approach gives you more control over how the input is split and parsed. It's particularly useful when dealing with more complex input formats or when you need to do additional validation.

Remember, when working with multiple inputs, always consider potential input errors and implement appropriate error handling to make your program robust.

This Question is from the Lesson:

User Input in the Terminal

This lesson introduces the fundamentals of capturing user input, using std::cin and std::getline

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

User Input in the Terminal

This lesson introduces the fundamentals of capturing user input, using std::cin and std::getline

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 57 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved