Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 26th, 2006, 4:20 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
simple program problem

I have to write a program that prompts the user to enter a course by department course number semester taken and what the final grade was. Then the user has the option to print the courses entered or check the courses entered against the requirements. Right now i am working on entering the courses and printing the courses entered. I can enter and print courses fine the first time. However, if i try to enter 1 course and then say print that course out. Then i ask to add another course and print that it should print both courses however it only prints the second course entered. I'm very new to C++ i just have some java experience so any help would be very appreciated. Heres the code:
#include <iostream>
#include <string.h>

using namespace std;

struct classInfo
{
  char department[2];
  int courseNumber;
  char semesterTaken[5];
  char grade;
};

struct classInfo clInfo[100];
int loc = 0;
int i = 0;

void print()
{
  while(i < loc)
  {
    cout << clInfo[i].department << "  "  
         << clInfo[i].courseNumber << "  " 
         << clInfo[i].semesterTaken << "  " 
         << clInfo[i].grade
         << endl;
    i++;

  }
  cout << endl;
}

void addClass ()
{
  cout << "Please enter the department code: ";
  cin >> clInfo[loc].department;
  cout << "Please enter the course number: ";
  cin >> clInfo[loc].courseNumber;
  cout << "Please enter the semester: ";
  cin >> clInfo[loc].semesterTaken;
  cout << "Please enter the grade earned: ";
  cin >> clInfo[loc].grade;
  cout << endl;
  loc++;

}

int main()
{
  int option;
  
  while(option !=4)
  {
    cout << "Please make a selection: \n"
         << "1. Add a new class. \n"
         << "2. Print your current list of classes. \n"
         << "3. Check your courses against the requirements. \n"
         << "4. Quit \n"
         << "Your selection: ";
    cin >> option;   
    cout << endl;

   if(option == 1)
     addClass(); 

   if(option == 2)
     print();
  }

  return 0;
}
cwl157 is offline   Reply With Quote
Old Jan 26th, 2006, 4:28 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
have you tried..

void print()
{
  while(i < loc)
  {
    cout << clInfo[i].department << "  "  
         << clInfo[i].courseNumber << "  " 
         << clInfo[i].semesterTaken << "  " 
         << clInfo[i].grade
         << endl;
    i++;

  }
  cout << endl;
}printnum;

