Jump to content

C++ help run again

Go to solution Solved by Nineshadow,
12 minutes ago, derrickd241 said:

and here's the whole program if that helps

No because when I run this sure the run again displays but it displays one client in the array and it needs to loop main again so it will print them all at the same time then ask run again? then it will print them all at once again, so no this is the same problem I was having before

I didn't really understand tbh, but is this what you're looking for?

int main()
{
    bool	moreInput = false;
    do
    {
    string  input = "";
    priority_queue<Client, vector<Client>, CompareClients> pq;
    Client clientArray[5] =
    {
        { "John",	"Doe",	"719 444-5555" },
        { "Mary",	"Doe",	"719 444-5556" },
        { "Hari",	"Keri", "719 555-4444" },
        { "Mari",	"Mee",	"719 555-4445" },
        { "John",	"Doe",	"719 444-5557" }
    };
    for(Client i : clientArray)
        pq.push(i);

        while (!pq.empty())
        {
            Client c2 = pq.top();
            cout << "The Client being Popped = " <<
                 setw(10) << left << c2.fName << " " <<
                 setw(10) << left << c2.lName << " " <<
                 setw(14) << left << c2.areaCode << " " <<
                 setw(14) << left << c2.prefix << " " <<
                 setw(14) << left << c2.suffix << " " << endl;
            pq.pop();
        }
        moreInput = runAgain();
    }
    while (moreInput);
}

 

So, part of my assignment is to incorporate the runagain function into this program that takes an array and converts it to a vector so you can compare them and modify the order for which the array prints to the screen  most of the code is from my teacher but modified lol this is what i have so far but when I put while(moreInput); anywhere to loop it I get a syntax error


 

void main()
{
    {
        string  input = "";
        bool    moreInput = false;


        // create an STL queue named pq - and use the STL priority_queue adapter
        // use the struct Client (that you created above) as your data type
        // use a vector of Clients for your Queue - to hold your Clients
        // then use the CompareClients (above) for comparison criterion
        priority_queue<Client, vector<Client>, CompareClients> pq;

        // Create and Initialize an Array of 5 Client objects:
        Client clientArray[5] = {
            { "John",    "Doe",    "719 444-5555" },
            { "Mary",    "Doe",    "719 444-5556" },
            { "Hari",    "Keri", "719 555-4444" },
            { "Mari",    "Mee",    "719 555-4445" },
            { "John",    "Doe",    "719 444-5557" }
        };

        // Convert clients Array into a Vector in order to create an Iterator
        vector<Client> clients(begin(clientArray), end(clientArray));

        // create an iterator for the clients Vector named clientItr
        vector<Client>::iterator clientItr;

        // load each Client from the clients Vector into your Priority Queue using the Iterator
        // Your Priority Queue will use your comparison criterion to sort by last then first names
        for (clientItr = clients.begin(); clientItr != clients.end(); clientItr++)
            pq.push(*clientItr);

        // get the Top element of the Priority Queue and print it out
        // then pop (or dequeue) that Client out of the Queue


        while (!pq.empty())
        {
            Client c2 = pq.top();
            cout << "The Client being Popped = " <<
                setw(10) << left << c2.fName << " " <<
                setw(10) << left << c2.lName << " " <<
                setw(14) << left << c2.areaCode << " " <<
                setw(14) << left << c2.prefix << " " <<
                setw(14) << left << c2.suffix << " " << endl;
            pq.pop();
            moreInput = runAgain();
        }
    }
} while (moreInput);
bool runAgain()
{
    string answer = "";
    cout << "Do you want to run the program again? (y/n): ";
    cin >> answer;
    cout << endl << endl;
    return ((answer[0] != 'n') && (answer[0] != 'N'));
}

 

 

Link to comment
https://linustechtips.com/topic/604254-c-help-run-again/
Share on other sites

Link to post
Share on other sites

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/604254-c-help-run-again/#findComment-7830439
Share on other sites

Link to post
Share on other sites

and here's the whole program if that helps

// CS115-WK-09-WORKSHOP.cpp
// uses the Standard Template Library (STL)
// includes the STL Queue 
// uses a priority adapter for the queue 
// the queue expects to contain data type <struct Client>
// the queue will maintain a vector of Client structs
// the queue will use CompareClient overloaded operator ()
// CompareClient will return true if less-than

