PDA

View Full Version : C++ Help


six_storm
03-19-07, 01:02 AM
Hey guys. I've never really asked for any programming help so here goes. Here is my 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;
}
}


I'm having trouble trying to figure out a few things. Actually, I just need to find out why I can't compile my code. Notice anything? Guess two sets of eyes are better than one. My compiler notices problems around the bolded areas jsyk.

rhink
03-19-07, 01:38 AM
deque<Student> req[4]; // deques to store request information
Student stu;

stu=req[i];

Those aren't the same data types. You're declaring an array of 4 deque's, then assigning one of those deque's to a student.

six_storm
03-19-07, 12:11 PM
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? :D

uOpt
03-19-07, 12:54 PM
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.

six_storm
03-19-07, 01:38 PM
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.

That's what "stu" is for, holding information for just one student. The teacher gave us a "template" for this project so he did half of the work, also the deque<Student> req[4]; I guess I need to get it working with that, does that make sense?

Thanks for everyone's help btw, it doesn't go unnoticed. :D

Subtestube
03-19-07, 04:42 PM
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!

six_storm
03-22-07, 11:59 AM
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!!!