I'm still rather new to C++ (as the handle infers, I deal more with the tech environment than programming)
My question deals with getline in a regular Win32 Console App. The following is an excerpt of simplified code. It compiles just fine, but I get a Debug Error! in runtime if I execute more than one test at a time.
_________________________________________
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void test1()
{
[tab]string phoneNum;
[tab]cout << "Enter a phone number (x-xxx-xxx-xxxx): ";
[tab]cin >> phoneNum;
//Output format (xxx) xxx-xxxx
[tab]cout << "\nFormatted number: (" << phoneNum.substr(2, 3) << "
";
[tab]cout << phoneNum.substr(6, 13) << "\n\n";
}
void test2()
{
[tab]string input, newName;
[tab]cout << "Enter a name:\t";
[tab]getline(cin, input);
[tab]// formats input to Last, First Middle.
{
[tab].
[tab].
[tab].
}
[tab]cout << "\nThe result is:\t" << newName << endl;
}
void main()
{
[tab]test1();
[tab]test2();
}
____________________________________________
Again, this has been oversimplified for obvious reasons. test1() runs just fine. When test2() executes, Enter a name: is displayed, but a debug error occurs instantly not allowing input. If i run either test by itself, the both execute just fine. I'm assuming that istream is getting corrupted during the transition from test1 to test2.
I have already fixed the string library bug that affects getline, but it didn't help. Aside from using a character array instead ( cin.getline(char[], n) ), is there any reason why this might happen?
TIA,
pctek
My question deals with getline in a regular Win32 Console App. The following is an excerpt of simplified code. It compiles just fine, but I get a Debug Error! in runtime if I execute more than one test at a time.
_________________________________________
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void test1()
{
[tab]string phoneNum;
[tab]cout << "Enter a phone number (x-xxx-xxx-xxxx): ";
[tab]cin >> phoneNum;
//Output format (xxx) xxx-xxxx
[tab]cout << "\nFormatted number: (" << phoneNum.substr(2, 3) << "
[tab]cout << phoneNum.substr(6, 13) << "\n\n";
}
void test2()
{
[tab]string input, newName;
[tab]cout << "Enter a name:\t";
[tab]getline(cin, input);
[tab]// formats input to Last, First Middle.
{
[tab].
[tab].
[tab].
}
[tab]cout << "\nThe result is:\t" << newName << endl;
}
void main()
{
[tab]test1();
[tab]test2();
}
____________________________________________
Again, this has been oversimplified for obvious reasons. test1() runs just fine. When test2() executes, Enter a name: is displayed, but a debug error occurs instantly not allowing input. If i run either test by itself, the both execute just fine. I'm assuming that istream is getting corrupted during the transition from test1 to test2.
I have already fixed the string library bug that affects getline, but it didn't help. Aside from using a character array instead ( cin.getline(char[], n) ), is there any reason why this might happen?
TIA,
pctek