#include "stdafx.h"
#include <iomanip>
#include <iterator>
#include <iostream>
#include <string>
#include <queue>
using namespace std;

struct Client
{
	string	fName;
	string	lName;
	string  areaCode;
	string  prefix;
	string  suffix;
};

class CompareClients
{
public:
	bool operator()(Client& c1, Client& c2)
	{
		// if last name is less-than - insert before
		if (c1.lName.compare(c2.lName) < 0) // less-than
			return false;

		// if last name is greater-than - insert after
		else if (c1.lName.compare(c2.lName) > 0) // greater-than
			return true;

		// if last name is the same and first name is less-than - insert before
		else if ((c1.lName.compare(c2.lName) == 0) && (c1.fName.compare(c2.fName) < 0))
			return false;

		// if last name is the same and first name is greater-than - insert after
		else if ((c1.lName.compare(c2.lName) == 0) && (c1.fName.compare(c2.fName) > 0))
			return true;

		else if (c1.suffix.compare(c2.suffix) < 0)
			return false;

		else if (c1.suffix.compare(c2.suffix) > 0)
			return true;

		else if (c1.prefix.compare(c2.prefix) < 0)
			return false;

		else if (c1.prefix.compare(c2.prefix) > 0)
			return true;

		else if (c1.areaCode.compare(c2.areaCode) < 0)
			return false;

		else if (c1.areaCode.compare(c2.areaCode) > 0)
			return true;

		else
			return false;
	}
};

void main()
{
	{
		string  input = "";
		bool	moreInput = false;



		// create an STL queue named pq - and use the STL priority_queue adapter
		// use the struct Client (that you created above) as your data type 
		// use a vector of Clients for your Queue - to hold your Clients
		// then use the CompareClients (above) for comparison criterion
		priority_queue<Client, vector<Client>, CompareClients> pq;

		// Create and Initialize an Array of 5 Client objects:
		Client clientArray[5] = {
			{ "John",	"Doe",	"719 444-5555" },
			{ "Mary",	"Doe",	"719 444-5556" },
			{ "Hari",	"Keri", "719 555-4444" },
			{ "Mari",	"Mee",	"719 555-4445" },
			{ "John",	"Doe",	"719 444-5557" }
		};

		// Convert clients Array into a Vector in order to create an Iterator
		vector<Client> clients(begin(clientArray), end(clientArray));

		// create an iterator for the clients Vector named clientItr
		vector<Client>::iterator clientItr;

		// load each Client from the clients Vector into your Priority Queue using the Iterator
		// Your Priority Queue will use your comparison criterion to sort by last then first names
		for (clientItr = clients.begin(); clientItr != clients.end(); clientItr++)
			pq.push(*clientItr);

		// get the Top element of the Priority Queue and print it out
		// then pop (or dequeue) that Client out of the Queue



		while (!pq.empty())
		{
			Client c2 = pq.top();
			cout << "The Client being Popped = " <<
				setw(10) << left << c2.fName << " " <<
				setw(10) << left << c2.lName << " " <<
				setw(14) << left << c2.areaCode << " " <<
				setw(14) << left << c2.prefix << " " <<
				setw(14) << left << c2.suffix << " " << endl;
			pq.pop();
			moreInput = runAgain();
		}
	}
} while (moreInput);
bool runAgain()
{
	string answer = "";
	cout << "Do you want to run the program again? (y/n): ";
	cin >> answer;
	cout << endl << endl;
	return ((answer[0] != 'n') && (answer[0] != 'N'));
}

 

Link to comment
https://linustechtips.com/topic/604254-c-help-run-again/#findComment-7830537
Share on other sites

Link to post
Share on other sites

#include <bits/stdc++.h>
using namespace std;
struct Client
{
    string	fName;
    string	lName;
    string  areaCode;
    string  prefix;
    string  suffix;
};

