Been developing for years under web based services like PHP and SQL, and I thought it would be a good idea that I come back and learn C++. Been enjoying it, but I got this one thing that is just driving me nuts.
In the do while loop after the cin is set on the first loop it goes into a death spin. In the 2nd example posted below it works just fine. Its as if the cin does not take a 2nd input after the first loop and just assumes its the same as the first input.
#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 ";
getline(cin, uInput);
saveInArray(1,uInput);
break;
case 3:
cout << "Customer Address: ";
getline(cin,uInput);
break;
default:
// exit(1);
break;
}
}
int main() {
customerClass rect;
int n;
rect.getActions(1);
do {
cin >> n;
rect.getActions(n);
}while(n!=0);
return 0;
}
2nd code test that works fine:
// number echoer
#include <iostream>
using namespace std;
int main ()
{
unsigned long n;
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
} while (n != 0);
return 0;
}