|
|
#1 | |
|
Registered User
|
Hey guys. I've never really asked for any programming help so here goes. Here is my code:
Code:
// Jonathan Andrew Scott
// CSCI 2010 - Jiang Li
// Assignment 5
// Deque Assignment
#include <iostream>
#include <string>
#include <deque>
using namespace std;
struct Student // data type to store information
{
string name;
char category;
};
struct Advisor
{
int advcode;
string name;
int nstuadv; // number of students seen
};
int main()
{
deque<Student> req[4]; // deques to store request information
Student stu; // struct to hold info for one student
Advisor adv[4]; // structs to store advisor information
adv[0].advcode = 2918;
adv[0].name = "Higgins";
adv[0].nstuadv = 0;
adv[1].advcode = 4502;
adv[1].name = "Beard";
adv[1].nstuadv = 0;
adv[2].advcode = 3408;
adv[2].name = "Watson";
adv[2].nstuadv = 0;
adv[3].advcode = 5644;
adv[3].name = "Sherwood";
adv[3].nstuadv = 0;
// Initialize advisor information
// e.g. adv[0].advcode = 2918;
// adv[0].name = "Higgins";
// adv[0].nstuadv = 0;
char reqcode;
int advcode;
bool isValid;
cout << endl << "Request Code: ";
cin >> reqcode;
reqcode = toupper(reqcode);
while (reqcode != 'Q') // exit if request code is Q
{
if (reqcode == 'R') // request to see an advisor
{
cout << "Student Name: ";
getline(cin, stu.name);
cout << endl;
cout << "Category: ";
cin >> stu.category;
// 1. input student's name and category, e.g.,
// Student Name: Brown, Susan
// Category: O
if(stu.category == 'S')
{
req[0].push_back(stu);
}
else if(stu.category == 'W')
{
req[1].push_back(stu);
}
else if(stu.category == 'O')
{
req[2].push_back(stu);
}
else if(stu.category == 'T')
{
req[3].push_back(stu);
}
else
{
cout << "Invalid Category" << endl;
cout << "Category: ";
cin >> stu.category;
}
// 2. put (push_back) student's request into the appropriate deque
// based on the student's category
} // end if
else if (reqcode == 'A') // advisor available to see a student
{
cout << "Advisor Code: ";
cin >> advcode;
// 1. input advisor code
if(advcode == '2918')
{
stu = req[0].front();
cout << adv[0].name << " is advising " << stu.name << ". /n";
req[0].pop_front();
}
else if(advcode == '4502')
{
stu = req[1].front();
cout << adv[1].name << " is advising " << stu.name << ". /n";
req[1].pop_front();
}
else if(advcode == '3408')
{
stu = req[2].front();
cout << adv[2].name << " is advising " << stu.name << ". /n";
req[2].pop_front();
}
else if(advcode == '5644')
{
stu = req[3].front();
cout << adv[3].name << " is advising " << stu.name << ". /n";
req[3].pop_front();
}
else
{
cout << "Invalid Advisor Code, Try Again" << endl;
cout << "Advisor Code: ";
cin >> advcode;
}
// 2. Use this code to find the advisor, e.g., adv[2],
// then check req[2] (because req[i] holds requests for adv[i])
// If a student is waiting to see the advisor, i.e., adv[2] is not empty
// print the student's name and the advisor's name,
// and remove (pop_front) that student from the deque.
} // end else if
else
cout << "Invalid Request Code" << endl;
cout << endl << "Request Code: ";
cin >> reqcode;
reqcode = toupper(reqcode);
} // end while
// Print out information after receiving input 'Q'
for (int i=0; i<4; i++)
{
if (!req[i].empty())
{
stu=req[i];
cout << "The following students are waiting for advisor " << adv[i].name << endl;
cout << stu.name << endl;
// print students who are waiting to see an advisor in each deque
}
// print the total number of students seen by each advisor
cout << endl << adv[i].name << " has seen " << req[i].size() << "student(s).";
cout << endl;
}
}
|
|
|
|
|
|
|
#2 | ||
|
Registered User
Join Date: Jul 2005
Posts: 434
|
Quote:
Quote:
|
||
|
|
|
|
|
#3 |
|
Registered User
|
Hmmm . . . that makes sense, yet I still don't know what else I could do. I really don't know how else I could call the information (stu) from the deque. Any hints?
![]() |
|
|
|
|
|
#4 | |
|
FreeBSD cheering section
Join Date: Nov 2004
Location: Boston, MA, USA
Posts: 609
|
You want
deque<Student> req; That gives you a collection object that can hold many students and can be used for req[i]. The way you did it you created an array of deques.
__________________
My Unix benchmark results |
|
|
|
|
|
|
#5 | |
|
Registered User
|
Quote:
Thanks for everyone's help btw, it doesn't go unnoticed. ![]() |
|
|
|
|
|
|
#6 | |
|
Anisymbolic
Join Date: Aug 2004
Location: Wellington, New Zealand
Posts: 1,365
|
You need to iterate _through_ the members of the deque.
Deques are nice general STL containers so you can do this in two ways. In the first, you can treat the deque like an array, and just make a for loop to go through it, using req[i].size() as your loop end condition - i.e., access each student _in_ each deque using req[i][j] as a way to reference each thing. Alternatively, and in _much better_ STL style, you could use an iterator - make an iterator using deque<Student>::iterator iter = req[i].begin(), and iterate through the members from there. I'd give you code for that, but that's really doing all the work for you ![]() EDIT: Sorry, had .front(), where I should've had .begin(). More the fool me!
__________________
Dr Possible: Core 2 Duo E6400 on Gigabyte GA-965P-DS4. Galaxy GeForce 7600GT. 2GB Corsair XMS 2 DDR2-6400 RAM (CL5). ATi Theatre 550 Pro. Windows XP MCE. All stored in Piano black Antec Sonata II, with a broken door. Mobile: ASUS M2400N, Pentium M 1.5 GHz. 512 MB DDR RAM. Intel EXTREME graphics. Windows XP SP 2 / Ubuntu 5.10. Ridiculous DOES not have an 'e' in it. It comes from "ridicule" and has less than nothing to do with the colour red. |
|
|
|
|
|
|
#7 |
|
Registered User
|
Thanks for your help guys, the project was late and I had other projects to take care of. I never really figured it out but I'll know by next Monday what I did wrong. I actually got the program to compile but it would skip over the "Student Name" input and there was something wrong with the array or deque when displaying their info. Oh well. Thanks again guys!!!
|
|
|
|
![]() |
| Thread Tools | |
|
|