>This may help
Yeah, Alex, it will surely help him, but you don't explain him what you've changed to make his code work ...
To the OP:
It's because of the getline function, so you'll have to flush the input stream each time to make your code work as expected, there's a thread about flushing the input stream, you can find it here
Yeah, after realizing how tired I was I also realized that I never answered the original question.
the getline function is accepting characters for your string but it doesn't seem like you're specifying a delimeter for the getline (by default it should be the newline char) nor are you flushing the input buffer after you push characters in it, so whan cin>>n is processed, characters are still in the buffer and then cin gets placed in a fail state.
Actually, Narue's post about this is far more intuitive than I could possibly explain. It wouldn't hurt to take the time to read it.
In this portion of code, after getting input from the user (such as the number for the option) I immediately ignore any characters that were put in the buffer then make a request to get characters in the input buffer until the newline delimiter is met then the characters are stored in the string.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class customerClass {
string masterArray[2][255];
public:
void getActions(int);
void saveInArray(int, string);
string readArray(int,int);
};
string customerClass::readArray(int x, int y) {
cout << masterArray[x][y];
}
void customerClass::saveInArray(int a, string sx) {
switch(a) {
case 1:
masterArray[a][0] = sx;
break;
}
}
void customerClass::getActions(int actions) {
string uInput;
switch(actions) {
case 1:
cout << "Options: (2) Add Name, (3) Add Address, (0) Exit: ";
break;
case 2:
cout << "Customer Name ";
cin.ignore(INT_MAX, '\n');
getline(cin, uInput, '\n');
saveInArray(1,uInput);
break;
case 3:
cout << "Customer Address: ";
cin.ignore(INT_MAX, '\n');
getline(cin, uInput, '\n');
break;
default:
// exit(1);
break;
}
}
int main() {
customerClass rect;
int n;
do {
rect.getActions(1);
cin >> n;
if(cin.fail()){
cout << "bad input!" << endl;
cin.clear();
cin.ignore(INT_MAX, '\n');
continue;
}
rect.getActions(n);
}while(n!=0);
return 0;
}