-
C++ cin.getline help
This is part of my code and i'm stuck on the cin part i dont exactly remember how it went for arrays... i need to cin a name that contains whitespace but when i do that the program starts looping forever...or skips an entry. Now i remember its something like cin.getline() but i can't figure it out any help would be greatly appreciated thx!
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int MAX = 40;
void bubbleID(int id[], int max);
void bubbleName(string name[], int max);
void dsply();
int main()
{
int id[40];
string name[40];
char choice;
bool running = true;
int count = 0;
while(running)
{
dsply();
cin >> choice;
switch(choice)
{
case 'A':
case 'a':
cout <<"\n"<< count + 1 << ":\n\nEnter Student's CWID: ";
cin>>id[count];
cout << "\nEnter Student's Name: ";
cin>>name[count];
break;
-
do not use >> for entering strings with spaces - this operator stops on white space
use getline for this purpose
-
Code:
getline(cin,name[count++]);
so like that right? but now that i put it in after i enter the number it just ends the program..
-
your >> operator leaves '\n' in the input stream
that is read by getline
use cin.ignore between using of operator >> and getline