Hi, I am learning C++ and have started a simple console program to create and load text files for clients. I have created the two functions (createClient and openClient) which work when I call them directly in main(), however when I create a menu and use an if or switch statement the functions are called but they do not work properly. I've hit the limit of my knowledge with why this is happening.
I'll just paste the code for main() and createClient:
int main ()
{
createClient();
/*
int choice;
int fQuit = 0;
cout << "Choice: ";
cin >> choice;
while(!fQuit)
{
switch(choice)
{
case 1: createClient(); break;
case 2: openClient(); break;
case 3: fQuit = 1; break;
default: "Error in switch\n";
}
}
*/
}
I commented out the section that has the switch statement. When I call the function createClient() without an if or switch it works fine. When I uncomment and run the program I get the error message from my createClient function that it wasn't able to create the file. My createClient function is below:
void createClient()
{
cout << "**** Enter new client information ****\n\n";
// Enter a new business name
string businessName;
cout << "Business Name: ";
getline(cin,businessName);
// copies string contents into a char array
char *clientBusinessName = newchar[businessName.size()];
clientBusinessName[businessName.size()]=0;
memcpy(clientBusinessName,businessName.c_str(),businessName.size());
// creates a file from the business name.
ofstream File;
File.open(clientBusinessName);
if (File.is_open())
{
// Enter Domain Name
string clientDomainName;
cout << "Client Domain Name: ";
getline(cin,clientDomainName);
// Enter Client Contact Name:
string clientContactName;
cout << "Client Contact Name: ";
getline(cin,clientContactName);
File << "Business Name:\t\t" << clientBusinessName << endl;
File << "Domain Name:\t\t" << clientDomainName << endl;
File << "Contact Name:\t\t" << clientContactName << endl;
File.close();
}
else cout << "Error: Not able to save file!" << endl;
}
I think I'm a little confused(or totally blank) on what is happening when I call the function in a switch or if statement as opposed to directly...if that makes sense. I tried to recreate this little program by creating a pointer to a class on the free store with the two functions but the same thing happened. Any help in understanding what is going on would be great. Thanks!
Your problem actually has nothing to do with switch or if statements, but with the combination of using cin>> and getline().
cin>> ignores leading whitespace and leaves trailing whitespace in the stream. When you call cin >> choice;, it ignores any leading whitespace (ie spaces and carriage returns), stashes the value entered into the variable choice, but then leaves the trailing carriage return.
getline does just the opposite. Hence the carriage return that was left in the stream is the first thing it sees, and it bails right there, not entering any value into your string.
If you're going to mix the two, you should use cin.ignore() to flush out anything in the input stream prior to calling getline().
On another note, there's no need to copy the contents of the string into a char array. You can just use the c_str() method of the string class, which you ironically are already using in the memcpy to the char array. This will work just fine: File.open(businessName.c_str());
Thanks! I believe that solved my little problem, your help is greatly appreciated. I added cin.ignore() as the first function called in the createClient() function:
1 2 3 4 5
// Enter a new business name
cin.ignore();
string businessName;
cout << "Business Name: ";
getline(cin,businessName);
This seems to have solved my problem, and I took out the code to convert it to a char array. I'm sure there's a million better ways to write this program but I'll see how far I get. :)