Answers for "input string in c++"

1

how to deny string input in c++

while( ! cin >> x){ //While cin failed to stream the data; Reads input then checks if cin has failed
//Alternative:
/*
cin >> x;
while(cin.fail()){
*/
   cin.clear(); //Reset the flags, so you can use cin again
   cin.ignore(100, 'n'); //Empty the buffer
   cout << "Please enter a number!n";
/* If alternative is used:
   cin >> x; //Read input again
*/
}
Posted by: Guest on January-14-2021
17

input a string in c++

string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
Posted by: Guest on May-04-2020
7

C++ user input

int x; 
cout << "hurry, give me a number!: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "you picked: " << x << " !" // Display the input value

OR use:
getline >> (cin, variable-name);
instead of 
cin >> x;
Posted by: Guest on May-01-2020
1

how to input 2-d array in python

matrix = [input().split() for i in range(no_of_rows)] # if only row is given and the number of coloumn has to be decide by user
matrix= [[input() for j in range(no_of_cols)] for i in range(no_of_rows)] # if both row and coloumn has been taken as input from user
Posted by: Guest on April-27-2020
4

c++ reading string

#include <iostream>
#include <string>
string str;
getline(cin, str);
//str contains line
Posted by: Guest on January-26-2020

Python Answers by Framework

Browse Popular Code Answers by Language