-
String to Array
One thing I am wondering is how to I put a string of info into a multidimensional array?
Let me specify the array:
Now in one of my functions I need to input between 1 to 10 different names into that array.
I have tried three different ways, each being unsuccessful. Let me show you the ways I have tried. The first being the only one where I have got some result, but I need it to include spaces.
Code:
for (i = 0; i < 10; i++)
{
cin >> name[i];
...
}
Obviously that would only input everything up until a whitespace character.
I tried the following two methods which I though would've worked, but didn't...
Code:
#include <cstring>
...
for (i = 0; i < 10; i++)
{
gets(name[i]);
...
}
Code:
for (i = 0; i < 10; i++)
{
cin.getline(name[i], 80, '\n');
...
}
Here is a snippet of my code and it's output with effort 2.
Code:
void enter()
{
int i;
for (i = 0; i < 10; i++)
{
cout << "\n\nEnter employee's name: ";
gets(name[i]);
cout << "Enter phone number: ";
gets(phone[i]);
cout << "Enter number of hours worked: ";
cin >> hours[i];
cout << "Enter wage: ";
cin >> wage[i];
}
}
Code:
Enter employee's name: Enter phone number:
One more question, what's the difference between gets() and cin.getline() anyhow? Is there a better one to use?
I'm probably just doing something extremely stupid, but I'm still new at this so take it easy on me :p
-
You need to eat the newline character after inputting the wage. So:
Code:
cout << "\n\nEnter employee's name: ";
cin.getline(name[i], 80, '\n');
cout << "Enter phone number: ";
cin.getline(phone[i], 80, '\n');
cout << "Enter number of hours worked: ";
cin >> hours[i];
cout << "Enter wage: ";
cin >> wage[i];
cin.ignore(numeric_limits<streamsize>::max(), '\n');
>One more question, what's the difference between gets() and cin.getline() anyhow? Is there a better one to use?
If you're coding C++, use cin.getline(). I believe gets() also leaves the newline character at the end of the string. If you are using C, fgets() is preferred over gets().
-
Yes, I am coding in C++. The book I'm reading uses gets() and I read an online tut which uses cin.getline(). I was just wondering.
Code:
for (i = 0; i < 1; i++)
{
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\n\nEnter employee's name: ";
cin.getline(name[i], 80, '\n');
cout << "Enter phone number: ";
cin.getline(phone[i], 80, '\n');
cout << "Enter number of hours worked: ";
cin >> hours[i];
cout << "Enter wage: ";
cin >> wage[i];
}
}
I do that and it works correctly. Thanks swoopy. Although I don't really understand what the cin.ignore() thing does. Could you please explain?
-
>Although I don't really understand what the cin.ignore() thing does. Could you please explain?
When you do:
cin >> wage[i];
There is a newline character left in the buffer from when you hit the <ENTER> key. Then when you loop the:
cin.getline(name[i], 80, '\n');
reads this newline character before you have a chance to enter the next name.
The cin.ignore() will read characters either until it sees a newline, or until the input buffer is filled. In this case you could probably just do:
cin.ignore();
Which only ignores one character, but the other is more general.
One correction: I said I thought gets() appends a newline character to the string. Actually it's fgets() which appends the newline character, not gets().
-
Cool stuff. Thanks for the assistance swoopy ;)