Program Skips Second Input
My code skips the second input and uses the second digit from the first input as the input for the second input. Any idea how to fix this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
string number1;
string number2;
int main()
{
for (int j = 0; j<100; j++)
{
cout << "Please enter a two digit number: ";
cin >> number1[j];
cout << "Please enter another two digit number: ";
cin >> number2[j];
}
}
|
Your code engenders undefined behavior. number1
and number2
are empty. You cannot access elements of those strings that are not there.
How do I fix it?
What exactly are you trying to accomplish? Repeatedly storing different values in the same strings doesn't seem like it would be particularly useful.
I am using it to troubleshoot a small aspect of a larger and more useful program.
Topic archived. No new replies allowed.