473,815 Members | 3,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structs in C++ using prototypes from the beginning

47 New Member
First week of the current trimester and we've taken a huge leap from writing little code snippets of loops and switches to writing an entire program, and expecting it to work!
I, nor anyone else in my class have heard of structs which is our first assignment. I've been pounding away for several days now and it won't compile beyond my struct declaration. Anyone who knows a bit about C++ I would enjoy you pointing out all of the syntax and logical errors in the first dozen lines of what is over 100 lines of code so far on this program. Here is the guideline:

CIS 247: Programming Assignment #1

Worth 5 Points Assigned: Week 1 Due: Week 3

You have contracted with a media company that deals with CDs, books, movies and video games and will be helping them manage inventory for one of the types of media.

Create a program that will internally store at least eight media items. Use a global (declared above main() ) structure called book, cd, movie or videoGame. Include at least five member variables (select the appropriate data types) to represent different attributes of a media (some examples: pages, cost, runningTime, format, hardcover, genre, …, etc.).
● One of them has to be price, because I will have you calculate tax (rate 8.5%) and total cost.
● One has to be a char array (C-String) and
● One has to be a char that is an abbreviation (such as r for Red). You select anything for the other two.

In main(), create 6 global month structure variables to represent the six different media items. For example, a structure for a shirt inventory might be:

struct shirt {
char size; // S, M, L, X;
char brand[15]; // uses C-style strings instead of string objects
bool shortSleeve;
char color; // for the required switch statement (r=Red, u=Blue, g=Green, w=White, b=Black, y=Yellow)
double price;
};

Note: The above code is an example, not to be used verbatim. Please choose your own set of variable names and data. You may use similar names. The structure can have the variables in any order.

You can load the data into the structures at the time of declaration, or read from a file.
struct shirt concert={'M', “Hanes”, true, ‘b’, 12.5}; // this would be shirt number 1
struct shirt work={'L', “Sears”, false, ‘u’, 21.95}; // this would be shirt number 2

… // shirt number 7
… // shirt number 8

Note: Select only one media type, come up with your own naming for the variables and use your own (unique) data.

