int counter = 0;
do {
if (counter % 2 == 0) // counter is even
{
// read int, store it in list<int>
int num;
cin >> num;
int_list.push_back(num);
if (num == 0)
break; // i.e. "while (i != 0)" in original code
}
else // counter is odd
{
// read string, store it in list<string>
string str;
cin >> str;
string_list.push_back(str);
}
counter ++;
} while (true);
Not sure if there is sentinel logic for the string input, you can add that by breaking there, too.
% is "modulus", which you can think of as being the remainder.
Odd numbers and even numbers alternate, so you can use this to determine to ask for an int or a string.
Strictly speaking, you don't need the counter variable, you could alternate based on the current sum of the sizes of the two lists, but I thought this would be easier to digest.
Following you reply this is what I currently have.
#include <list>
#include <string>
#include <iostream>
using namespace std;
int main(int argv, char* argc[])
{
list<int> l;
list<string> s;
int counter = 0;
do {
if (counter % 2 == 0) //counter is even
{
//read int and store in list<int>
int num;
cin >> num;
l.push_back(num);
if (num == 0);
break;
}
else // counter is odd
{
//read string and store in list<string>
string str;
cin >> str;
s.push_back(str);
}
counter++;
} while (true);
}
However, when I try to run it, it exits after one input. I am not sure if I am doing something wrong.