Help - Addition

This is a discussion on Help - Addition within the C++ Programming forums, part of the General Programming Boards category; We have been instructed among many things to "Notice that the cost is totaled and the hours are totaled. These ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    Help - Addition

    We have been instructed among many things to "Notice that the cost is totaled and the hours are totaled. These values are not stored but computed for the sole purpose of being displayed to the screen. Since the number of hours and individual cost is stored, there is no reason to store the totals. (See totalhours)

    I have written this and all works but the total.
    #include<iostream.h>
    #include <stdlib.h>
    #include <stdio.h>

    class Student
    {
    public:
    char firstName[25];
    char lastName[25];
    char address[50];
    char city[25];
    char state[10];
    char zipcode[11];
    int studentCounter;
    int courseCounter;
    char SID [7];

    char courseNumber[7][25];
    char courseTitle[7][25];
    int courseHours[7];
    };

    Student createStudent(Student newStudent)
    {
    int StudentCounter = 0;
    int y = 0;
    cout << "Enter the student\'s first name: ";
    cin.getline(newStudent.firstName, 25, '\n');
    cout << "Enter the student\'s last name: ";
    cin.getline(newStudent.lastName, 25, '\n');
    cout<<"Enter street address: ";
    cin.getline(newStudent.address,50,'\n');
    cout<<"Enter city: ";
    cin.getline(newStudent.city,25,'\n');
    cout<<"Enter state: ";
    cin.getline(newStudent.state,10,'\n');
    cout<<"Enter zip code: ";
    cin.getline (newStudent.zipcode,11,'\n');
    y = 0;

    do
    {
    cout<<"Enter a course Number\t";
    cin.getline (newStudent.courseNumber[y],25,'\n');
    cout<<"Enter course Name\t";
    cin.getline (newStudent.courseTitle[y],25,'\n');
    cout<<"Enter the number of hours\t";
    cin>>newStudent.courseHours[y];
    newStudent.courseCounter++;
    cin.ignore();
    y++;
    }
    while (y<3);

    newStudent.studentCounter++;
    sprintf (newStudent.SID, "%c%c-%d",newStudent.firstName[0], newStudent.lastName[0], newStudent.studentCounter);

    return newStudent;
    }

    void displayStudent(Student newStudent)
    {
    int totalHours = 0;
    int y = 0;

    do
    {
    cout<<totalHours = totalHours + newStudent.courseHours[y];
    y++;
    }
    while (y<newStudent.courseCounter);
    endl;


    }

    void main()
    {
    Student student; //declare one student for class Student
    int menuChoice;

    cout<<"Menu of choices: "<<endl;
    cout<<"1) Enter a new student "<<endl;
    cin>>menuChoice;
    cin.ignore();


    switch (menuChoice)
    {
    case 1: student = createStudent(student);
    cout << endl << endl;
    cout<<"Menu of choices: "<<endl;
    cout<<"1) Enter a new student "<<endl;
    cout<<"2) Display student and list of classes "<<endl;
    cout<<"3) Print a student's bill "<<endl;
    cout<<"4) Exit"<<endl;
    cin>>menuChoice;
    cin.ignore();
    case 2: displayStudent(student);
    cout << endl << endl;
    }
    system ("Pause");
    }
    Last edited by Billye Scott; 03-04-2002 at 04:05 PM.

  2. #2
    Unregistered
    Guest
    and you want help with what?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    I changed only a few things, the program seems to work, except it only displays the total hours (you have yet to code the rest).
    I attatched the code in a .cpp document so you dont have to copy paste all this, just download.

    Here is the new code however:

    #include<iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <stdio.h>

    class Student
    {
    public:
    char firstName[25];
    char lastName[25];
    char address[50];
    char city[25];
    char state[10];
    char zipcode[11];
    int studentCounter;
    int courseCounter;
    char SID [7];

    char courseNumber[7][25];
    char courseTitle[7][25];
    int courseHours[7];
    };

    Student createStudent(Student newStudent)
    {
    int StudentCounter = 0;
    int y = 0;
    cout << "Enter the student's first name: ";
    cin.getline(newStudent.firstName, 25, '\n');
    cout << "Enter the student's last name: ";
    cin.getline(newStudent.lastName, 25, '\n');
    cout<<"Enter street address: ";
    cin.getline(newStudent.address,50,'\n');
    cout<<"Enter city: ";
    cin.getline(newStudent.city,25,'\n');
    cout<<"Enter state: ";
    cin.getline(newStudent.state,10,'\n');
    cout<<"Enter zip code: ";
    cin.getline (newStudent.zipcode,11,'\n');
    y = 0;

    do
    {
    cout<<"Enter a course Number\t";
    cin.getline (newStudent.courseNumber[y],25,'\n');
    cout<<"Enter course Name\t";
    cin.getline (newStudent.courseTitle[y],25,'\n');
    cout<<"Enter the number of hours\t";
    cin>>newStudent.courseHours[y];
    newStudent.courseCounter++;
    cin.ignore();
    y++;
    }
    while (y<3);

    newStudent.studentCounter++;
    sprintf (newStudent.SID, "%c%c-%d",newStudent.firstName[0], newStudent.lastName[0], newStudent.studentCounter);

    return newStudent;
    }

    void displayStudent(Student newStudent)
    {
    int totalHours = 0;
    int y = 0;

    do
    {
    totalHours += newStudent.courseHours[y];
    cout << totalHours << endl; //Vegetto comment: I added an endl to separate the numbers.
    y++;
    }
    while (y<newStudent.courseCounter);
    endl;
    }

    void main()
    {
    clrscr();
    Student student; //declare one student for class Student
    int menuChoice;

    cout<<"Menu of choices: "<<endl;
    cout<<"1) Enter a new student "<<endl;
    cin>>menuChoice; // Vegetto comment: You can erase these two lines and put menuChoice = getch();
    cin.ignore(); // for faster menu navigation, but I wont change your code.

    switch (menuChoice)
    {
    case 1: student = createStudent(student);
    cout << endl << endl;
    cout<<"Menu of choices: "<<endl;
    cout<<"1) Enter a new student "<<endl;
    cout<<"2) Display student and list of classes "<<endl;
    cout<<"3) Print a student's bill "<<endl;
    cout<<"4) Exit"<<endl;
    cin>>menuChoice;
    cin.ignore();
    case 2: displayStudent(student);
    cout << endl << endl;
    }
    getch(); //Vegetto comment: Better way of doing 'system ("Pause");'
    }
    Attached Files Attached Files
    C:\DOS\
    C:\DOS\RUN\
    \RUN\DOS\RUN\

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reliably checking for overflow before addition?
    By cyberfish in forum C++ Programming
    Replies: 18
    Last Post: 06-23-2008, 03:35 AM
  2. Replies: 7
    Last Post: 06-01-2008, 07:47 AM
  3. Repeated Addition
    By rocksteady in forum C Programming
    Replies: 15
    Last Post: 12-11-2007, 06:05 AM
  4. need some help with binary addition.
    By InvariantLoop in forum C++ Programming
    Replies: 21
    Last Post: 01-27-2005, 05:52 AM
  5. Problems with an addition operator
    By jamjar in forum C++ Programming
    Replies: 12
    Last Post: 03-28-2003, 12:47 PM

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