How to structure my arrays?

This is a discussion on How to structure my arrays? within the C++ Programming forums, part of the General Programming Boards category; Hello i have an assignment due soon where I need to make three seperate arrays that will be Student Number ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    How to structure my arrays?

    Hello i have an assignment due soon where I need to make three seperate arrays that will be Student Number as an integer, GPA as a double and Student last name as a string. Where the information input will be terminated if the student number is sentinel -999. I am to assume there are up to 100 students in file. Then cout the information into columns with column headings in order GPA, Name and number. Then finding at the end the total number of students and the average number of GPA scores.

    So far all i have been able to do is make a column of all the GPA scores with no heading, sense i cant find out how to make a heading for it and I cant make a string based array work at all im utterly confused.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Can you show what you've got so far?

    Normally I'd advise to do these things in steps. Based on the information in the assignment, it seems like you should be able to declare your arrays. Have you done that?

    After that, you should be able to read data into the arrays from cin. Have you done that? Does your check count how many entries it is reading in? Does it stop at -999?

    For the columns, it sounds like you need to use stuff from <iomanip>, have you studied that?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    Are you sure you have to use three separate arrays? It would be better to define your own data structure I think. Anyway, just declare the arrays like this: - remember to include the "string" library

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int studentID[100];
    double studentGPA[100];
    string studentName[100];
    Last edited by rocketman50; 04-05-2010 at 06:37 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    rocketman50, we generally try not to give solutions to homework assignments, especially not ones that are incorrect. I know yours is meant as a starting point, but the code accomplishes (or tries to accomplish) several of the specific pieces of the assignment that are being tested.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    
    int main ()
    {
    int counter=0;
    
    int const max_array=100;
    string name_array[max_array];
    double gpa_array[max_array];
    int id_array[max_array];
    
    cout<<"Please Enter the GPA for the number of students"<<endl;
    
    {
    for(int counter=0; counter<5; counter++) 
    cin>>gpa_array[counter];
    }         
    
            
    {
    cout<<endl;
    for(int counter=0; counter<5; counter++)
    cout<<gpa_array[counter]<<" "<<endl;
        
    return 0;
    }   
    }

    so far this is what I have done on my own after this im am unsure of how to begin my string array and how to manipulate it.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    Not sure if this is what you are after, but if you change
    Code:
    #include <cstring>
    to the C++ version
    Code:
    #include <string>
    You can add strings to the array in the same way as you are doing now with the gpa_array


    i.e. this will work
    Code:
    {
    for(int counter=0; counter<5; counter++) 
    cin>>name_array[counter];
    }
    Also, you might want to delete the first 'int counter=0;' above the array declarations as it is not needed

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Not sure if this is what you are after, but if you change
    Code:

    #include <cstring>

    to the C++ version
    Code:

    #include <string>
    Using letter 'c' before C header file names is C++ recommendation. In other words, using <string.h> is C style and <cstring> is C++ style. We use '.h' with header files in C but not in C++.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hash View Post
    Using letter 'c' before C header file names is C++ recommendation. In other words, using <string.h> is C style and <cstring> is C++ style. We use '.h' with header files in C but not in C++.
    <cstring> is not the same as <string>, altho it is the same as <string.h>. If you want to use C++ strings, you include <string>. If you want to include C string functions, you include <cstring> or <string.h>.

    And yes, we do use .h files in C++.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    I said the same thing. '.h' is added to user defined header files not the standard header files in C++. We can write #include "abc.h" for our own defined header files (that too within " " ), #include <cabc> (with 'c' added to abc) for C header files and #include <abc> for standard C++ header files. I think I made my point clear now.
    Last edited by hash; 04-07-2010 at 10:27 AM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> I think I made my point clear now.
    Yeah, but the point is confusing since the OP should be using <string>. What rocketman50 said was correct.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    What rocketman50 said was correct.
    I never meant to say that. Never mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structure arrays
    By mapunk in forum C Programming
    Replies: 8
    Last Post: 11-17-2005, 07:44 AM
  2. Structure Within Structure
    By Shakira in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 02:35 PM
  3. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 10:45 AM
  4. displaying data from a structure of arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 03-14-2002, 11:35 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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