please help me...

hello everyone
this is my coding. why when i want to put the name of the movie with spacing it become error..

#include <iostream>
#include <cstdlib>

using namespace std;
#define TRUE 1
#define FALSE 0

struct node
{ int data;
string name;
node *next;
} *head, *tail,user;

struct nod
{
int info;
struct nod *left;
struct nod *right;
};



void menu ();
void addFirst(node *newnode);
void displayList();
void booking();

nod* maketree (int);
nod* getnode(void);
nod* insert(nod*,int);
void setleft (nod*, int);
void setright (nod*, int);
void intrav (nod*);


int main()
{

int num;
nod* ptree;
ptree = NULL;

do
{
menu ();
cin>> num;
switch (num)
{
case 1:
{int number;
std::string name;
cout<<"CREATE THE NEW DETAILS\n"<<endl;

cout << "MOVIE TITLE: ";
cin>>name;

cout<<"MOVIE NO:";
cin>>number;

ptree = insert(ptree, number);
node *newnode;
newnode = new node;
newnode->data = number;
newnode->name = name;
newnode->next = NULL;
addFirst (newnode);
}break;
case 2:
{displayList();
}break;
case 3:
{ booking();
}break;
case 4:
{cout<<"Thank you for visiting"<<endl;
}break;

}} while(num!=4);
}



void menu ()
{

cout<<"\n CINEMA ONLINE"<<endl;
cout<<"\n\t MENU"<<endl;
cout<<"----------------------"<<endl;
cout<<" 1. ADD MOVIE"<<"\n 2. AVAILABLE MOVIE "<<endl;
cout<<" 3. BOOKING "<<"\n 4. EXIT "<<endl;

}

void addFirst (node *newnode) //linked list
{
if (newnode == NULL)
return;
else
{
if (head == NULL)
{
newnode->next == NULL;
head = newnode;
tail = newnode; }
else
{
newnode->next = head;
head = newnode; }
}
}
void displayList(){
node *current = head;
node *previous = NULL;
while (current != NULL) {
cout<< "MOVIE NO."<<current->data<<" " << current->name<< "\n";
previous = current;
current = current->next;

}
}





void booking(){
int movieChoice;
int seat, adult, child, total, total2;
char flag;


cout<<endl;
cout<<"MOVIE NO: ";
cin>>movieChoice;

switch (movieChoice){

case 1:
cout << "Enter number of adult customers:" << endl;
cin >> adult;

cout << "Enter number of child customers:" << endl;
cin >> child;

total = (adult * 10) + (child * 5);

// total2 will keep track of overall sales
total2 = total2 + total;

seat = seat + adult + child;
cout << "\n";
cout << "\n";
cout << "******************************************************" << endl;
cout << "Adult " << adult << endl;
cout << "Children " << child << endl;
cout << "TOTAL RM" << total << endl;
cout << "******************************************************" << endl;
cout << "\n";
cout << "\n";
break;

case 2:
cout << "Enter number of adult customers" << endl;
cin >> adult;

cout << "Enter number of child customers" << endl;
cin >> child;

total = (adult * 8) + (child * 4);

// total2 will keep track of overall sales
total2 = total2 + total;

seat = seat + adult + child;
cout << "\n";
cout << "\n";
cout << "******************************************************" << endl;
cout << "Adult " << adult << endl;
cout << "Children " << child << endl;
cout << "TOTAL RM" << total << endl;
cout << "******************************************************" << endl;
cout << "\n";
cout << "\n";

cout << "Enter Y to continue" << endl;
cin >> flag;

// if statement below will check whether to continue or quit
if (flag == 'y' || flag == 'Y')
{
//continue;
}
else
{
break;
}

}


}

nod* maketree(int x) //trees
{
nod* p;
p = getnode();
p->info = x;
p->left = NULL;
p->right = NULL;
return p;
}

nod* getnode(void)
{
nod* p;
p = new nod;
return p;
}

void setleft(nod* p, int x)
{
if(p == NULL)
cout<<"void insertion\n";
else if (p->left != NULL)
cout<<"invalid insertion\n";
else
p->left = maketree(x);

}
void setright(nod* p, int x)
{
if(p == NULL)
cout<<"void insertion\n";
else if (p->right != NULL)
cout<<"invalid insertion\n";
else
p->right = maketree(x);
}

void intrav(nod* tree)
{
if(tree != NULL){
intrav(tree->left);
cout<< tree->info<<"\n";
intrav(tree->right);
}
}

nod* insert(nod* p, int x)
{
if(p == NULL){
p=maketree(x);
return p;
}
else{
if(x < p->info)
p->left = insert(p->left, x);
else
p->right = insert(p->right, x);
return p;
}
}
cin only reads input up to a space or newline (enter/carriage return). When you enter in a movie title with a space, the first word gets stored in name, and the rest is passed on to the next cin. But since the next variable to be streamed into is an int, the cin fails, resulting in your error.
To include spaces, use getline(cin, stringObject);.
But this comes with a downside, too. Since you have cin's beforehand, there is a likely chance that there is a newline character hanging in the buffer which getline will catch. You can use [code]cin.ignore()[code] to catch these newlines before the getline instruction is reached. So, be careful that you clean up the buffer before calling getline.
how about this?

