Thread: Why isn't my program working?

  1. #1
    Unregistered
    Guest

    Question Why isn't my program working?


    The program is suppose to declare a structure of student record:
    • Student name – string type
    • Student grade1 – integer
    • Student grade2 – integer
    • Student grade3 – integer
    • Average score – float

    Use a loop to enter 10 student names and their 3 test scores
    Calculate the average of each student
    Sort the array into ascending order
    Print the student record with the highest average
    Print the student with the lowest average

    This is the source code that I have created:
    Code:
    #include <iostream>
    #define TAB '\t'
    #define MAX 13
    // Creating the structure/array
    struct studentrec {
    char name[15];
    int test1, test2, test3;
    float Ave;
    };
    main() {
    struct studentrec testrec[MAX];
    int i;	// loop control
    // Inputing data into the structure
    for (i = 0; i < MAX; i++) {
    cout << "Name" << endl;
    cin >> testrec[i].name;
    cin >> testrec[i].test1;
    cin >> testrec[i].test2;
    cin >> testrec[i].test3;
    // Calculating the average of each student
    testrec[i].Ave = (testrec[i].test1 + testrec[i].test2 + testrec[i].test3) / 3;
    // Setting the structure
    struct studentrec temp;
    for (int k = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++) {
    if (testrec[i].Ave > testrec[j+1].Ave) {
    temp = testrec [j];
    testrec[j] = testrec [j+1];
    testrec[j+1] = temp;
    } // end of if (testrec[i].Ave > testrec [j+1]) statement
    // Printing the structure
    for (i+0; i < MAX; i++) {
    cout << testrec[i].name
    << testrec[i].test1 << TAB
    << testrec[i].test2 << TAB
    << testrec[i].test3 << TAB
    << testrec[i].Ave;
    } // end of for j loop
    } // end of for i loop
    } // end of for i+0 loop
    } // end of for k loop
    return (0);
    } // end of main

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Next time it would be great if you could throw in the errors your getting.

    Is it where you declare the struct?

    U have:

    struct studentrec testrec[MAX];

    normally it would be:

    studentrec testrec[MAX];

    I don't think what you have would work...
    What error are you getting...is it in compile, runtime, or is the programming just not doing what you want?
    ______________________
    The Gekko

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    u dont specify a return type for main()...it should be int

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You also need to indent your code properly, to find out what the nesting of all your loops is.

    Because it seems to me that you are sorting the whole array each time you add a record.

    You want

    - the for loop for input

    - the two for loops to sort

    - the for loop to print

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM