Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,911 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,177 people online right now. Registration is fast and FREE... Join Now!




Struct

 
Reply to this topicStart new topic

Struct, Won't save info or read out.

mistic857
3 Dec, 2006 - 06:19 AM
Post #1

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
Can't get this to print out to the screen. Get a bunch of numbers that make no sense.
I thought it might be a problem with the 2nd structure, but can't seem to figure it out.
any help?
I also got the windows error, so I think I have an over load somewhere. (right?)





CODE

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{//Beginning of main function

    //variable
    int num_courses;

    //structure

    struct schedule  
    {

    int sch_num[100];            
    char course_name[100];
    char course_sec[100];
    char course_meeting[100];
    char course_room[100];

    }school;

        
    //Number of courses
    cout << "Enter in number of courses you want to enter : ";
    cin >> num_courses;
    cout << endl << endl; //Loop to enter in schedule information
    for(int cnt = 0; cnt> school.sch_num[cnt]; cnt++);
    {

        cout << "Enter the course's name : ";
        cin.ignore(80, '\n');
        cin.getline(school.course_name, 100);
        cout << "Enter the course's section : ";
        cin >> school.course_sec[cnt];

        cout << "Enter the course's meeting time." << endl;
    
        cout << "(Ex. M W F 10:00A - 10:50) : ";
        cin.ignore(80, '\n');
        cin.getline(school.course_meeting, 100);

        cout << "Enter the course's classroom." << endl;
        cout << "Ex. 112 Swift) : ";
        cin.ignore(80, '\n');
        cin.getline(school.course_room, 100);
        cout << endl;


    }

    //clear screen
    system ("cls");

    //output

    //Loop
        for( cnt = 0; cnt> school.sch_num[cnt]; cnt++);
        {
        cout << school.sch_num[cnt] << school.course_name[cnt] << school.course_sec[cnt] << school.course_meeting[cnt] << school.course_room[cnt] << endl;
        }

  



return 0;
}//End of main functi

User is offlineProfile CardPM
+Quote Post

chris.tkd
RE: Struct
3 Dec, 2006 - 06:59 AM
Post #2

D.I.C Head
Group Icon

Joined: 26 Sep, 2006
Posts: 62


Dream Kudos: 25
My Contributions
its probably printing out the memory addresses, try creating your structure outside the main function. also remember that computers count from 0 so you may have to print to length-1.
User is offlineProfile CardPM
+Quote Post

mistic857
RE: Struct
3 Dec, 2006 - 08:04 AM
Post #3

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
QUOTE(chris.tkd @ 3 Dec, 2006 - 07:59 AM) *

its probably printing out the memory addresses, try creating your structure outside the main function. also remember that computers count from 0 so you may have to print to length-1.


We have to keep within the main,
Thanks....
I will try the length- to see what happens.
I thought maybe I had to declare the amount of items or something silly.

User is offlineProfile CardPM
+Quote Post

BitByte
RE: Struct
3 Dec, 2006 - 08:50 AM
Post #4

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
What is the varibale num_courses doing? You have asked for a value but you do not do anything with it. Are you tring to make an array of structures?
User is offlineProfile CardPM
+Quote Post

horace
RE: Struct
3 Dec, 2006 - 09:52 AM
Post #5

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(mistic857 @ 3 Dec, 2006 - 04:04 PM) *

QUOTE(chris.tkd @ 3 Dec, 2006 - 07:59 AM) *

its probably printing out the memory addresses, try creating your structure outside the main function. also remember that computers count from 0 so you may have to print to length-1.


We have to keep within the main,
Thanks....
I will try the length- to see what happens.
I thought maybe I had to declare the amount of items or something silly.

if the structure represents a School with up to 100 courses etc and as you are using C++ I would have thought the arrays would be arrays of string rather than arrays of char, e.g.
CODE

struct schedule
{
int sch_num[100];
string course_name[100];
string course_sec[100];
string course_meeting[100];
string course_room[100];
}school;

or does each structure represent a course, if so should it be
CODE

struct schedule
{

int sch_num;
string course_name;
string course_sec;
string course_meeting;
string course_room;
}course;

I think it would help if you gave us the spefification of what you are trying to do
User is offlineProfile CardPM
+Quote Post

mistic857
RE: Struct
3 Dec, 2006 - 02:19 PM
Post #6

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
Yes, it is suppose to be a school with many course's.
Sorry, I just didn't think about explaining what it was suppose to do.
Suppose to enter in course, course name, number, room...
The out put should show what ever you put in.
I have never worked with structure, this is my first attempt so I am really lost. I just went by the example that we had.
As far as char arrays? I just assumed that is what I would need......
Since that is how the example was.

By changing it to a string I get many errors....

.cpp(35) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

I figure I need the #include<string> so I did that, not all it does is shut down...

This post has been edited by mistic857: 3 Dec, 2006 - 02:28 PM
User is offlineProfile CardPM
+Quote Post

okyup
RE: Struct
3 Dec, 2006 - 03:53 PM
Post #7

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
CODE
struct {
    int sch_num;
    struct {
        string course_name;
        ...
    } course[100];
} schools[X];


If im understanding this correctly.

schools[5].courses[43].course_name="whatever";

This post has been edited by okyup: 3 Dec, 2006 - 03:54 PM
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: Struct
3 Dec, 2006 - 06:31 PM
Post #8

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
QUOTE
.cpp(35) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion


this is because cout does not know how to display the object you are giving it because the object you are giving it is not of a standard type. you will have to write your own function to display the object properly, or overload cout's << operator to handle your object correctly.
User is offlineProfile CardPM
+Quote Post

horace
RE: Struct
3 Dec, 2006 - 11:28 PM
Post #9

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
if your struct is
CODE

typedef struct
{
int sch_num[100];
string course_name[100];
string course_sec[100];
string course_meeting[100];
string course_room[100];
}school;

you can then define a variable of type school and read and write members so
CODE

school s;
cout << "Enter the course's name : ";
getline(cin, s.course_name[0]);
cout << " course name " << s.course_name[0] << endl;


I think you need need to specify exactly what the program is to do and from that design your data structure(s) and the functions that operate on them. Seeing as you are using C++ you could define a couple of classes Course (with data members name, room, etc) and School (with data members name, address, Course[], etc ) with their associated operator functions.
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Struct
4 Dec, 2006 - 06:50 AM
Post #10

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
Why don't you dynamically allocate the memory needed. What if you don't need to enter 100 students details? Memory is wasted. Here is an example:

CODE
#include <iostream>
#include <string>

struct details
{
    std::string name;
    int age;
};

int main( void )
{
    std::cout << "How many students to catalogue? : ";
    // Concatenate .get() to eat the newline.
    int amount;
    ( std::cin >> amount ).get();

    // Dynamically allocate the memory needed.
    // There is no memory wasted.
    details *students = new details[amount];

    for ( int i = 0; i < amount; i++ )
    {
        std::cout << "Enter students #" << ( i + 1 ) << "\'s name: ";
        std::getline( std::cin, students[i].name );

        std::cout << "Enter students age: ";
        ( std::cin >> students[i].age ).get();

        std::cout << std::endl;
    }

    // Delete the memory.
    delete [] students;

    // Use another loop to print them out.
    return 0;
}

User is offlineProfile CardPM
+Quote Post

mistic857
RE: Struct
4 Dec, 2006 - 02:50 PM
Post #11

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
Well this is my first year in C++ and we have not learned a lot of the things you talk about here.
The std::string name; is something i have not seen before.
I did however had the value set to high [100] and that did create a problem.

I ended up changing it all and did a loop for out put.

my errors were silly mistakes that I couldn't see... need some fresh eyes to look at it.
I had a ; after my for loop which made it go nuts...
Thanks for all your help!
You all gave me some a different way of handling the problem and I used a little bit of everything to get the job done.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 01:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month