#include <iostream>
#include <cstdlib>
#include <string>



using namespace std;
#define TRUE 1
#define FALSE 0

struct node
{ int data;
std::string name;
node *next;
} *head, *tail,user;

struct nod
{
int info;
struct nod *left;
struct nod *right;
};



void menu ();
void addFirst(node *newnode);
void displayList();
void booking();

nod* maketree (int);
nod* getnode(void);
nod* insert(nod*,int);
void setleft (nod*, int);
void setright (nod*, int);
void intrav (nod*);


int main()
{
string name;
int num;
nod* ptree;
ptree = NULL;

do
{
menu ();
cin>> num;
switch (num)
{
case 1:
{
int number;
std::string name;

//std::string name;
cout<<"CREATE THE NEW DETAILS\n"<<endl;


std::cout << "MOVIE TITLE: ";
std::getline (std::cin,name);

cout<<"MOVIE NO:";
cin>>number;

ptree = insert(ptree, number);
node *newnode;
newnode = new node;
newnode->data = number;
newnode->name = name;
newnode->next = NULL;
addFirst (newnode);
}break;
case 2:
{displayList();
}break;
case 3:
{ booking();
}break;
case 4:
{cout<<"Thank you for visiting"<<endl;
}break;

}} while(num!=4);
}



void menu ()
{

cout<<"\n CINEMA ONLINE"<<endl;
cout<<"\n\t MENU"<<endl;
cout<<"----------------------"<<endl;
cout<<" 1. ADD MOVIE"<<"\n 2. AVAILABLE MOVIE "<<endl;
cout<<" 3. BOOKING "<<"\n 4. EXIT "<<endl;

}

void addFirst (node *newnode) //linked list
{
if (newnode == NULL)
return;
else
{
if (head == NULL)
{
newnode->next == NULL;
head = newnode;
tail = newnode; }
else
{
newnode->next = head;
head = newnode; }
}
}
void displayList(){
node *current = head;
node *previous = NULL;
while (current != NULL) {
cout<< "MOVIE NO."<<current->data<<" " << current->name<< "\n";
previous = current;
current = current->next;

}
}





void booking(){
int movieChoice;
int seat, adult, child, total, total2;
char flag;


cout<<endl;
cout<<"MOVIE NO: ";
cin>>movieChoice;

switch (movieChoice){

case 1:
cout << "Enter number of adult customers:" << endl;
cin >> adult;

cout << "Enter number of child customers:" << endl;
cin >> child;

total = (adult * 10) + (child * 5);

// total2 will keep track of overall sales
total2 = total2 + total;

seat = seat + adult + child;
cout << "\n";
cout << "\n";
cout << "******************************************************" << endl;
cout << "Adult " << adult << endl;
cout << "Children " << child << endl;
cout << "TOTAL RM" << total << endl;
cout << "******************************************************" << endl;
cout << "\n";
cout << "\n";
break;

case 2:
cout << "Enter number of adult customers" << endl;
cin >> adult;

cout << "Enter number of child customers" << endl;
cin >> child;

total = (adult * 8) + (child * 4);

// total2 will keep track of overall sales
total2 = total2 + total;

seat = seat + adult + child;
cout << "\n";
cout << "\n";
cout << "******************************************************" << endl;
cout << "Adult " << adult << endl;
cout << "Children " << child << endl;
cout << "TOTAL RM" << total << endl;
cout << "******************************************************" << endl;
cout << "\n";
cout << "\n";

cout << "Enter Y to continue" << endl;
cin >> flag;

// if statement below will check whether to continue or quit
if (flag == 'y' || flag == 'Y')
{
//continue;
}
else
{
break;
}

}


}

nod* maketree(int x) //trees
{
nod* p;
p = getnode();
p->info = x;
p->left = NULL;
p->right = NULL;
return p;
}

nod* getnode(void)
{
nod* p;
p = new nod;
return p;
}

void setleft(nod* p, int x)
{
if(p == NULL)
cout<<"void insertion\n";
else if (p->left != NULL)
cout<<"invalid insertion\n";
else
p->left = maketree(x);

}
void setright(nod* p, int x)
{
if(p == NULL)
cout<<"void insertion\n";
else if (p->right != NULL)
cout<<"invalid insertion\n";
else
p->right = maketree(x);
}

void intrav(nod* tree)
{
if(tree != NULL){
intrav(tree->left);
cout<< tree->info<<"\n";
intrav(tree->right);
}
}

nod* insert(nod* p, int x)
{
if(p == NULL){
p=maketree(x);
return p;
}
else{
if(x < p->info)
p->left = insert(p->left, x);
else
p->right = insert(p->right, x);
return p;
}
}
Instead of cin>>name; try getline(cin, name);
But be careful of any newline character '\n' remaining in the input buffer after a previous cin >> operation. You may want to get rid of it if necessary using cin.ignore();
Topic archived. No new replies allowed.