Thread: dynamic arrays

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    dynamic arrays

    I'm trying to create a program that takes user input for a number of students then asks the user for students names and a test score. Names and test scores should be assigned to two arrays, we are not allowed to include <string>. The program then sorts the test scores, calculates the average, and displays all the data. My program compiles and runs but there is an error when trying to assign names for the students.
    I think the problem has something to do with the getString() function???

    Can someone please help me spot my error?


    Code:
    // Project 8: Test Scores
    // October 31, 2008
    
    
    // This program stores students and their respective test
    // score in two seperate arrays, then calculates the
    // average test score, and displays the students, their 
    // test score, and the average test score for the class. 
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //function Prototypes
    int getSize();
    char* getString(int i);
    int stringLength(char string[]);
    void stringCopy(char string[], char* ptr, int size);
    void selSort(char** stu, int tes[],int size);
    float avg(int tes[], int size);
    
    
    int main()
    {
        int i, x, size = getSize();
        char* stu[size];
        int tes[size];
        float average; 
        
        for(i = 0; i < size; i++)
        {
              cout << "Please enter student " << (i + 1) << "'s last name: ";
              stu[i] = getString(i);
              cout << "Enter that student's test score: ";
              cin  >> tes[i];
              cout << endl;
        }
        
        selSort(stu, tes, size);
        
        average = avg(tes, size);
       
        cout << setw(12) << left << "Name"
             << setw(12) << left << "Score"
             << endl
             <<"-------------------------\n";  
        for(i = 0; i < size; i++) 
        {
              cout << setw(12) << left << stu[i]
                   << setw(12) << left << tes[i]
                   << endl;
        }
        cout << endl<< "The average test score is: " << average << endl;
        
     cin >> x; 
        
        
    }
    
    int getSize()
    {
        int size ;
        cout << "How many test scores will you enter?: ";
        cin  >> size;
        return size;
    }
    //----------------------------------------------------------------------------------------------------------
    
    char* getString(int i)
    {
        int size = 80;
        char string[size];
        cin.getline(string, size);
        cout << endl;
        size = stringLength(string);
        char* ptr = new char[size + 1];
        stringCopy(string, ptr, size);
        return ptr;
    }
    
    int stringLength(char string[])
    {
        int count = 0;
        while(string[count] != '\0')
            count++;
        return count;
    }
    
    void stringCopy(char string[], char* ptr, int size)
    {
        for(int i = 0; i <= size; i++)
            ptr[i] = string[i];
    }
    //---------------------------------------------------------------------------------------------------------
    void selSort(char* stu[], int tes[],int size)
    {
            int temp, min;
            char* stuTemp;
            
            for( int i = 0; i < size; i++)
            {
                    min = i;
                    for (int j = 1; j < size; j++)
                    {
                            if (tes[j] < tes[min])
                            min = j;
                    }
                    stuTemp = stu[i];
                    stu[i] = stu[min];
                    stu[min] = stuTemp;
                    
                    temp = tes[i];
                    tes[i] = tes[min];
                    tes[min] = temp;
            }
    }
    
    float avg(int tes[], int size)
    {
          int i;
          float sum = 0, avg; 
          
          for(i = 0; i < size; i++)
          {
                sum += tes[i];
          }            
          avg = (static_cast<float>(sum)/size);
      return avg;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So cin >> size, or cin >> tes[i], will leave enter-key in the input buffer, which means the next input operation (such as cin.getline) will start by seeing that enter-key. If you don't want this to happen, you can use cin.ignore. (Oh, and you are allowed to use variable names of longer than three characters.)

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Dunno if that is only the problem but stringCopy should add the '\0' character at the end of the string copied.

    EDIT: The same goes for cin.getline(). You have to add cin.ignore()
    Last edited by C_ntua; 11-03-2008 at 10:15 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    Dunno if that is only the problem but stringCopy should add the '\0' character at the end of the string copied.

    EDIT: The same goes for cin.getline(). You have to add cin.ignore()
    Nope, it should add one since stringCopy iteratores to <= size, so it copies the char at index size, too, which will be the '\0'.

    Unfortunately, this code has two major flaws:
    1) It does not delete what memory it allocates.
    2) It uses variable-sized arrays which is illegal in C++.
    You need to allocate appropriate space with new and then delete it. You cannot create static arrays from a non-constant expression.
    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.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Better yet, use a standard container. And standard strings instead of manual string copying.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    we are not allowed to include <string>
    So that isn't possible CornedBee

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Meh.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM