Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 03-19-07, 12:02 AM   #1
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default C++ Help

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;
  }
}
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.
six_storm is offline   Reply With Quote
Old 03-19-07, 12:38 AM   #2
rhink
Registered User
 
Join Date: Jul 2005
Posts: 434
Default Re: C++ Help

Quote:
deque<Student> req[4]; // deques to store request information
Student stu;
Quote:
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.
rhink is offline   Reply With Quote
Old 03-19-07, 11:11 AM   #3
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default Re: C++ Help

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?
six_storm is offline   Reply With Quote
Old 03-19-07, 11:54 AM   #4
uOpt
FreeBSD cheering section
 
uOpt's Avatar
 
Join Date: Nov 2004
Location: Boston, MA, USA
Posts: 609
Default Re: C++ Help

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
uOpt is offline   Reply With Quote
Old 03-19-07, 12:38 PM   #5
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default Re: C++ Help

Quote:
Originally Posted by Martin Cracauer
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.
six_storm is offline   Reply With Quote
Old 03-19-07, 03:42 PM   #6
Subtestube
Anisymbolic
 
Subtestube's Avatar
 
Join Date: Aug 2004
Location: Wellington, New Zealand
Posts: 1,365
Default Re: C++ Help

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.
Subtestube is offline   Reply With Quote
Old 03-22-07, 10:59 AM   #7
six_storm
Registered User
 
six_storm's Avatar
 
Join Date: Jun 2004
Location: Nashvegas
Posts: 11,575
Send a message via Skype™ to six_storm
Default Re: C++ Help

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!!!
six_storm is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:20 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.