int main()
{
int i = 0;
addClass();
if(addClass)
{
i += 1;
printnum[i];
}

I'm really hungry so not really into it as much, i hope you understand what i'm trying to say.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Jan 26th, 2006, 5:20 PM   #3
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by jayme
have you tried..

SNIP

I'm really hungry so not really into it as much, i hope you understand what i'm trying to say.
Do you even know what you're doing when you type code in? Your code doesn't even come close to compiling, so no I don't "understand what you're trying to say".

First off...

void print()
{
  ...
}printnum;

What is printnum? I know you can create instance members of classes like that, but never seen anyone do that with a function.

Also...

addClass();
if(addClass)

First you use addClass like a function and then you use it like a variable. Which is it?

I realize you're probably learning C++, but the best way to learn it is:
  1. Read forums. [CHECK]
  2. Participate in discussions. [CHECK]
  3. Test your code before you post, instead of guessing [...]

Anyhow, to the original poster. The reason your print function isn't printing out everything is because you must set "i = 0" in the print function. You have a global variable (which is generally a bad idea) that is incremented up to loc, but it's never set back to the beginning.

There are other issues however. Department code can only be one character, because you've declared "department" to be 2 characters, but one of those characters is going to be occupied by \0 because you're using a char* string. You should probably stick to std::string if you're using C++, unless of course your professor does not allow it.
Jason Isom is offline   Reply With Quote
Old Jan 26th, 2006, 5:43 PM   #4
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Quote:
Do you even know what you're doing when you type code in? Your code doesn't even come close to compiling, so no I don't "understand what you're trying to say".
You don't have to be a dick about it. I said i wasn't into it, i'm not going to give him a complete answer. I was simply trying to point him in the right direction. You seemed to figure out what i was getting at by bringing up classes, so you did understand somewhat.

PS. And no, i don't have a clue what i'm doing when i type code in... but i still get it right more than half the time so it's ok.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Jan 26th, 2006, 5:47 PM   #5
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by jayme
You don't have to be a dick about it. I said i wasn't into it, i'm not going to give him a complete answer. I was simply trying to point him in the right direction. You seemed to figure out what i was getting at by bringing up classes, so you did understand somewhat.
I apologize. I definitely did not intend to come off as a dick. Sometimes I feel as though members just post code to increase their post count, but I guess the important thing is that all you were trying to do was to help the original poster.
Jason Isom is offline   Reply With Quote
Old Jan 26th, 2006, 5:51 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Jason, you were kinda harsh, but I agree when you say that code should have been checked first. There's nothing worse than teaching a newbie the wrong thing - I know it confuses the heck out of me when people do that.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 26th, 2006, 5:59 PM   #7
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Here's some code of how I would do it. It's not perfect, but then again I haven't programmed C++ in years (mostly a C# guy). I think by placing the code for the functions in the class it will suggest to the compiler that it should be inlined, but the functions are so simple it shouldn't matter that much.

The output could probably be cleaner. Maybe use some \t or setw(...) to make some nice preformatted output.

#include <iostream>
#include <string>
#include <vector>

class Transcript{
public:		
	void print()
	{
		for(unsigned int i = 0; i < m_Classes.size(); i++)
		{
			std::cout << m_Classes[i].m_Department << " "
				<< m_Classes[i].m_CourseNumber << " "
				<< m_Classes[i].m_SemesterTaken << " "
				<< m_Classes[i].m_Grade << std::endl;
		}	  
		std::cout << std::endl;
	}

	void addClass()
	{
		ClassInfo temp;
		
		std::cout << "Please enter the department code: ";
		std::cin >> temp.m_Department;
		std::cout << "Please enter the course number: ";
		std::cin >> temp.m_CourseNumber;
		std::cout << "Please enter the semester: ";
		std::cin >> temp.m_SemesterTaken;
		std::cout << "Please enter the grade earned: ";
		std::cin >> temp.m_Grade;
		std::cout << std::endl;
		
		m_Classes.push_back(temp);
	}

private:	
	struct ClassInfo
	{
		std::string m_Department;
		int m_CourseNumber;
		std::string m_SemesterTaken;
		char m_Grade;
	};
	std::vector<ClassInfo> m_Classes;
};

int main()
{
	Transcript myTranscript;
	
	int option = -1;  
	while(option !=4)
	{
		std::cout << "Please make a selection: \n"
			<< "1. Add a new class. \n"
			<< "2. Print your current list of classes. \n"
			<< "3. Check your courses against the requirements. \n"
			<< "4. Quit \n"
			<< "Your selection: ";
			
		std::cin >> option;   
		std::cout << std::endl;

		switch(option)
		{
		case 1:
			myTranscript.addClass();
			break;
		case 2:
			myTranscript.print();
			break;
		case 3:
			// Add Functionality
			break;
		case 4:
			std::cout << "Quitting..." << std::endl;
			break;
		default:
			std::cout << "Error: Choose again." << std::endl << std::endl;
		}	
	} 
}
Jason Isom is offline   Reply With Quote
Old Jan 26th, 2006, 6:03 PM   #8
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
ok i apologize. I should have made this clear in the beginning but i can't use classes to make this program. I can only use structs and subroutines.
cwl157 is offline   Reply With Quote
Old Jan 26th, 2006, 6:06 PM   #9
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 850
Rep Power: 4 The Dark is on a distinguished road
Without rearranging your whole program as Jason did, the simple fix is that you have a global variable called i which is not being reset when you print. You don't need a globale variable here and I would certainly recommend against ever having a global variable called i.

Delete the global variable i and change your print loop to look something like:

  for (int i = 0; i < loc; i++)
  {
   cout << clInfo[i].department << "  "  
         << clInfo[i].courseNumber << "  " 
         << clInfo[i].semesterTaken << "  " 
         << clInfo[i].grade
         << endl;
    i++;

  }

The int declaration inside the for loop declares it as local to the for (depending on your compiler, it might also be available outside the for loop, but it is still local to the function).
The Dark is offline   Reply With Quote
Old Jan 26th, 2006, 6:36 PM   #10
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by cwl157
ok i apologize. I should have made this clear in the beginning but i can't use classes to make this program. I can only use structs and subroutines.
Can you use std::string? Or do you have to use char arrays? If you have to use char arrays, can department code be more than one character?

Because...

Quote:
Originally Posted by Jason Isom
Anyhow, to the original poster. The reason your print function isn't printing out everything is because you must set "i = 0" in the print function. You have a global variable (which is generally a bad idea) that is incremented up to loc, but it's never set back to the beginning.

There are other issues however. Department code can only be one character, because you've declared "department" to be 2 characters, but one of those characters is going to be occupied by \0 because you're using a char* string. You should probably stick to std::string if you're using C++, unless of course
your professor does not allow it.
You're using std::cin for input into a character array without any thought about how many characters a user will type. If they type more than 1 (because you're using char department[2]) it will actually cause a out of bounds error. When you std::cout the department, it will actually keep printing random stuff until it encounters a null-terminating character (\0). They occur often enough on disk that your program won't crash every single time, but you definitely should look into a version of getline. At least I think getline is what you're looking for.
Jason Isom is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC