If I prompt the user to enter 6 numbers in one line, for example:
3 4 5 6 7 8
How can I store the first number in the string into variable Num1
, 2nd number into variable Num2
, 3rd number into variable Num3
, etc.? I.e., I need to prompt the user to input a single line containing 6 different numbers, and then split those 6 numbers into 6 different variables.
This is my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string num;
cout << "Enter one line containing 6 integers" << endl;
getline(cin, num)
return 0;
}
Not sure if string
is the right type to use.
And this method causes all 6 numbers to be stored into num
instead of splitting the 6 numbers up into separate variables.