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; I'm having problems displaying a struct. The program recieves input of a person's class schedule, and the person can search ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Problem Displaying a Struct

    I'm having problems displaying a struct. The program recieves input of a person's class schedule, and the person can search for a class using the id number. I keep getting errors when it comes to displaying the search results. I know that it doesn't seem to recognize the struct, but i don't know how to fix it. Thanks for your time.

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int InputClass(int cindex);
    int DisplayClass(int cindex);
    
    struct CLASS
    {	
       string name;
       int number;
       string meets;
       int start;
       int end;
       string teacher;
       int size;
    };
    
    
    int main()
    {
    	int cindex=0;
    	int menu;
    //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);
    				break;
    			case 2:
    				DisplayClass(cindex);
    				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;
    		getline(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;
    		getline(cin,Class[cindex].meets);
    		cout<<"Start time:"<<endl;
    		cin>>Class[cindex].start;
    		cout<<"End time:"<<endl;
    		cin>>Class[cindex].end;
    		cout<<"Teacher Name:"<<endl;
    		getline(cin,Class[cindex].teacher);
    		cout<<"Number of Students in Class:"<<endl;
    		cin>>Class[cindex].size;
    
    		cindex++1;
    		cout<<"Would you like to add another class?"<<endl;
    		cin>>crerun; 
    	} while(crerun == 'Y' || crerun == 'y');
    	
    	return 0;
    }
    
    
    int DisplayClass(int cindex)
    {
    	int clnumber;
    	int loop;
    	
    	cout<<"Enter class number to print that 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
    Errors:

    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(74) : error C2143: syntax error : missing ';' before 'constant'
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(92) : error C2065: 'Class' : undeclared identifier
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(92) : error C2228: left of '.name' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(93) : error C2065: 'Class' : undeclared identifier
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(93) : error C2228: left of '.number' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(94) : error C2065: 'Class' : undeclared identifier
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(94) : error C2228: left of '.meets' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(95) : error C2065: 'Class' : undeclared identifier
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(95) : error C2228: left of '.start' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(96) : error C2065: 'Class' : undeclared identifier
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(96) : error C2228: left of '.end' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(97) : error C2065: 'Class' : undeclared identifier
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(97) : error C2228: left of '.teacher' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(98) : error C2065: 'Class' : undeclared identifier
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(98) : error C2228: left of '.size' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(103) : error C2228: left of '.number' must have class/struct/union
    type is ''unknown-type''
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(103) : error C3861: 'Class': identifier not found
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(106) : error C2228: left of '.name' must have class/struct/union
    type is ''unknown-type''
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(106) : error C3861: 'Class': identifier not found
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(107) : error C2228: left of '.number' must have class/struct/union
    type is ''unknown-type''
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(107) : error C3861: 'Class': identifier not found
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(108) : error C2228: left of '.meets' must have class/struct/union
    type is ''unknown-type''
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(108) : error C3861: 'Class': identifier not found
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(109) : error C2228: left of '.start' must have class/struct/union
    type is ''unknown-type''
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(109) : error C3861: 'Class': identifier not found
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(110) : error C2228: left of '.end' must have class/struct/union
    type is ''unknown-type''
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(110) : error C3861: 'Class': identifier not found
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(111) : error C2228: left of '.teacher' must have class/struct/union
    type is ''unknown-type''
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(111) : error C3861: 'Class': identifier not found
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(112) : error C2228: left of '.size' must have class/struct/union
    type is ''unknown-type''
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(112) : error C3861: 'Class': identifier not found

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Why don't you use a vector which has functions to find data given a predicate function. or even a foreach to display only if it satisfies a predicate function.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The array Class is local to InputClass and does not exist in DisplayClass. This also means that all the data will be lost as soon as InputClass exits.

    You should declare the CLASS list in main and pass it to both functions (together with any other data these might need to update/display the data, such as current size / max size).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Unless you typedef the struct you must refer to it with the struct keyword:

    Code:
    int InputClass(int cindex)
    {
            //Wrong
    	//CLASS Class[54];
    
            //Right
            struct CLASS Class[54];
    ...

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This is C++, here you don't need that typedef.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Quote Originally Posted by rags_to_riches View Post
    Unless you typedef the struct you must refer to it with the struct keyword:

    Code:
    int InputClass(int cindex)
    {
            //Wrong
    	//CLASS Class[54];
    
            //Right
            struct CLASS Class[54];
    ...

    Only in C. His program is C++, so that's acceptable.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    You can see each function as a separate building. What exists in one building doesn't necessarily exist in another building.
    So you either need to transport that something into the building (pass an argument, which is the recommended way) or put the something outside any buildings, in which case all buildings can see and use it (called global, not recommended).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Dammit, I guess I should stay in C programming only...this is the second C++ question I've boned from lack of use. Or maybe it's the crack.

    Oh well, very sorry for the confusion.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    Well, mistakes makes perfect. If you don't make mistakes, you don't learn! So don't be so down
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(34) : error C2664: 'InputClass' : cannot convert parameter 2 from 'CLASS [54]' to 'CLASS'
    This particular error means that you have something like:
    Code:
    int InputClass(int cindex, CLASS Class);
    ...
    
    CLASS array[54];
    inputClass(n, array);
    Which of course, the compiler is going to complain about.

    You should EITHER:
    1. Use class[] in the prototype.
    2. Pass in an indexed element from the array variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    I now only have errors in the second function, excluding line 73 error. I haven't changed any code in the second function. Code is now 1 line less from original post.

    Errors:

    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(73) : error C2143: syntax error : missing ';' before 'constant'
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(102) : error C2064: term does not evaluate to a function taking 1 arguments
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(102) : error C2228: left of '.number' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(105) : error C2064: term does not evaluate to a function taking 1 arguments
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(105) : error C2228: left of '.name' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(106) : error C2064: term does not evaluate to a function taking 1 arguments
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(106) : error C2228: left of '.number' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(107) : error C2064: term does not evaluate to a function taking 1 arguments
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(107) : error C2228: left of '.meets' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(108) : error C2064: term does not evaluate to a function taking 1 arguments
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(108) : error C2228: left of '.start' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(109) : error C2064: term does not evaluate to a function taking 1 arguments
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(109) : error C2228: left of '.end' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(110) : error C2064: term does not evaluate to a function taking 1 arguments
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(110) : error C2228: left of '.teacher' must have class/struct/union
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(111) : error C2064: term does not evaluate to a function taking 1 arguments
    c:\documents and settings\student\my documents\visual studio 2008\projects\mod 2 project 3\mod 2 project 3\class planner.cpp(111) : error C2228: left of '.size' must have class/struct/union
    Last edited by rockstarpirate; 05-02-2008 at 09:40 AM. Reason: line number changes

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    cindex++1;

    and what do you mean by this?


    DisplayClass - this function should somehow know about the Class - for example make it class member...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    I accedentally put a 1 there, its not needed. I don't understand what you mean, I'm just using the fuction DisplayClass to display a class schedule or a searched class, not code class, but school class.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    DisplayClass does not sees the Class variable declared in another function... it is upto you how to make it seing it - make function a class member, pass object as a parameter and (worst choice) make it global
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    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.

Page 1 of 2 12 LastLast
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-27-2008, 11:35 PM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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