7 Replies - 387 Views - Last Post: 11 February 2014 - 11:25 AM Rate Topic: -----

#1 MCroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 14

need help here in queue

Posted 10 February 2014 - 09:50 AM

#include<iostream>
#include<string>
#include<queue>
using namespace std;

int main()
{
	queue<string> name;
	queue<string> code;
	queue<int> nob;
	queue<float> wob;
	int choice;

	cout<<"\n";
    cout<<"\t\t\t\t- - Bag Handling System - - "<<endl;
	cout<<"\t\t- - By Aljon Alacapa, Paul Henston Ganut and Christian Morilla - - "<<endl;
	cout<<"\n\n\t\tType the number of the command you want to perform:"<<endl;
    cout<<endl;
    cout<<"\t\t\t1) Enter a record"<<endl;
    cout<<"\t\t\t2) Delete a record"<<endl;
    cout<<"\t\t\t3) Exit"<<endl;
    cout<<"\t\t\t"<<endl;
    cout<<"\t\t\t";
	cin>>choice;
	if (choice==1)
        {
            cout<<"Enter Customer name: ";
			string s;
			getline(cin,s);
			name.push(s);
			cout<<"Enter Number of Bag: ";
			int n;
			cin>>n;
			nob.push(n);
			cout<<"Enter Weight of Bag: ";
			float w;;
			cin>>w;
			wob.push(w);
			cout<<"Enter Customer name: ";
			string bc;
			getline(cin,bc);
			code.push(bc);
        }
       else if (choice==2)
        {

        }
        else if(choice==3)
        {
            system("cls");
            cout<<"Bye.";
        }
        else
        {
            cout<<"Invalid Input! Try again."<<endl;
        }
return 0;

}




Can someone help me here.
I'm having a problem in the output of this program.
when i run this and i start entering a customer's name and press enter the program jumps to the end it skips the input for the number of bag, weight of bag and bag code.
can someone help me find whats wrong here.

Is This A Good Question/Topic? 0
  • +

Replies To: need help here in queue

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: need help here in queue

Posted 10 February 2014 - 11:37 AM

If input is being skipped, it usually means that a newline character ('\n') is being left as the first character in the input stream. If this is the case and you try to get input from the stream, it will appear as though the input is being skipped. You can either input single characters to move past the newline character or you can use the istream::ignore() member function which takes the number of characters to "ignore" as an argument.

Something like the following usually works:
char ans = 'y';
int num;

while(ans == 'y' || ans == 'Y')
{
    cout << "Enter number: ";
    cin >> num;

    cout << endl << "Try Again? (y/n): ";
    cin >> ans;
    cin.get();
}


Because the 'ans' variable is a char, when you enter y or n and hit enter, the next character in the input stream will be a newline because of the user hitting enter. The last cin.get() call just inputs that newline and immediately ignores it. You can use the istream::peek() function to check what the next character is in the input stream. So if you might come across many newlines, you could use it in a loop:
while(cin.peek() == '\n')
{
    cin.get();
}


This post has been edited by vividexstance: 10 February 2014 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 MCroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 14

Re: need help here in queue

Posted 11 February 2014 - 07:44 AM

#include<iostream>
#include<string>
#include<queue>
using namespace std;

int main()
{
	queue<string> name;
	queue<string> code;
	queue<int> nob;
	queue<float> wob;
	int choice;
	
	cout<<"\n";
    cout<<"\t\t\t\t- - Bag Handling System - - "<<endl;
	cout<<"\t\t- - By Aljon Alacapa, Paul Henston Ganut and Christian Morilla - - "<<endl;
	cout<<"\n\n\t\tType the number of the command you want to perform:"<<endl;
    cout<<endl;
    cout<<"\t\t\t1) Enter a record"<<endl;
    cout<<"\t\t\t2) Delete a record"<<endl;
    cout<<"\t\t\t3) Exit"<<endl;
    cout<<"\t\t\t"<<endl;
    cout<<"\t\t\t";
	cin>>choice;
	if (choice==1)
        {
            cout<<"Enter Customer name: ";
			string s;
			getline(cin,s);
			name.push(s);
			cout<<"Enter Number of Bag: ";
			int n;
			cin>>n;
			nob.push(n);
			cout<<"Enter Weight of Bag: ";
			float w;;
			cin>>w;
			wob.push(w);
			cout<<"Enter Customer name: ";
			string bc;
			getline(cin,bc);
			code.push(bc);
        }
       else if (choice==2)
        {

        }
        else if(choice==3)
        {
            system("cls");
            cout<<"Bye.";
        }
        else
        {
            cout<<"Invalid Input! Try again."<<endl;
        }
return 0;

}




no sir this is what i mean.
try to run my code
after you run my code you will select what will you do on my program
so try to input 1 to enter a data
after that you will input the customers name
after you input the customers name it skips the other input.

look here

        {
            cout<<"Enter Customer name: ";
			string s;
			getline(cin,s);
			name.push(s);
			cout<<"Enter Number of Bag: ";
			int n;
			cin>>n;
			nob.push(n);
			cout<<"Enter Weight of Bag: ";
			float w;;
			cin>>w;
			wob.push(w);
			cout<<"Enter Customer name: ";
			string bc;
			getline(cin,bc);
			code.push(bc);
        }

after the line name.push(s);
it should be asking for the number of bag and wait for the user input but it skips it can you pinpoint whats wrong here.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: need help here in queue

Posted 11 February 2014 - 08:08 AM

Your entry code seems fine. However, you do ask for a second customer name at the end, which is probably wrong and what's leading to confusion.

Now, stop, and do NOT use parallel queues. Instead, use an actual record for your record:
#include<iostream>
#include<string>
#include<queue>
using namespace std;

struct Bag {
  string name, code;
  int num;
  float weight;
};

Bag getBagFromUser();

int main() {
  queue<string> bags;
  // ...
  if (choice==1) {
    bags.push(getBagFromUser());
  }


Was This Post Helpful? 0
  • +
  • -

#5 MCroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 14

Re: need help here in queue

Posted 11 February 2014 - 09:03 AM

i can't understand your code sir can you explain it for a bit
did you use a class?
can you tell me how to get the functions on every variables?
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: need help here in queue

Posted 11 February 2014 - 09:33 AM

Sorry, I did typo up there on the queue type.

With more comments
// this is a struct
// it contains multiple values
struct Bag {
  string name, code;
  int num;
  float weight;
};

// function prototype
// you write this later
Bag getBagFromUser();

int main() {
  queue<Bag> bags; // note the object type it contains
  // ...
  if (choice==1) {
    // call the function to get a bag object from user
    // and place the object returned in the queue
    bags.push(getBagFromUser());
  }
  //...
}

// implementation of function
Bag getBagFromUser() {
  Bag bag; // declare an instance of Bag
  // load the object with values from user
  cout<<"Enter Customer name: ";
  getline(cin, bag.name);
  // ... load the other values ...
  return bag;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 MCroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 14

Re: need help here in queue

Posted 11 February 2014 - 10:41 AM

#include<iostream>
#include<string>
#include<queue>
using namespace std;

struct Bag {
  string name, code;
  int num;
  float weight;
};

Bag getBagFromUser();



int main()
{
	queue<Bag> bags;
	int choice;
	
	cout<<"\n";
    cout<<"\t\t\t\t- - Bag Handling System - - "<<endl;
	cout<<"\t\t- - By Aljon Alacapa, Paul Henston Ganut and Christian Morilla - - "<<endl;
	cout<<"\n\n\t\tType the number of the command you want to perform:"<<endl;
    cout<<endl;
    cout<<"\t\t\t1) Enter a record"<<endl;
    cout<<"\t\t\t2) Delete a record"<<endl;
    cout<<"\t\t\t3) Exit"<<endl;
    cout<<"\t\t\t"<<endl;
    cout<<"\t\t\t";
	cin>>choice;
	if (choice==1)
        {
			bags.push(getBagFromUser());
        }
       else if (choice==2)
        {

        }
        else if(choice==3)
        {
            system("cls");
            cout<<"Bye.";
        }
        else
        {
            cout<<"Invalid Input! Try again."<<endl;
        }
return 0;

}

Bag getBagFromUser() {
  Bag bag; 
  cout<<"Enter Customer name: ";
  getline(cin, bag.name);
  cout<<"Enter Number of Bags: ";
  cin>>bag.num;
  cout<<"Enter Weight of Bags: ";
  cin>>bag.weight;
  cout<<"Enter Bag Code: ";
  getline(cin, bag.code);
  return bag;
}




this is my new code sir. but still the output is the same
try to run my code sir this is the output

Posted Image

can you examine my code again


also sir can you take a look at this i try to make a class queue but i got some errors
#include<iostream>
#include<cstdlib>
#include<string>
#include<queue>
#include <stdio.h>
#define MAX 10
#define LENGTH 20
using namespace std;
class Queue{
        int nob[MAX];
		string name[MAX][LENGTH];
		float wob[MAX];
		string code[MAX][LENGTH];
        int rear;
		char ch;
        int front;
    public:
        Queue();
        void pushNAME(char);
		void pushNOB(int);
		void pushWOB(float);
		void pushCODE(char);
		int popNOB();
		float popWOB();
		void popNAME();
		void popCODE();
};
Queue::Queue(){
    rear = 0;
    front = 0;
}
void Queue::pushNAME(char ch){
		cout<<"Enter Name of the Customer: ";
		cin.getline(ch[rear],LENGTH);
		name[rear] = ch[rear];
}
void Queue::pushNOB(int data){
		cout<<"Enter number of bags: ";
		cin>>data;
        nob[rear] = data;
}


void Queue::pushWOB(float w){
		cout<<"Enter weight of the bags: ";
		cin>>w;
		wob[rear] = w;
}
void Queue::pushCODE(char code[MAX][LENGTH]){
		cout<<"Enter the Bag Code: ";
		cin.getline(cd[rear],LENGTH)
		code[rear] = cd;
		rear++;
}

int main()
{
	Queue woi;
	cout<<"Tignan: \n";
	return 0;
}


that is my other code for queue but it has some errors mind if you take a look
i'm having trouble in implementing the queue in this 2d array. what do you think will be the easiest way?
the first one or this one?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: need help here in queue

Posted 11 February 2014 - 11:25 AM

You haven't implemented vividexstance's suggestion yet, which would help.

Your custom Queue class is a completely different and unrelated topic and should have a different post.

However, it would reasonably work the same way:
class Queue{
	static int MAX = 10;
	Bag items[MAX];
	int front, rear;
public:
	Queue();
	void push(const Bag &);
	bool pop(Bag &);
	bool empty() const;
};


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1