Thread: Help - Structs, Functions, and Arrays

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Help - Structs, Functions, and Arrays

    I have a simple assignment that makes my head spin. (I guess I'm not quite there yet.)

    I must build a program that asks for 20 records. Each record holds a first and last name, and a test score. It then figures out the grade letter for the test score and then outputs all the data that was entered; also finding the highest score amongst the 20 test scores.

    To do this I must use a struct to hold the data and four functions to do all the work. So that main pretty much does nothing.

    (the book is no help. I hate it.)
    My first problem is that when I run a basic version of the program (I made a program just to read 1 record and read it back out to me.), it doesnt keep the first and last name data. It keeps testscore and grade data. But for some reason it's not keeping any name data I input and I can't figure out why

    Here's the code I've written:
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string>
    #include<fstream>
    #include<iomanip>
    
    using namespace std;
    
    struct studentType
    {
        string FName;
        string LName;
        int testScore;
        char grade;
    };
    
    int main()
    {
        void studentData();
        void getGrade();
        void highestScore();
        void printData();
        
        studentData();
        
        getGrade();
        
        highestScore();
        
        printData();
    
    getch();
    }
    
    void studentData()
    {
        studentType student;
        cout<<"Hey"<<endl;
        cin>>student.FName>>student.LName;
        cin>>student.testScore;
    }
    
    void getGrade()
    {
        studentType student;
        if (student.testScore >= 90)
            student.grade = 'A';
        else if (student.testScore >= 80)
            student.grade = 'B';
        else if (student.testScore >= 70)
            student.grade = 'C';
        else if (student.testScore >= 60)
            student.grade = 'D';
        else
            student.grade = 'F';
    }
    
    void highestScore()
    {
        cout<<"Hi!"<<endl;
    }
    
    void printData()
    {
    //Last name, First Name, Score, Grade
        studentType student;
        cout<<student.LName<<", "<<student.FName<<" : "<<
            student.testScore<<" : "<<student.grade;
    }
    If anyone could just help me out here. All I need is a little push in the right direction. I'm not asking for you to do the entire work for me. I just need to get over this bump in the road.

    -What's wrong with the program; Why wont my name data carry over.
    -How would I eventually incorporate an array into this to input and hold 20 records of student data?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    because your vars are all local to your functions...

    declare 1 (!) instance of studentType student; in main and pass it in all your functions as a parameter by reference (const ref if function does not modify it)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Also consider moving your function prototypes outside of main().

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Todd Burch View Post
    Also consider moving your function prototypes outside of main().

    Todd
    If we are so serios - also should be added request to use only standard headers:
    #include<iostream.h> - outdated, should be replaced with <iostream>
    #include<conio.h> - non-standard at all (probably - you need to look for a compiler that is C++ standard complient
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    Quote Originally Posted by vart View Post
    If we are so serios - also should be added request to use only standard headers:
    #include<iostream.h> - outdated, should be replaced with <iostream>
    #include<conio.h> - non-standard at all (probably - you need to look for a compiler that is C++ standard complient
    Its the crap they tell us to use in the book. I just throw it in there not even knowing if I'm going to need it or not. I don't understand it well enough.

  6. #6
    coder
    Join Date
    Feb 2008
    Posts
    127
    If you take a look at the <iostream> header file (its location depends by your OS), you will see that it includes many other headers itself:
    to make your program working you probably need only: #include <iostream>, not the others.

    Anyway a pratic way to understand which header (library) you need is:
    Code:
    begin:
        #include one header;
        try to compile the program;
        if the compiler says that some function or type are not declared:
            goto begin
    done

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's not really a good way to do it, because many older compilers have <iostream.h> and other things anyway, and don't complain when you use them. And many compilers, for example, include other headers from within something like <stdio.h>, so you can use exit() without any warnings, which ought to require <stdlib.h>.

    The best way to do it is to google for each function to see what header file it's in. Of course, that's quite time-consuming and difficult for C++ objects sometimes. Perhaps the best way is to post it on CBoard and see what happens.

    Note: conio.h is only required for getch() and other non-standard stuff. In C++, if you're using a .h header file, perhaps you shouldn't be. At least until you start using external libraries.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    coder
    Join Date
    Feb 2008
    Posts
    127
    In sites like this http://www.cplusplus.com/
    you can find everything you need to know about headers, functions types, etc...
    By the way, if you are using an old compiler, perhaps it is time to upgrade it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM