Problem Displaying a Struct

This is a discussion on Problem Displaying a Struct within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by rockstarpirate I'm pretty much a noobie when it comes to programming, so I don't really know how ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by rockstarpirate View Post
    I'm pretty much a noobie when it comes to programming, so I don't really know how I would pass an object as a parameter. If you could just point me in the right direction it would be much appreciated.
    Considering you did it several times in your program, that seems inconsistent somehow. But anyway: You pass things to a function by putting it in parentheses behind the function name, like function(this_object), or function(that_object), or whatever. In this case, you want to pass the class list, which is an array of Classes, so your function would take a Class whatever[].

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    The class that I'm taking doesn't really explain what the code is doing, it's more of a "if you want the program to do this, use this code."

    Anyway, I finally have all the errors gone, I had () instead of []. It works fine until I search for a class. I can search for one class fine, but if I enter an invalid number or "all," it doesn't perform correctly.

    Here's my code so you can test it.

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    struct CLASS
    {	
       string name;
       int number;
       string meets;
       int start;
       int end;
       string teacher;
       int size;
    };
    
    int InputClass(int cindex, CLASS Class[54]);
    int DisplayClass(int cindex, CLASS Class[54]);
    
    int main()
    {
    	int cindex=0;
    	int menu;
    	CLASS Class[54];
    //menu
    	do {
    		cout<<"Class Organizer"<<endl;
    		cout<<"1: Enter in your classes"<<endl;
    		cout<<"2: Search with class number"<<endl;
    		cout<<"3: Quit"<<endl;
    		cin>>menu;
    		
    		switch(menu) {
    			case 1:
    				InputClass(cindex, Class);
    				break;
    			case 2:
    				DisplayClass(cindex, Class);
    				break;
    			case 3:
    				break;
    			default:
    				cout<<"Invalid input, please enter a number given.";
    				break;
    		}
    
    	} while(menu);
    	
    	system ("PAUSE");
    	return 0;
    }
    
    
    int InputClass(int cindex, CLASS Class[54])
    {
    	char crerun='y';
    
    	do {
    		cout<<"Name of Class:"<<endl;
    		cin>>Class[cindex].name;
    		cout<<"Class Number:"<<endl;
    		cin>>Class[cindex].number;
    		cout<<"Meets on (MWF if it meets on Monday, Wednesday, and Friday):"<<endl;
    		cin>>Class[cindex].meets;
    		cout<<"Start time:"<<endl;
    		cin>>Class[cindex].start;
    		cout<<"End time:"<<endl;
    		cin>>Class[cindex].end;
    		cout<<"Teacher Name:"<<endl;
    		cin>>Class[cindex].teacher;
    		cout<<"Number of Students in Class:"<<endl;
    		cin>>Class[cindex].size;
    
    		cindex++;
    		cout<<"Would you like to add another class?"<<endl;
    		cin>>crerun; 
    	} while(crerun == 'Y' || crerun == 'y');
    	
    	return 0;
    }
    
    
    int DisplayClass(int cindex, CLASS Class[54])
    {
    	int clnumber;
    	int loop;
    	
    	cout<<"Enter class number to print or 'all' to print all classes";
    	cin>>clnumber;
    	if (clnumber=-858993460) {
    		for (loop=0; loop<=cindex; loop++) {
    			cout<<Class[loop].name<<endl;
    			cout<<Class[loop].number<<endl;
    			cout<<Class[loop].meets<<endl;
    			cout<<Class[loop].start<<endl;
    			cout<<Class[loop].end<<endl;
    			cout<<Class[loop].teacher<<endl;
    			cout<<Class[loop].size<<endl<<endl;
    		}
    	}
    	else {
    		for( loop=0; loop<=cindex; loop++){
    			if (Class[loop].number!=clnumber)
    				cout<<""<<endl;
    			else {
    				cout<<Class[loop].name<<endl;
    				cout<<Class[loop].number<<endl;
    				cout<<Class[loop].meets<<endl;
    				cout<<Class[loop].start<<endl;
    				cout<<Class[loop].end<<endl;
    				cout<<Class[loop].teacher<<endl;
    				cout<<Class[loop].size<<endl;
    				break;
    				}
    			}
    		
    			if (loop==cindex) {
    			cout<<"There is no class with that number.";
    			}
    
    			else 
    				cout<<"";
    	}
    
    	return 0;
    }//goes back to menu
    Last edited by rockstarpirate; 05-05-2008 at 10:26 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with struct?
    By cangel in forum C Programming
    Replies: 8
    Last Post: 09-28-2008, 12:35 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

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