Lab 9

This week in lab we are going to be working on 2 things. First, we are going to create a simple struct. Finally, we are going to make an array of our struct and store values in it.

Part 1

For this part, we are going to make a basic struct. This struct will store a string and a double. The struct will be called

basicStruct

and it will be declared in a header file called:

lab9.h

The double needs to be called number and the string needs to be called name.

Testing

This part's a little harder to test, but here's some code that you can use that will at least compile it and see if it works.

#include <iostream>
#include "lab9.h"
using std::cout;
using std::cin;
using std::endl;

int main()
{
    struct basic;
    basic.number = 10.5;
    basic.name = "this is my name";
    cout << "The number is: " << basic.number << endl;//this should say "The number is: 10.5"
    cout << "The name is: " << basic.name << endl; //this should say, "The name is: this is my name"

}

Submission of Part 1

All you need to do for this part, is turn in your lab9.h file. You don't need to zip it and you don't need the implementation. For me to test this, all I need is the declaration of the struct.

Part II

This is the last part and by now you are probably exhausted from all the typing. This will put last week's lab and part 1 together and make an array of structs (sounds like a project idea).

You are going to create an array of basicStruct of size 10. You will then make a function with the following signature:

You are going to write one function called:

void readData( istream& in, ostream& );

declare it in, you guessed it:

lab9.h

I would make it the last declaration in the file. So have the struct first, then this one.

This function will work more or less like the lab from last week. It will read data and store it in the array. The array this time is an array of the basicStruct type instead of ints, so our input will a string and a double. You are to read the string and the double. If there is space in the array, then store the string and the double in the array at the next available location in the array.

Sample Input

Dave 10.3 
Kelly 15.2 
Sue 45.5 
Michael 36.5 
Matt 23.1 
Molly 7.25

Sample Output

Index Name Number 
0 Dave 10.3 
1 Kelly 15.2 
2 Sue 45.5
3 Michael 36.5
4 Matt 23.1 
5 Molly 7.25 
6 ? ? 
7 ? ? 
8 ? ? 
9 ? ?

You should not initialize your array and then print out ?'s Just print out the values in the array.

I used ? above to represent the value that I don't know. Since those locations didn't get a value C++ doesn't guarantee any value. I will check that the good values are set.

Testing

It's always a good idea to test your code. I'll provide a real simple main you can use.

#include <iostream>
#include "lab9.h"
using std::cout;
using std::cin;
using std::endl;

int main()
{
    readData( cin, cout );
}

The output here will depend on what you type in on the keyboard. I would enter some valid data and some invalid data. Pressing control+Z will get the loop to stop.

Submission of Part II

For this submission, zip up your lab9.h file and the implementation file. You can put all the code for both parts 1 and III in the same cpp that is fine.