Program Skips Second Input
Apr 18, 2017 at 12:01am
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];
}
}
| |
Apr 18, 2017 at 12:05am
Your code engenders undefined behavior. number1
and number2
are empty. You cannot access elements of those strings that are not there.
Apr 18, 2017 at 12:09am
How do I fix it?
Apr 18, 2017 at 12:22am
What exactly are you trying to accomplish? Repeatedly storing different values in the same strings doesn't seem like it would be particularly useful.
Apr 18, 2017 at 12:27am
I am using it to troubleshoot a small aspect of a larger and more useful program.
Topic archived. No new replies allowed.