Write a program that will generate a report showing all of the six media items (I'm using shirts instead here). Requirements:

● Create a text menu using a switch() block, to ask for media number, or 0 for all of them, or 99 to exit. All other input will result in an error message.
● Write one function that accepts media number as an int (1-8) and prints the appropriate media package information (see Sample Output), by passing the appropriate media to the function as a parameter. It will use the switch() statement to display the appropriate color (or whatever you want to use as a coded item),

I will be using the following key for color of shirt: (r=Red, u=Blue, g=Green, w=White, b=Black, y=Yellow)

Sample Output - Show output for at least 4 of the 6 items in your inventory
Shirt Menu

Please Enter 1-8 to list the shirts
Please Enter 0 to list all shirts
Please Enter 99 to end the program
Selection: 2

You have chosen to display information on the Sears shirt:
Size: L
Long Sleeve
Color: Blue
Price: $21.95
Tax: $ 1.87
Total Cost: $23.82

Note that the output will depend upon the type of media (not shirt) and variables that you select.

Now, I've given a great deal of effort into the code you are going to be looking at so keep in mind 6 months ago I had never stroked one key of code.
I'm not sure I'm posting this in the right area either, if not someone please direct me to the C++ posting threads please.



//Assignment One CIS247 Joseph Matzke
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

struct movie
{
string rating; //G,PG,PG13,R17,X ,XXX
char title; //Oceans 13, Oceans 12, Oceans 11, Braveheart, Harry Potter, Caligula
bool releaseDate; //Still in theatre-released
char language;//English, Spanish, Japanese, Chinese, German, Swaheely
double cost [2];
double tax;
};
void playMovie (struct movie); //it will not compile beyond here
int main(void)
{
int choice = 0;
//movie anyMovie; //declaration-creates object?
struct movie1={"R17", "Oceans 13", true, 'E', 19.99}; //movie number 1
movie movie2={"R17", "Oceans 12", false, 'E', 9.99};
movie movie3={"R17", "Oceans 11", false, 'E', 9.99);
movie movie4={"R17", "Braveheart ", false, 'E', 9.99};
movie movie5={"PG", "Harry Potter", false, 'E', 9.99};
movie movie6={"XXX", "Caligula", false, 'S', 1.99};

system ("pause");
return 0;
}

do
{
cout << "Please enter 0 to list all movies" << endl; //makes sense to display the menu for choice
cout << "Pleae enter 99 to end the program" << endl; //either display the menu or stop the program
cout << "Selection: ";

There is a lot more code to this, my switch module, my array, and so on but they do me no good if I can't compile beyond where I've noted.
I've changed from --void main (void) to--int main()--to int main (void)-- and regardless of what I do, actually my original code did not have a return in the main but the compiler message seemed to want me to return an int. Can anyone spot the problem? Thanks
Jul 21 '07
38 3046
joeschnell
47 New Member
So when I declared the elements in my struct, all of the elements are basically the same thing as a variable, why given they've been declared than do they not act in the code as if they are declared variables?
I know how the prototype works, but, now I'm not using a prototype like I have in the past such as movie(&char, &int, &double, &string), now I'm using the prototype from the struct such as
movie (choice = movie1) or how does the value pass in the struct?
This is the whole problem trying to figure out where the value is, how it is passed with the proper syntax.
Thanks Joe
Jul 26 '07 #31
ilikepython
844 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1.  /*{
  2.       int choice = 0;
  3. cout << "Choose a movie by entering 1 thru 6" << endl;
  4. cout << "Selection :";
  5. cin >> choice;
  6. }*/
  7. //playMovie [movies[choice - 1]);
  8. do
  9.  
  10. {
  11.  
  12.     cout << "Choose a movie by entering 1 thru 6" << endl;
  13.  
  14.     cout << "Selection :";
  15.  
  16.     cin >> choice;
  17. }
  18.     while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);
  19.     {
  20.     system ("pause");
  21.     return 0;
  22.  
  23.     }
  24.  // playMovie (movies [choice - 1];
  25.  
  26. /*
  27. while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);
  28. movie myMovie;
  29. switch (choice)
  30. {
  31.     case 1:
  32.          myMovie = movie1;
  33.          break;
  34.     case 2:
  35.          myMovie = movie2;
  36.          break;
  37.     case 3:
  38.          myMovie = movie3;
  39.          break;
I've tried this every way I can imagine, many more than shown here and regardless something is not right and I don't know what it is my friend. I have to get to class, I'll try some others tonight.
Expand|Select|Wrap|Line Numbers
  1.  /*{
  2.       int choice = 0;
  3. cout << "Choose a movie by entering 1 thru 6" << endl;
  4. cout << "Selection :";
  5. cin >> choice;
  6. }*/
  7. //playMovie [movies[choice - 1]);
  8. do
  9.  
  10. {
  11.  
  12.     cout << "Choose a movie by entering 1 thru 6" << endl;
  13.  
  14.     cout << "Selection :";
  15.  
  16.     cin >> choice;
  17. }
  18.     while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);
  19.     {                               // what's this?
  20.     system ("pause");      //
  21.     return 0;                    //
  22.  
  23.     }                           //
  24.  // playMovie (movies [choice - 1]);     // that should work
  25.  
  26. /*
  27. while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);
  28. movie myMovie;
  29. switch (choice)
  30. {
  31.     case 1:
  32.          myMovie = movie1;
  33.          break;
  34.     case 2:
  35.          myMovie = movie2;
  36.          break;
  37.     case 3:
  38.          myMovie = movie3;
  39.          break;
You almost have it. I don't know why you had that code with the system call and the return statement. This should work:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int choice = 0;
  4.     //movie anyMovie;  //declaration-creates object?
  5.     movie movie1={'R', "Oceans 13", true, 'E', 19.99, .08}; //movie number 1
  6.     movie movie2={'R', "Oceans 12", false, 'E', 9.99};
  7.     movie movie3={'R', "Oceans 11", false, 'E', 9.99};
  8.     movie movie4={'R', "Braveheart", false, 'E', 9.99};
  9.     movie movie5={'G', "Harry Potter", false, 'E', 9.99};
  10.     movie movie6={'X', "Caligula", false, 'S', 1.99};\
  11.  
  12.     movie movies[6] = {movie1, movie2, movie3, movie4, movie5, movie6};
  13.  
  14.  
  15.    do  
  16.    {
  17.        cout << "Please enter 0 to list all movies" << endl; //makes sense to display the menu for choice
  18.        cout << "Pleae enter 99 to end the program" << endl; //either display the menu or stop the program
  19.        cout << "Selection:  ";
  20.        cin >> choice;
  21.        cout << endl; cout << endl; cout << endl;
  22.    }
  23.    while (choice != 0 && choice !=99);   //repeat if choice not 0 or 99
  24.  
  25.     if (choice == 99)
  26.     {
  27.         system ("pause");
  28.         return 0;
  29.     }  
  30.  
  31.     if (choice == 0)     
  32.     {     
  33.         cout  << "Movie 1:\t" << movies[0].movie << endl;     // avoid hard coding literals
  34.         cout  << "Movie 2:\t" << movies[1].movie << endl; 
  35.         cout  << "Movie 3:\t" << movies[2].movie << endl;
  36.         cout  << "Movie 4:\t" << movies[3].movie << endl; 
  37.         cout  << "Movie 5:\t" << movies[4].movie << endl;
  38.         cout  << "Movie 6:\t" << movies[5].movie << endl << endl;
  39.     }
  40.  
  41.     do
  42.     {
  43.         cout << "Choose a movie by entering 1 thru 6" << endl;
  44.         cout << "Selection :";
  45.         cin >> choice;
  46.     } while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);  // make sure choice is 1 through 6
  47.  
  48.     playMovie(movies[choice-1]); // display correct movie; note: indexing starts at 0
  49.  
  50.     system ("pause");
  51.     return 0;      
  52. }
  53.  
If you don't understand any of it just ask and I will help you. Don't use something if you don't understand it.
Jul 26 '07 #32
ilikepython
844 Recognized Expert Contributor
So when I declared the elements in my struct, all of the elements are basically the same thing as a variable, why given they've been declared than do they not act in the code as if they are declared variables?
I know how the prototype works, but, now I'm not using a prototype like I have in the past such as movie(&char, &int, &double, &string), now I'm using the prototype from the struct such as
movie (choice = movie1) or how does the value pass in the struct?
This is the whole problem trying to figure out where the value is, how it is passed with the proper syntax.
Thanks Joe
I don't really understand what you are talking about. Member of a struct can only be accesed by objects of that struct using the '.' operator. What do you mean by prototype? If you want to write a function that takes a movie struct object as a parameter, write this:
Expand|Select|Wrap|Line Numbers
  1. void funcName(movie testMovie);
  2.  1       2      3        4
  3. 1 = return type of function
  4. 2 = name of function
  5. 3 = type of parameter, in your case movie
  6. 4 = name of parameter (optional in prototype)
  7.  
What don't you understand?
Jul 26 '07 #33
joeschnell
47 New Member
OK, few questions
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <string>
  5. using namespace std;
  6. struct movie
  7. {
  8.        char rating; //G,PG,PG13,R17,X
  9.        string movie; //Oceans 13, Oceans 12, Oceans 11, Braveheart, Harry Potter, Caligula
  10.        bool releaseDate;//released-inTheatre
  11.        char language;//English E, Spanish S, Japanese J, Chinese C, German G, Swaheely S
  12.        double cost; 
  13.        double tax;
  14. };
  15. void playMovie (movie);
  16.  
  17.       int main()
  18.  
  19.       {
  20.  
  21.           int choice = 0;
  22.  
  23.          // movie anyMovie;  //declaration-creates object?
  24.  
  25.           movie movie1={'R', "Oceans 13", true, 'E', 19.99, .08}; //movie number 1
  26.  
  27.           movie movie2={'R', "Oceans 12", false, 'E', 9.99};
  28.  
  29.           movie movie3={'R', "Oceans 11", false, 'E', 9.99};
  30.  
  31.           movie movie4={'R', "Braveheart", false, 'E', 9.99};
  32.  
  33.           movie movie5={'G', "Harry Potter", false, 'E', 9.99};
  34.  
  35.           movie movie6={'X', "Caligula", false, 'S', 1.99};\
  36.  
  37.  
  38.  
  39.           movie movies[6] = {movie1, movie2, movie3, movie4, movie5, movie6};
  40.  
  41.  
  42.  
  43.  
  44.  
  45.          do 
  46.  
  47.          {
  48.  
  49.              cout << "Please enter 0 to list all movies" << endl; //makes sense to display the menu for choice
  50.  
  51.              cout << "Pleae enter 99 to end the program" << endl; //either display the menu or stop the program
  52.  
  53.              cout << "Selection:  ";
  54.  
  55.              cin >> choice;
  56.  
  57.              cout << endl; cout << endl; cout << endl;
  58.  
  59.          }
  60.  
  61.          while (choice != 0 && choice !=99);   //repeat if choice not 0 or 99
  62.  
  63.  
  64.  
  65.           if (choice == 99)
  66.  
  67.           {
  68.  
  69.               system ("pause");
  70.  
  71.               return 0;
  72.  
  73.           } 
  74.  
  75.  
  76.  
  77.           if (choice == 0)     
  78.  
  79.           {     
  80.  
  81.               cout  << "Movie 1:\t" << movies[0].movie << endl;     // avoid hard coding literals
  82.  
  83.               cout  << "Movie 2:\t" << movies[1].movie << endl;
  84.  
  85.               cout  << "Movie 3:\t" << movies[2].movie << endl;
  86.  
  87.               cout  << "Movie 4:\t" << movies[3].movie << endl;
  88.  
  89.               cout  << "Movie 5:\t" << movies[4].movie << endl;
  90.  
  91.               cout  << "Movie 6:\t" << movies[5].movie << endl << endl;
  92.  
  93.           }
  94.  
  95.  
  96.  
  97.           do
  98.  
  99.           {
  100.  
  101.               cout << "Choose a movie by entering 1 thru 6" << endl;
  102.  
  103.               cout << "Selection :";
  104.  
  105.               cin >> choice;
  106.  
  107.           } 
  108.           while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);  // make sure choice is 1 through 6
  109.  
  110.  
  111.  
  112.           playMovie(movies[choice-1]); // display correct movie; note: indexing starts at 0
  113.  
  114.  
  115.  
  116.           system ("pause");
  117.  
  118.           return 0;     
  119.  
  120.       }
  121.  
This compiles, but does not link. Produces the error message
Linker error. undefined reference to 'playMovie(movi e)' and I have not clue one what this means.
I need only to add some very basic modules now adding final output with cost, tax, title, in correct format.
As for a few coding questions it will be easier if I post them with code snippets one at a time. I thank you for your indulgence. Joe
Jul 27 '07 #34
joeschnell
47 New Member
I don't access any members of struct per say using the '.' operator in the above code, which I need to do. The entire thing is this supposedly is an EASIER way to begin to understand OOP, which I'm not too sure about. Using a struct for your first program that is kind of object oriented as movie has sub sets of itself which makes it an object, correct? movie has, title, rating, et cetera so it has attributes making it an object, even though it is a struct! See this may not be confusing to you, but it is very confusing to me. I could get this desired output in 20 minutes if I could use primitive variables and simply write the program.

In the code:

Expand|Select|Wrap|Line Numbers
  1.  while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);  // make sure choice is 1 through 6
  2.  
  3.  
  4.  
  5.           playMovie(movies[choice-1]);
Wouldn't this, or shouldn't this be less the !?

like while(choice ==1 || choice ==2||choice==3 ........)
playMovie(movie s[choice-1]; so that whatever the user enters it is minus one thereby displaying the correct array value?? Why would it be while choice is not equal to 1? I don't understand that one. If it's && the user is going to have to enter 1,2,3,4,5,6 to get anything, correct? If it would compile I would know, but I think this is right. You have to remember last term we wrote ONE if loop, ONE while loop, ONE for loop, ONE switch statement, ONE prototype program, ONE getline program, so it's not like I've written a hundred while loops using these operands. Just makes sense to me this would not work.

Here's another:
Expand|Select|Wrap|Line Numbers
  1.  if (choice == 0)     
  2.  
  3.           {     
  4.  
  5.               cout  << "Movie 1:\t" << movies[0].movie << endl;     // avoid hard coding literals
  6.  
What does "avoid hard coding literals mean? I understand the concept of hard coding as in -- int i = 10; ,, it's the literal I don't understand. The other

Expand|Select|Wrap|Line Numbers
  1.  cout  << "Movie 6:\t" << movies[5].movie << endl << endl;
The >movies[5].movie<< I understand this, the "Movie 6:/t << I do not.

/t must be a short cut like /n for endl; is that I have not seen before.

Other than that I do understand every line, what it is doing, why, and just need to figure out the best place to put the calc, and display modules and hopefully it will compile. Thanks for your patience and help Python, I am struggling with this but my professor continues to tell me one day the light simply turns on and poof, you're a programmer. Ha, we'll see.

OK, I've got to start thinking about this, and try and google that compiler message which I doubt I'll find anything on as it is referring to playMovie. I've never encountered a program that compiles but does not link.
Thanks Joe
Jul 27 '07 #35
ilikepython
844 Recognized Expert Contributor
OK, few questions
<Sorry, I had to snip your code again>

This compiles, but does not link. Produces the error message
Linker error. undefined reference to 'playMovie(movi e)' and I have not clue one what this means.
I need only to add some very basic modules now adding final output with cost, tax, title, in correct format.
As for a few coding questions it will be easier if I post them with code snippets one at a time. I thank you for your indulgence. Joe
That linker error usually means that you have a prototype of a function that you never defined. So you have your playMovie prototype but you never define it. All you have to do is include the actual body of the playMovie() function.
Jul 27 '07 #36
ilikepython
844 Recognized Expert Contributor
I don't access any members of struct per say using the '.' operator in the above code, which I need to do. The entire thing is this supposedly is an EASIER way to begin to understand OOP, which I'm not too sure about. Using a struct for your first program that is kind of object oriented as movie has sub sets of itself which makes it an object, correct? movie has, title, rating, et cetera so it has attributes making it an object, even though it is a struct! See this may not be confusing to you, but it is very confusing to me. I could get this desired output in 20 minutes if I could use primitive variables and simply write the program.
You are not accesing any members because you don't need to. All you need to do, is display the variables, which you will do in playMovie (you pass the desired movie to the function). You could probably get the output easier but it would not be a very flexible program. RIght now, you have six movies, it's also very easy to add more movies or change one of the movies's attributes.
In the code:

Expand|Select|Wrap|Line Numbers
  1.  while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);  // make sure choice is 1 through 6
  2.  
  3.  
  4.  
  5.           playMovie(movies[choice-1]);
Wouldn't this, or shouldn't this be less the !?

like while(choice ==1 || choice ==2||choice==3 ........)
playMovie(movie s[choice-1]; so that whatever the user enters it is minus one thereby displaying the correct array value?? Why would it be while choice is not equal to 1? I don't understand that one. If it's && the user is going to have to enter 1,2,3,4,5,6 to get anything, correct? If it would compile I would know, but I think this is right. You have to remember last term we wrote ONE if loop, ONE while loop, ONE for loop, ONE switch statement, ONE prototype program, ONE getline program, so it's not like I've written a hundred while loops using these operands. Just makes sense to me this would not work.
The type of condition in the while loop can be confusing lots of times. What it says is, loop throught the loop again if choice is not 1, and choice is not 2, and choice is not 3, etc... So if choice is either 1, 2, 3, 4, 5, or 6 it will go past the loop. You want to loop again if choice is not 1 through 6. Does that make sense? Tell me if it doesn't.

To play the correct movie, you simply call the function with variable at array offset choice - 1. The array is in order. So if user picks, 4 for example, you need to call movies[3] (the 4th movie). Remember, arrays start counting at zero.
Here's another:
Expand|Select|Wrap|Line Numbers
  1.  if (choice == 0)     
  2.  
  3.           {     
  4.  
  5.               cout  << "Movie 1:\t" << movies[0].movie << endl;     // avoid hard coding literals
  6.  
What does "avoid hard coding literals mean? I understand the concept of hard coding as in -- int i = 10; ,, it's the literal I don't understand. The other

Expand|Select|Wrap|Line Numbers
  1.  cout  << "Movie 6:\t" << movies[5].movie << endl << endl;
The >movies[5].movie<< I understand this, the "Movie 6:/t << I do not.

/t must be a short cut like /n for endl; is that I have not seen before.
By not hard coding, literals, I mean don't say "cout << "Ocean's 12" << endl;" If you want to print the name of the movie Ocean's 12. Let's say you want to change the name of that movie. You have to change it in two places, possibly more (declaration and literals). You can avoid that by just using the struct variable. "movies[0].movie. The "\t" just means tab. It's like "\n" except "\n" is a newline and "\t" is a tab.
Other than that I do understand every line, what it is doing, why, and just need to figure out the best place to put the calc, and display modules and hopefully it will compile. Thanks for your patience and help Python, I am struggling with this but my professor continues to tell me one day the light simply turns on and poof, you're a programmer. Ha, we'll see.
The professor might be right :) Just don't give up, and ask questions if you need to.
Jul 27 '07 #37
joeschnell
47 New Member
Python you've helped me through my first two weeks and I can't thank you enough. I now, have an understanding of structs and how they work, hard coding and why it is not good to do, much easier to change one attribute than it is to comb through code looking for every instance of your hard coded crap. I understand the while loop much deeper than I did, I picked up several new short cuts such as /n and /t which are handy.
Biggest thing is I've been up since 11am yesterday, left class at 6 and stayed up all night writing this program. I dumped the whole damn bowl of spaghetti and started over. I made an IPO chart, I made a visio flowchart, I used real movies with their directors, time et cetera and started coding.
I wrote each module by itself first, compiled along the way, fixed along the way, and viola a program, and it works.
I'm the happiest C++ guy in North America, or was until I looked in our doc share bin and found this weeks assignment sitting there. Yikes, I can't keep up...

Well, looks like I get to learn how to write a program that pulls from a file, and writes to the file, and updates and sorts the file for stock exchange symbols and their prices. Insanity to think I can write that! But I'm gonna try-

First, look at this
Expand|Select|Wrap|Line Numbers
  1. // Assignment 1 CIS 247
  2. // Joseph L Matzke
  3.  
  4.  
  5. #include <iostream>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. struct Movie {
  11.     char  title[256];//Max her out
  12.     char  director[30];//Give me enough room
  13.     int  minutes; //Just to come up with some other element
  14.     char genre; // Technical, Fantasy, History, Classics gives me another attribute
  15.     double price; //Seems technical books out cost classics by far, it's insane
  16.  
  17. };
  18.  
  19. void showItem(Movie );
  20.  
  21. int main()
  22. {
  23.  
  24.     Movie movie[8] =    //might as well be eight or eighty just more typing once you get it
  25.     {
  26.         {"Harry Potter and the Deathly Hallows", "J. K. Rowling ",159, 'F',17.99},
  27.         {"Armed America: Documentary of Gun Owners in Their Homes", "Kyle Cassidy",94,'H',19.80},
  28.         {"The Old Man and The Sea","Ernest Hemingway",112,'C', 9.80},
  29.         {"Adventures of Huckleberry Finn (Mark Twain Library)","Mark Twain",129,'C',10.17},
  30.         {"A Christmas Carol","Charles Dickens",145,'C',4.49},//lists them in order like a prototype
  31.         {"C++ Programming (THIRD EDITION)", "D S MALIK",71,'T',127.30},
  32.         {"Legacy of Ashes: The History of the CIA", "Tim Weiner", 120, 'H', 15.12},
  33.         {"Introduction to Programming with C++", "Diane Zuk", 50, 'T', 123.79}//lists object elements,attributes
  34.  
  35.     };
  36.  
  37.  
  38.  
  39.  
  40.     bool exit = false;//assign false to exit, gonna change that down the line
  41.     do {
  42.         cout << "Type item no. to display info; 0 to display all the items, 99 to exit" << endl << endl;
  43.         int choice;
  44.         cin >> choice;
  45.  
  46.         switch (choice) {
  47.             case 1:
  48.             case 2:
  49.             case 3:
  50.             case 4:
  51.             case 5:
  52.             case 6:
  53.             case 7:
  54.             case 8:
  55.  
  56.                 showItem(movie[choice-1]);//using my array to display, it starts at 0 so I less one -)
  57.                 break;
  58.             case 0:
  59.                 cout << "Here are all the movies available :" << endl;
  60.                 for (int i=0; i < 8; i++) //here's my array loop
  61.                                 {
  62.                     showItem(movie[i]);
  63.                 }
  64.                 break;
  65.  
  66.             case 99: 
  67.                 exit = true;        
  68.                 break;
  69.             default:
  70.                 cout << "Incorrect value entered; try again" << endl;
  71.  
  72.         }
  73.     } while (!exit);//while we are not exiting
  74.  
  75.  
  76.     return 0;
  77. }
  78.  
  79. void showItem(Movie b) {
  80.  
  81.  
  82.  
  83.     cout << endl << "*************************************" << endl;//stole this idea from Zeschke
  84.     cout << "Information on " << "\""<<b.title<<"\":" << endl;
  85.     cout << "Director: " << b.director << endl;
  86.     cout << "Title: " << b.title << endl;
  87.     cout << "Number of minutes: " << b.minutes << endl;
  88.  
  89.     cout << "Genre: ";//calling my struct attributes with the '.' operator for my switch statement
  90.     switch (b.genre) {
  91.         case 'T' : cout << "Technical";
  92.                 break;
  93.         case 'F' : cout << "Fantasy";
  94.                 break;
  95.         case 'H' : cout << "History";
  96.                 break;
  97.         case 'C' : cout << "Classics";
  98.                 break;
  99.         default : cout << "!!Wrong Genre";
  100.  
  101.     }
  102.  
  103.     cout << endl;                                 //usually you don't need std:: except when using setprecision i think
  104.  
  105.     cout << std::fixed << std::setprecision(2) << "Price: " << b.price << endl;//i knew there was something missing 
  106.     cout << "Tax: "<< b.price * 0.085 << endl;                                // using namespace std takes care of std::
  107.     cout << "Total Cost: "<< b.price * 1.085 << endl;                         //calling all elements with the '.' operand
  108.     cout << "*************************************" << endl;                  //i spent, ahhhhhh, about 40 hrs writing this prgm
  109.                                                                               //trashed about 9 builds resulting in perfection
  110.                                                                               //later JL Matzke
  111. }
  112.  
Hope your around in a few days when my program writing from file doesn't work. If you ever need anything. Joe
Jul 27 '07 #38
ilikepython
844 Recognized Expert Contributor
Python you've helped me through my first two weeks and I can't thank you enough. I now, have an understanding of structs and how they work, hard coding and why it is not good to do, much easier to change one attribute than it is to comb through code looking for every instance of your hard coded crap. I understand the while loop much deeper than I did, I picked up several new short cuts such as /n and /t which are handy.
Biggest thing is I've been up since 11am yesterday, left class at 6 and stayed up all night writing this program. I dumped the whole damn bowl of spaghetti and started over. I made an IPO chart, I made a visio flowchart, I used real movies with their directors, time et cetera and started coding.
I wrote each module by itself first, compiled along the way, fixed along the way, and viola a program, and it works.
I'm the happiest C++ guy in North America, or was until I looked in our doc share bin and found this weeks assignment sitting there. Yikes, I can't keep up...

Well, looks like I get to learn how to write a program that pulls from a file, and writes to the file, and updates and sorts the file for stock exchange symbols and their prices. Insanity to think I can write that! But I'm gonna try-

<That's the third time, I think I might start a thread in the feedback forum>

Hope your around in a few days when my program writing from file doesn't work. If you ever need anything. Joe
Congrats, good work Joe!!. That program is a big improvement over your others. I really liked what you did. Good luck, on your next assignement. It will surely be easier now that you have a written some programs and gained more experience. Also, when you get stuck, don't forget to use Google. I use it all the time and it can help you find what you need. And don't get frustrated, keep trying. Wish you good luck ;-)
Jul 28 '07 #39

Sign in to post your reply or Sign up for a free account.

Similar topics

30
4276
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
5
2921
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say that, a structure definition that includes function pointers only defines the function prototypes to be used with them, but not the actual implementations, whereas in C++, member functions cannot be changed *unless* virtual functions are used, or the
20
2650
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
61
3786
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
17
3482
by: Johan Tibell | last post by:
Could someone outline the pros and cons of typedefing pointers to structs like this? typedef struct exp_ { int val; struct exp_ *child; } *exp; (This is straight from memory so it might not even compile.)
5
1291
by: Daniele M. | last post by:
Hi folks! I'm almost newbie in c++ programming, so please don't blame me :) I have a little (i hope) problem with following code and i really can't understand what i'm missing :( Hope you can help me! The following code is from my simple GUI class.
1
4774
by: radskate360 | last post by:
Hi I am newer to programming and need a bit of help with this program. OK, heres the directions. The distance between two places on earth can be calculated by using their latitudes and longitudes. The calculation for this is as follows: (The latitudes and longitudes must be converted to radians (radians=degrees * pi / 180)). PI must be set set to 20 decimals as follows: PI = 3.1419265358979323846 Earth's Radius = 3963.1
5
1572
by: robert | last post by:
Hi all, I'm trying to understand some code - u-boot 1.3rc3 - that reads data from an ethernet phy. A struct is defined as: struct mii_dev { struct list_head link; char *name; int (* read)(char *devname, unsigned char addr, unsigned char reg, unsigned short *value);
17
1243
by: mdh | last post by:
Quick question. In the code below, omitting the word "word", as in char *word, produces an error of syntax. In similar "non-struct" declarations, the expression "char *" should suffice. ( I think). Is there a reason that one needs to add this? Is this unique to structs. Thanks as usual.
0
9735
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10427
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7687
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5710
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.