Need help

This is a discussion on Need help within the C++ Programming forums, part of the General Programming Boards category; I need some help with this program. When I'm trying to print the data to the screen, something weird happens. ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    10

    Need help

    I need some help with this program. When I'm trying to print the data to the screen, something weird happens.
    also does anybody has any suggestions how to print just one class(in printOne function)?
    THANK YOU


    Code:
    #include <iostream> 
    #include "myHeader.h"
    #include <stdlib.h>
    #include <conio.h>
    using namespace std;
    
    //Function prototypes
    void newData();
    void printData();
    void printOne();
    void printAll();
    void quit();
    
    
    //global variables
    bool showMenu = true;
    const int NUMCLASSES = 10;
    int NUMCLASSES2;
    int iIndex;
    int cData[NUMCLASSES];
    
    int main()
    { 
         int function;
    	
    	//shows menu when showMenu is true.
    	while(showMenu == true)
    	{
    		system("cls");
    		cout << "==========================================" << endl;
    		cout << " Class Planner " << endl;     //Prints data on screen
    		cout << "==========================================" << endl;
    		cout << " What do you want to do " << endl;
    		cout << "1. Enter new data" << endl;
    		cout << "2. Print data to screen" << endl;
    		cout << "3. Exit" << endl;
    		cout << "==========================================" << endl;
    		cin>>function;
    
    		switch(function)   //using switch to enter a function
    		{
    		case 1:
    			{
    				newData();
    				break;
    			}
    		case 2:
    			{
    				printData();
    				break;
    			}
    		case 3:
    			{
    				quit();
    				break;
    			}
    		default:
    			{
    				break;
    			}
    		}
    
    
    	}
    
    	return 0;	
    }
    //quit function
    void quit()
    {
    	char cQuit;
    	
    	system("cls");
    	cout<<"Would you like to quit? (Y/N)\n";
    	cin>>cQuit;
    
    	if(cQuit == 'N' || cQuit == 'n')
    	{
    		system("cls");
    	}
    	else if(cQuit == 'Y' || cQuit == 'y')
    	{
    		system("cls");
    		showMenu = false;
    		cout<<"Please use this program again!"<<endl;
    	}
    	else
    	{
    		system("cls");
    		quit();
    	}
    
    }
    //asks the user to print new data
    void newData()
    {
    	system("cls");
    	cout<<"How many classes do you have? "; cin>>  NUMCLASSES2;
    	
    	cInfo cData[NUMCLASSES]; 
         //int iIndex; 
         for(iIndex = 0; iIndex < NUMCLASSES2; iIndex++)  
         {  
    		   cout<<endl;
    		   cin.ignore();
    		   cout << "Class name: ";
    		   cin.getline(cData[iIndex].className, NAMELENGTH);
               cout << "Class number: "; cin >> cData[iIndex].classNumber; 
               cin.ignore();
               cout << "Class meets on ('MWF' if it meet on Monday, Wednesday, and Friday): ";  
               cin.getline(cData[iIndex].classMeets, NAMELENGTH); 
               cout << "Start time: ";  cin >> cData[iIndex].classStart; 
               cout << "End time: ";  cin >> cData[iIndex].classEnd;
    		   cin.ignore();
    		   cout << "Teacher name: ";
    		   cin.getline(cData[iIndex].teacherName, (NAMELENGTH));
    		   cout << "Number of students: "; cin >> cData[iIndex].numberStudents;
    		   cout << endl;
         }  
         for(iIndex = 0; iIndex < NUMCLASSES2; iIndex++) 
         {  
               cout << endl;
    		   cout << "Class Name: " << cData[iIndex].className << endl; 
    		   cout << "Class Number: " <<  cData[iIndex].classNumber << endl; 
               cout << "Class meets on: " << cData[iIndex].classMeets << endl; 
               cout << "Start Time: " << cData[iIndex].classStart << endl;
    		   cout << "End Time: " << cData[iIndex].classEnd << endl;
    		   cout << "Teacher: " << cData[iIndex].teacherName << endl;
    		   cout << "Number of students: " << cData[iIndex].numberStudents << endl << endl;
         }
    	 system("PAUSE");
         
    	
    }
    
    void printData()
    {
    	cInfo cData[NUMCLASSES];
    	int dataOption;
    	
    	system("cls");
    	cout<<"Print Data to screen"<<endl;
    	cout<<"1. Print All"<<endl;
    	cout<<"2. Print a class"<<endl;
    	cin>>dataOption;
    
    	if(dataOption == 1)
    	{
    		system("cls");
    		for(iIndex=0; iIndex < NUMCLASSES2; iIndex++)
    		{
    		
    		cout <<  endl;
    		cout << "Class Name: " << cData[iIndex].className << endl; 
    		cout << "Class Number: " <<  cData[iIndex].classNumber << endl; 
            cout << "Class meets on: " << cData[iIndex].classMeets << endl; 
            cout << "Start Time: " << cData[iIndex].classStart << endl;
    		cout << "End Time: " << cData[iIndex].classEnd << endl;
    		cout << "Teacher: " << cData[iIndex].teacherName << endl;
    		cout << "Number of students: " << cData[iIndex].numberStudents << endl << endl;
    		}
    		system("PAUSE");
    	}
    	else if(dataOption == 2)
    	{
    		system("cls");
    	}
    	else
    	{
    		printData();
    	}
    }
    
    void printOne()
    {
    	system("cls");
    }
    
     //(contents of the file myHeader.h)
    #ifndef _MYHEADER_H
    #define _MYHEADER_H
    
    const int NAMELENGTH = 35;
    
    struct cInfo
    {
    	char className[NAMELENGTH];
    	int classNumber;
    	char classMeets[NAMELENGTH];
    	int classStart;
    	int classEnd;
    	char teacherName[NAMELENGTH];
    	int numberStudents;
    	
    };
    #endif

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> When I'm trying to print the data to the screen, something weird happens.
    Can you be more specific?

    >> does anybody has any suggestions how to print just one class(in printOne function)?
    The same as in printData except don't use a for loop and only do one object?

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    #include <stdlib.h>
    it should be
    #include <cstdlib>

    #include <conio.h> - do not use it

    Code:
    	cout<<"How many classes do you have? "; cin>>  NUMCLASSES2;
    	
    	cInfo cData[NUMCLASSES];
    You do not event check that NUMCLASSES2 is not greater than NUMCLASSES
    Better use vector of structs of appropriate length

    Try to avoid globas=ls - your usage of global array of int cData and local array of cInfo cData is confusing.

    Calling regular var with UpperCase - is confusing as well

    so - replace char arrays with std::string and arrays with std::vector
    remove global variables

    (and better remove system("cls") - users do not like, when the info from the screen just dissapears without a hint...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    10

    Question

    The printing error is becouse I'm declaring
    cData[NUMCLASSES] twice;

    "array in one function it doesn't populate the array in the other function."
    So, how can I make the array a global variable?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MKDWOLF View Post
    The printing error is becouse I'm declaring
    cData[NUMCLASSES] twice;

    "array in one function it doesn't populate the array in the other function."
    So, how can I make the array a global variable?
    The answer to the question you're literally asking is "declare it outside of any function (including main)". IOW: you already have a global variable named cData, so don't declare new ones in your functions because they will hide the global one.

    HOWEVER, you don't actually want a global variable. You want to have an array in main (better yet -- a vector), and you want to pass that array (better yet -- a vector) into your functions.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    HOWEVER, you don't actually want a global variable. You want to have an array in main (better yet -- a vector), and you want to pass that array (better yet -- a vector) into your functions.
    And how I can do that??
    I'm learning C++ for 4 months, so I need more info.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    #include <vector>
    int some_func(std::vector<int>& my_intvector_ref);
    
    int main()
    {
      std::vector<int> test;
      some_func(test);
      return 0;
    };
    There are also tutorials on this site that tell you how to use them.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    10

    Smile

    I'm pretty sure that I'm not supposed to use vectors(becouse I haven't learned about them yet). So, is any other way to make this program work.

    I think that I should declare the array only once as a global variable. And I should use that array for adding and printing data.
    But I don't know how to do that?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MKDWOLF View Post
    I think that I should declare the array only once as a global variable. And I should use that array for adding and printing data.
    But I don't know how to do that?
    I don't know why you think that you "don't know how to do that", considering that's what you've already bloody done. Take out all definitions of cData that aren't the global one at the top of the code et voila.

    If you ever feel the need to do it correctly (using non-global variables), then you'll want to look at passing parameters to functions.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21