class CompareClients
{
public:
    bool operator()(Client& c1, Client& c2)
    {
        if (c1.lName.compare(c2.lName) < 0)
            return false;

        else if (c1.lName.compare(c2.lName) > 0)
            return true;
        else if ((c1.lName.compare(c2.lName) == 0) && (c1.fName.compare(c2.fName) < 0))
            return false;
        else if ((c1.lName.compare(c2.lName) == 0) && (c1.fName.compare(c2.fName) > 0))
            return true;
        else if (c1.suffix.compare(c2.suffix) < 0)
            return false;
        else if (c1.suffix.compare(c2.suffix) > 0)
            return true;
        else if (c1.prefix.compare(c2.prefix) < 0)
            return false;
        else if (c1.prefix.compare(c2.prefix) > 0)
            return true;
        else if (c1.areaCode.compare(c2.areaCode) < 0)
            return false;
        else if (c1.areaCode.compare(c2.areaCode) > 0)
            return true;
        else
            return false;
    }
};
bool runAgain()
{
    string answer = "";
    cout << "Do you want to run the program again? (y/n): ";
    cin >> answer;
    cout << endl << endl;
    return ((answer[0] != 'n') && (answer[0] != 'N'));
}
int main()
{
    string  input = "";
    bool	moreInput = false;
    priority_queue<Client, vector<Client>, CompareClients> pq;
    Client clientArray[5] =
    {
        { "John",	"Doe",	"719 444-5555" },
        { "Mary",	"Doe",	"719 444-5556" },
        { "Hari",	"Keri", "719 555-4444" },
        { "Mari",	"Mee",	"719 555-4445" },
        { "John",	"Doe",	"719 444-5557" }
    };
    vector<Client> clients(begin(clientArray), end(clientArray));
    vector<Client>::iterator clientItr;
    for (clientItr = clients.begin(); clientItr != clients.end(); clientItr++)
        pq.push(*clientItr);
    do
    {
        while (!pq.empty())
        {
            Client c2 = pq.top();
            cout << "The Client being Popped = " <<
                 setw(10) << left << c2.fName << " " <<
                 setw(10) << left << c2.lName << " " <<
                 setw(14) << left << c2.areaCode << " " <<
                 setw(14) << left << c2.prefix << " " <<
                 setw(14) << left << c2.suffix << " " << endl;
            pq.pop();
            moreInput = runAgain();
        }
    }
    while (moreInput);
}

Like this?

 

Although I don't get why you don't just do this :

    priority_queue<Client, vector<Client>, CompareClients> pq;
    Client clientArray[5] =
    {
        { "John",	"Doe",	"719 444-5555" },
        { "Mary",	"Doe",	"719 444-5556" },
        { "Hari",	"Keri", "719 555-4444" },
        { "Mari",	"Mee",	"719 555-4445" },
        { "John",	"Doe",	"719 444-5557" }
    };
    for(Client i : clientArray)
        pq.push(i);

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/604254-c-help-run-again/#findComment-7830565
Share on other sites

Link to post
Share on other sites

14 minutes ago, Nineshadow said:

#include <bits/stdc++.h>
using namespace std;
struct Client
{
    string	fName;
    string	lName;
    string  areaCode;
    string  prefix;
    string  suffix;
};

class CompareClients
{
public:
    bool operator()(Client& c1, Client& c2)
    {
        if (c1.lName.compare(c2.lName) < 0)
            return false;

        else if (c1.lName.compare(c2.lName) > 0)
            return true;
        else if ((c1.lName.compare(c2.lName) == 0) && (c1.fName.compare(c2.fName) < 0))
            return false;
        else if ((c1.lName.compare(c2.lName) == 0) && (c1.fName.compare(c2.fName) > 0))
            return true;
        else if (c1.suffix.compare(c2.suffix) < 0)
            return false;
        else if (c1.suffix.compare(c2.suffix) > 0)
            return true;
        else if (c1.prefix.compare(c2.prefix) < 0)
            return false;
        else if (c1.prefix.compare(c2.prefix) > 0)
            return true;
        else if (c1.areaCode.compare(c2.areaCode) < 0)
            return false;
        else if (c1.areaCode.compare(c2.areaCode) > 0)
            return true;
        else
            return false;
    }
};
bool runAgain()
{
    string answer = "";
    cout << "Do you want to run the program again? (y/n): ";
    cin >> answer;
    cout << endl << endl;
    return ((answer[0] != 'n') && (answer[0] != 'N'));
}
int main()
{
    string  input = "";
    bool	moreInput = false;
    priority_queue<Client, vector<Client>, CompareClients> pq;
    Client clientArray[5] =
    {
        { "John",	"Doe",	"719 444-5555" },
        { "Mary",	"Doe",	"719 444-5556" },
        { "Hari",	"Keri", "719 555-4444" },
        { "Mari",	"Mee",	"719 555-4445" },
        { "John",	"Doe",	"719 444-5557" }
    };
    vector<Client> clients(begin(clientArray), end(clientArray));
    vector<Client>::iterator clientItr;
    for (clientItr = clients.begin(); clientItr != clients.end(); clientItr++)
        pq.push(*clientItr);
    do
    {
        while (!pq.empty())
        {
            Client c2 = pq.top();
            cout << "The Client being Popped = " <<
                 setw(10) << left << c2.fName << " " <<
                 setw(10) << left << c2.lName << " " <<
                 setw(14) << left << c2.areaCode << " " <<
                 setw(14) << left << c2.prefix << " " <<
                 setw(14) << left << c2.suffix << " " << endl;
            pq.pop();
            moreInput = runAgain();
        }
    }
    while (moreInput);
}

Like this?

 

Although I don't get why you don't just do this :


    priority_queue<Client, vector<Client>, CompareClients> pq;
    Client clientArray[5] =
    {
        { "John",	"Doe",	"719 444-5555" },
        { "Mary",	"Doe",	"719 444-5556" },
        { "Hari",	"Keri", "719 555-4444" },
        { "Mari",	"Mee",	"719 555-4445" },
        { "John",	"Doe",	"719 444-5557" }
    };
    for(Client i : clientArray)
        pq.push(i);

 

and here's the whole program if that helps

// CS115-WK-09-WORKSHOP.cpp
// uses the Standard Template Library (STL)
// includes the STL Queue 
// uses a priority adapter for the queue 
// the queue expects to contain data type <struct Client>
// the queue will maintain a vector of Client structs
// the queue will use CompareClient overloaded operator ()
// CompareClient will return true if less-than

#include "stdafx.h"
#include <iomanip>
#include <iterator>
#include <iostream>
#include <string>
#include <queue>
using namespace std;

struct Client
{
	string	fName;
	string	lName;
	string  areaCode;
	string  prefix;
	string  suffix;
};

class CompareClients
{
public:
	bool operator()(Client& c1, Client& c2)
	{
		// if last name is less-than - insert before
		if (c1.lName.compare(c2.lName) < 0) // less-than
			return false;

		// if last name is greater-than - insert after
		else if (c1.lName.compare(c2.lName) > 0) // greater-than
			return true;

		// if last name is the same and first name is less-than - insert before
		else if ((c1.lName.compare(c2.lName) == 0) && (c1.fName.compare(c2.fName) < 0))
			return false;

		// if last name is the same and first name is greater-than - insert after
		else if ((c1.lName.compare(c2.lName) == 0) && (c1.fName.compare(c2.fName) > 0))
			return true;

		else if (c1.suffix.compare(c2.suffix) < 0)
			return false;

		else if (c1.suffix.compare(c2.suffix) > 0)
			return true;

		else if (c1.prefix.compare(c2.prefix) < 0)
			return false;

		else if (c1.prefix.compare(c2.prefix) > 0)
			return true;

		else if (c1.areaCode.compare(c2.areaCode) < 0)
			return false;

		else if (c1.areaCode.compare(c2.areaCode) > 0)
			return true;

		else
			return false;
	}
};

void main()
{
	{
		string  input = "";
		bool	moreInput = false;



		// create an STL queue named pq - and use the STL priority_queue adapter
		// use the struct Client (that you created above) as your data type 
		// use a vector of Clients for your Queue - to hold your Clients
		// then use the CompareClients (above) for comparison criterion
		priority_queue<Client, vector<Client>, CompareClients> pq;

		// Create and Initialize an Array of 5 Client objects:
		Client clientArray[5] = {
			{ "John",	"Doe",	"719 444-5555" },
			{ "Mary",	"Doe",	"719 444-5556" },
			{ "Hari",	"Keri", "719 555-4444" },
			{ "Mari",	"Mee",	"719 555-4445" },
			{ "John",	"Doe",	"719 444-5557" }
		};

		// Convert clients Array into a Vector in order to create an Iterator
		vector<Client> clients(begin(clientArray), end(clientArray));

		// create an iterator for the clients Vector named clientItr
		vector<Client>::iterator clientItr;

		// load each Client from the clients Vector into your Priority Queue using the Iterator
		// Your Priority Queue will use your comparison criterion to sort by last then first names
		for (clientItr = clients.begin(); clientItr != clients.end(); clientItr++)
			pq.push(*clientItr);

		// get the Top element of the Priority Queue and print it out
		// then pop (or dequeue) that Client out of the Queue



		while (!pq.empty())
		{
			Client c2 = pq.top();
			cout << "The Client being Popped = " <<
				setw(10) << left << c2.fName << " " <<
				setw(10) << left << c2.lName << " " <<
				setw(14) << left << c2.areaCode << " " <<
				setw(14) << left << c2.prefix << " " <<
				setw(14) << left << c2.suffix << " " << endl;
			pq.pop();
			moreInput = runAgain();
		}
	}
} while (moreInput);
bool runAgain()
{
	string answer = "";
	cout << "Do you want to run the program again? (y/n): ";
	cin >> answer;
	cout << endl << endl;
	return ((answer[0] != 'n') && (answer[0] != 'N'));
}

No because when I run this sure the run again displays but it displays one client in the array and it needs to loop main again so it will print them all at the same time then ask run again? then it will print them all at once again, so no this is the same problem I was having before

Link to comment
https://linustechtips.com/topic/604254-c-help-run-again/#findComment-7830661
Share on other sites

Link to post
Share on other sites

12 minutes ago, derrickd241 said:

and here's the whole program if that helps

No because when I run this sure the run again displays but it displays one client in the array and it needs to loop main again so it will print them all at the same time then ask run again? then it will print them all at once again, so no this is the same problem I was having before

I didn't really understand tbh, but is this what you're looking for?

int main()
{
    bool	moreInput = false;
    do
    {
    string  input = "";
    priority_queue<Client, vector<Client>, CompareClients> pq;
    Client clientArray[5] =
    {
        { "John",	"Doe",	"719 444-5555" },
        { "Mary",	"Doe",	"719 444-5556" },
        { "Hari",	"Keri", "719 555-4444" },
        { "Mari",	"Mee",	"719 555-4445" },
        { "John",	"Doe",	"719 444-5557" }
    };
    for(Client i : clientArray)
        pq.push(i);

        while (!pq.empty())
        {
            Client c2 = pq.top();
            cout << "The Client being Popped = " <<
                 setw(10) << left << c2.fName << " " <<
                 setw(10) << left << c2.lName << " " <<
                 setw(14) << left << c2.areaCode << " " <<
                 setw(14) << left << c2.prefix << " " <<
                 setw(14) << left << c2.suffix << " " << endl;
            pq.pop();
        }
        moreInput = runAgain();
    }
    while (moreInput);
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/604254-c-help-run-again/#findComment-7830705
Share on other sites

Link to post
Share on other sites

58 minutes ago, Nineshadow said:

I didn't really understand tbh, but is this what you're looking for?


int main()
{
    bool	moreInput = false;
    do
    {
    string  input = "";
    priority_queue<Client, vector<Client>, CompareClients> pq;
    Client clientArray[5] =
    {
        { "John",	"Doe",	"719 444-5555" },
        { "Mary",	"Doe",	"719 444-5556" },
        { "Hari",	"Keri", "719 555-4444" },
        { "Mari",	"Mee",	"719 555-4445" },
        { "John",	"Doe",	"719 444-5557" }
    };
    for(Client i : clientArray)
        pq.push(i);

        while (!pq.empty())
        {
            Client c2 = pq.top();
            cout << "The Client being Popped = " <<
                 setw(10) << left << c2.fName << " " <<
                 setw(10) << left << c2.lName << " " <<
                 setw(14) << left << c2.areaCode << " " <<
                 setw(14) << left << c2.prefix << " " <<
                 setw(14) << left << c2.suffix << " " << endl;
            pq.pop();
        }
        moreInput = runAgain();
    }
    while (moreInput);
}

 

you're a beast lol thank you very much works flawlessly now :) although I didn't use some of your code at least the run again works now lol

Link to comment
https://linustechtips.com/topic/604254-c-help-run-again/#findComment-7831037
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×