Structs in C++ using prototypes from the beginning

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeschnell
    New Member
    • Apr 2007
    • 47

    #31
    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

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #32
      Originally posted by joeschnell
      [CODE=cpp] /*{
      int choice = 0;
      cout << "Choose a movie by entering 1 thru 6" << endl;
      cout << "Selection :";
      cin >> choice;
      }*/
      //playMovie [movies[choice - 1]);
      do

      {

      cout << "Choose a movie by entering 1 thru 6" << endl;

      cout << "Selection :";

      cin >> choice;
      }
      while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);
      {
      system ("pause");
      return 0;

      }
      // playMovie (movies [choice - 1];

      /*
      while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);
      movie myMovie;
      switch (choice)
      {
      case 1:
      myMovie = movie1;
      break;
      case 2:
      myMovie = movie2;
      break;
      case 3:
      myMovie = movie3;
      break;[/CODE]

      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.
      [CODE=cpp] /*{
      int choice = 0;
      cout << "Choose a movie by entering 1 thru 6" << endl;
      cout << "Selection :";
      cin >> choice;
      }*/
      //playMovie [movies[choice - 1]);
      do

      {

      cout << "Choose a movie by entering 1 thru 6" << endl;

      cout << "Selection :";

      cin >> choice;
      }
      while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);
      { // what's this?
      system ("pause"); //
      return 0; //

      } //
      // playMovie (movies [choice - 1]); // that should work

      /*
      while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);
      movie myMovie;
      switch (choice)
      {
      case 1:
      myMovie = movie1;
      break;
      case 2:
      myMovie = movie2;
      break;
      case 3:
      myMovie = movie3;
      break;[/CODE]
      You almost have it. I don't know why you had that code with the system call and the return statement. This should work:
      [code=cpp]
      int main()
      {
      int choice = 0;
      //movie anyMovie; //declaration-creates object?
      movie movie1={'R', "Oceans 13", true, 'E', 19.99, .08}; //movie number 1
      movie movie2={'R', "Oceans 12", false, 'E', 9.99};
      movie movie3={'R', "Oceans 11", false, 'E', 9.99};
      movie movie4={'R', "Braveheart ", false, 'E', 9.99};
      movie movie5={'G', "Harry Potter", false, 'E', 9.99};
      movie movie6={'X', "Caligula", false, 'S', 1.99};\

      movie movies[6] = {movie1, movie2, movie3, movie4, movie5, movie6};


      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: ";
      cin >> choice;
      cout << endl; cout << endl; cout << endl;
      }
      while (choice != 0 && choice !=99); //repeat if choice not 0 or 99

      if (choice == 99)
      {
      system ("pause");
      return 0;
      }

      if (choice == 0)
      {
      cout << "Movie 1:\t" << movies[0].movie << endl; // avoid hard coding literals
      cout << "Movie 2:\t" << movies[1].movie << endl;
      cout << "Movie 3:\t" << movies[2].movie << endl;
      cout << "Movie 4:\t" << movies[3].movie << endl;
      cout << "Movie 5:\t" << movies[4].movie << endl;
      cout << "Movie 6:\t" << movies[5].movie << endl << endl;
      }

      do
      {
      cout << "Choose a movie by entering 1 thru 6" << endl;
      cout << "Selection :";
      cin >> choice;
      } while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6); // make sure choice is 1 through 6

      playMovie(movie s[choice-1]); // display correct movie; note: indexing starts at 0

      system ("pause");
      return 0;
      }
      [/code]
      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.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #33
        Originally posted by joeschnell
        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:
        [code=cpp]
        void funcName(movie testMovie);
        1 2 3 4
        1 = return type of function
        2 = name of function
        3 = type of parameter, in your case movie
        4 = name of parameter (optional in prototype)
        [/code]
        What don't you understand?

        Comment

        • joeschnell
          New Member
          • Apr 2007
          • 47

          #34
          OK, few questions
          Code:
           #include <iostream>
          #include <iomanip>
          #include <cstdlib>
          #include <string>
          using namespace std;
          struct movie
          {
                 char rating; //G,PG,PG13,R17,X
                 string movie; //Oceans 13, Oceans 12, Oceans 11, Braveheart, Harry Potter, Caligula
                 bool releaseDate;//released-inTheatre
                 char language;//English E, Spanish S, Japanese J, Chinese C, German G, Swaheely S
                 double cost; 
                 double tax;
          };
          void playMovie (movie);
             
                int main()
             
                {
             
                    int choice = 0;
             
                   // movie anyMovie;  //declaration-creates object?
             
                    movie movie1={'R', "Oceans 13", true, 'E', 19.99, .08}; //movie number 1
             
                    movie movie2={'R', "Oceans 12", false, 'E', 9.99};
             
                    movie movie3={'R', "Oceans 11", false, 'E', 9.99};
             
                    movie movie4={'R', "Braveheart", false, 'E', 9.99};
          
                    movie movie5={'G', "Harry Potter", false, 'E', 9.99};
            
                    movie movie6={'X', "Caligula", false, 'S', 1.99};\
            
                   
            
                    movie movies[6] = {movie1, movie2, movie3, movie4, movie5, movie6};
            
                   
            
                       
            
                   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:  ";
            
                       cin >> choice;
            
                       cout << endl; cout << endl; cout << endl;
            
                   }
            
                   while (choice != 0 && choice !=99);   //repeat if choice not 0 or 99
            
                       
            
                    if (choice == 99)
            
                    {
            
                        system ("pause");
            
                        return 0;
            
                    } 
            
                     
            
                    if (choice == 0)     
            
                    {     
            
                        cout  << "Movie 1:\t" << movies[0].movie << endl;     // avoid hard coding literals
            
                        cout  << "Movie 2:\t" << movies[1].movie << endl;
            
                        cout  << "Movie 3:\t" << movies[2].movie << endl;
            
                        cout  << "Movie 4:\t" << movies[3].movie << endl;
            
                        cout  << "Movie 5:\t" << movies[4].movie << endl;
            
                        cout  << "Movie 6:\t" << movies[5].movie << endl << endl;
            
                    }
            
                 
            
                    do
            
                    {
            
                        cout << "Choose a movie by entering 1 thru 6" << endl;
            
                        cout << "Selection :";
            
                        cin >> choice;
            
                    } 
                    while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);  // make sure choice is 1 through 6
            
                 
            
                    playMovie(movies[choice-1]); // display correct movie; note: indexing starts at 0
            
                   
            
                    system ("pause");
            
                    return 0;     
            
                }
          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

          Comment

          • joeschnell
            New Member
            • Apr 2007
            • 47

            #35
            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:

            Code:
             while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);  // make sure choice is 1 through 6
              
                   
              
                      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:
            Code:
             if (choice == 0)     
              
                      {     
              
                          cout  << "Movie 1:\t" << movies[0].movie << endl;     // avoid hard coding literals
            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

            Code:
             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

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #36
              Originally posted by joeschnell
              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.

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #37
                Originally posted by joeschnell
                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:

                Code:
                 while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6);  // make sure choice is 1 through 6
                  
                       
                  
                          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:
                Code:
                 if (choice == 0)     
                  
                          {     
                  
                              cout  << "Movie 1:\t" << movies[0].movie << endl;     // avoid hard coding literals
                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

                Code:
                 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.

                Comment

                • joeschnell
                  New Member
                  • Apr 2007
                  • 47

                  #38
                  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
                  Code:
                  // Assignment 1 CIS 247
                  // Joseph L Matzke
                  
                  
                  #include <iostream>
                  #include <iomanip>
                  
                  using namespace std;
                  
                  struct Movie {
                  	char  title[256];//Max her out
                  	char  director[30];//Give me enough room
                  	int  minutes; //Just to come up with some other element
                  	char genre; // Technical, Fantasy, History, Classics gives me another attribute
                  	double price; //Seems technical books out cost classics by far, it's insane
                  
                  };
                  
                  void showItem(Movie );
                  
                  int main()
                  {
                  	
                  	Movie movie[8] =    //might as well be eight or eighty just more typing once you get it
                  	{
                  		{"Harry Potter and the Deathly Hallows", "J. K. Rowling ",159, 'F',17.99},
                  		{"Armed America: Documentary of Gun Owners in Their Homes", "Kyle Cassidy",94,'H',19.80},
                  		{"The Old Man and The Sea","Ernest Hemingway",112,'C', 9.80},
                  		{"Adventures of Huckleberry Finn (Mark Twain Library)","Mark Twain",129,'C',10.17},
                  		{"A Christmas Carol","Charles Dickens",145,'C',4.49},//lists them in order like a prototype
                  		{"C++ Programming (THIRD EDITION)", "D S MALIK",71,'T',127.30},
                  		{"Legacy of Ashes: The History of the CIA", "Tim Weiner", 120, 'H', 15.12},
                  		{"Introduction to Programming with C++", "Diane Zuk", 50, 'T', 123.79}//lists object elements,attributes
                  
                  	};
                  
                  	
                  
                  	
                  	bool exit = false;//assign false to exit, gonna change that down the line
                  	do {
                  		cout << "Type item no. to display info; 0 to display all the items, 99 to exit" << endl << endl;
                  		int choice;
                  		cin >> choice;
                  
                  		switch (choice) {
                  			case 1:
                  			case 2:
                  			case 3:
                  			case 4:
                  			case 5:
                  			case 6:
                  			case 7:
                  			case 8:
                  				
                  				showItem(movie[choice-1]);//using my array to display, it starts at 0 so I less one -)
                  				break;
                  			case 0:
                  				cout << "Here are all the movies available :" << endl;
                  				for (int i=0; i < 8; i++) //here's my array loop
                                                  {
                  					showItem(movie[i]);
                  				}
                  				break;
                  
                  			case 99: 
                  				exit = true;		
                  				break;
                  			default:
                  				cout << "Incorrect value entered; try again" << endl;
                  		
                  		}
                  	} while (!exit);//while we are not exiting
                  
                  	
                  	return 0;
                  }
                  
                  void showItem(Movie b) {
                  	
                  	
                  
                  	cout << endl << "*************************************" << endl;//stole this idea from Zeschke
                  	cout << "Information on " << "\""<<b.title<<"\":" << endl;
                  	cout << "Director: " << b.director << endl;
                  	cout << "Title: " << b.title << endl;
                  	cout << "Number of minutes: " << b.minutes << endl;
                  
                  	cout << "Genre: ";//calling my struct attributes with the '.' operator for my switch statement
                  	switch (b.genre) {
                  		case 'T' : cout << "Technical";
                  				break;
                  		case 'F' : cout << "Fantasy";
                  				break;
                  		case 'H' : cout << "History";
                  				break;
                  		case 'C' : cout << "Classics";
                  				break;
                  		default : cout << "!!Wrong Genre";
                  
                  	}
                  
                  	cout << endl;                                 //usually you don't need std:: except when using setprecision i think
                  
                  	cout << std::fixed << std::setprecision(2) << "Price: " << b.price << endl;//i knew there was something missing 
                  	cout << "Tax: "<< b.price * 0.085 << endl;                                // using namespace std takes care of std::
                  	cout << "Total Cost: "<< b.price * 1.085 << endl;                         //calling all elements with the '.' operand
                  	cout << "*************************************" << endl;                  //i spent, ahhhhhh, about 40 hrs writing this prgm
                                                                                                //trashed about 9 builds resulting in perfection
                                                                                                //later JL Matzke
                  }
                  Hope your around in a few days when my program writing from file doesn't work. If you ever need anything. Joe

                  Comment

                  • ilikepython
                    Recognized Expert Contributor
                    • Feb 2007
                    • 844

                    #39
                    Originally posted by joeschnell
                    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 ;-)

                    Comment

                    Working...