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 3047
VRX, if you think I would know how to write that code assignment you're crazy! This is the first 'real' program I have ever tried to write myself. If you wrote some code I might see some things wrong? I have 6 days left to finish this one and I'm going backwards. I thought you were explaining to me how to approach writing a program when I started reading your posting-)
No, I wrote "Hello World" 3 months ago so I'm not the one to ask. If you thought you were posting your own thread, you were not: You simply tagged on to mine. Good luck, I know the feeling.
On to my little project that is getting there, slowly but getting there. I've decided it's easier to just write out my initial cout's as this:
cout << "Braveheart " << movie4 << endl; in order to bring the variable name to the same line as the title. The console manipulation is too advanced and I don't have the time.
I do have a question that someone may know the answer to: I can compile to line, I can't say which line until I post the code, but it's this line
switch (choice)
and the compiler is telling me 'expected unqulified-id before "switch"
which I had the same message in the prior line were I had this
movie *pmovie;=NULL;
and needed to remove ; before the = operator.
Now, I don't see anything wrong with the code, and even if I // that line out it will not compile any further. Any ideas what is wrong?
Thank You!
Joe -
-
<I had to snip your code otherwise my post wouldn't show>
-
-
void playMovie (movie)
-
-
-
{
-
int choice = 0;
-
cout << "Choose a movie by entering 1 thru 6" << endl;
-
cout << "Selection :";
-
cin >> choice;
-
}
-
-
movie *pmovie = NULL; //pointer to movie that user chooses
-
-
switch (choice)
-
{
-
case 1:
-
pmovie = &movie1;
-
break;
-
case 2:
-
pmovie = &movie2;
-
break;
-
case 3:
-
pmovie = &movie3;
-
break;
-
case 4:
-
pmovie = &movie4;
-
break;
-
case 5:
-
pmovie = &movie5;
-
break;
-
case 6:
-
pmovie = &movie6;
-
break;
-
}
-
I believe once I get past this switch the real problems are going to occur, the output I've changed around so far, and have compiled it to this point. Now, will it work, and I also need to rearrange some code also towards the end but I'd like to see how it outputs first. Anyway, thanks again. Later
You ended the function again. Look at line 75. You added the closing brace to playMovie().
Shouldn't the code with the switch appear in main, if your playMovie is supposed to display the variables in a movie object? If not, what is playMovie supposed to do. I imagined playMovie like this: -
void playMovie(movie myMovie)
-
{
-
cout << "Movie name: " << myMovie.movie << endl;
-
cout << "Movie cost: " << myMovie.cost << endl;
-
cout << "Movie rating: " << myMovie.rating << endl;
-
cout << "Movie tax: " << myMovie.tax << endl;
-
// other members of a movie
-
}
-
Then in main you would prompt the user for a movie and then call playMovie with that movie.
Python, there's a contributor listed under the name Python at the O'Reilly site we visited in class today while searching through different code help, would that be you?
That's probably not me, I'm not that good (lol). I haven't contributed to anything so, probably not me. Plus, I've barely been programming for 6 months.
I thought you always needed a brace around any statement block? I've never written more than one statement block before. Now, it does compile down to the first break in the switch and says I haven't declared movie1 yet, I guess I closed main up at line where I have return 0; following all those couts, but if I don't put a brace there it won't compile. I need to move some blocks of code up higher in main first, this is going to take a while. Thanks.......ag ain. Joe
No I didn't either, I have one open and one closed brace running now that I've removed the one at line 75. I don't know how I keep doing that. Now, the compiler hangs up at the first break in switch as it was. So, I haven't declared the variables correctly, or elements since I am using a struct. I don't understand how the pointers work I guess. I think if I change my switch to a regular switch it will work, I'm going to try it.
I know how a switch works, and I can't turn it in if I don't understand what everything in it is doing. I appreciate the jump start in pointers, but I think I'll wait until we get there. He'll be wondering how I knew about pointers anyway if I used them, which wouldn't matter if I understood them, but I don't.
Let me try changing the switch. Thanks again. Joe
OK, I'm back to abusing my defenseless mouse. Apparently there is something I don't understand about passing the value in a struct.
Why won't my switch compile??
I've tried using numerous variables in the
switch (rightHerePart)
and it's not compiling, and driving me nuts. What would the proper call be in this instance, I think and thought it would be simply
switch (myMovie.choice )
but apparently not! Do you have any ideas for me to try? Anyone/?
It compiles through the first 100 lines down to line 17 above with the following error message: struct has no member named choice--I know that, that's the cin>> I am switching. OMG!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!! Bye
Thank You - cout << setw(2) << "movie6" << endl;
-
}
-
//system ("pause");
-
//return 0;
-
}
-
void playMovie ( struct movie myMovie)
-
-
{
-
int choice = 0;
-
cout << "Choose a movie by entering 1 thru 6" << endl;
-
cout << "Selection :";
-
cin >> choice;
-
-
-
//movie *pmovie = NULL; //pointer to movie that user chooses
-
-
switch (myMovie.choice)
-
{
-
case 1:
-
movie1 = "Oceans 13";
-
break;
-
case 2:
-
movie2 = "Oceans 12";
-
break;
-
case 3:
-
movie3 = "Oceans 11";
-
break;
-
case 4:
-
movie4 = "Braveheart";
-
break;
-
case 5:
-
movie5 = "Harry Potter";
-
break;
-
case 6:
-
movie6 = "Caligula";
-
default: "Invalid entry";
-
}
I'm done for the night, I won't have any computers left if I keep trying on this *&%^* program.
Joe
OK, I'm back to abusing my defenseless mouse. Apparently there is something I don't understand about passing the value in a struct.
Why won't my switch compile??
I've tried using numerous variables in the
switch (rightHerePart)
and it's not compiling, and driving me nuts. What would the proper call be in this instance, I think and thought it would be simply
switch (myMovie.choice )
but apparently not! Do you have any ideas for me to try? Anyone/?
It compiles through the first 100 lines down to line 17 above with the following error message: struct has no member named choice--I know that, that's the cin>> I am switching. OMG!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!! Bye
Thank You - cout << setw(2) << "movie6" << endl;
-
}
-
//system ("pause");
-
//return 0;
-
}
-
void playMovie ( struct movie myMovie)
-
-
{
-
int choice = 0;
-
cout << "Choose a movie by entering 1 thru 6" << endl;
-
cout << "Selection :";
-
cin >> choice;
-
-
-
//movie *pmovie = NULL; //pointer to movie that user chooses
-
-
switch (myMovie.choice)
-
{
-
case 1:
-
movie1 = "Oceans 13";
-
break;
-
case 2:
-
movie2 = "Oceans 12";
-
break;
-
case 3:
-
movie3 = "Oceans 11";
-
break;
-
case 4:
-
movie4 = "Braveheart";
-
break;
-
case 5:
-
movie5 = "Harry Potter";
-
break;
-
case 6:
-
movie6 = "Caligula";
-
default: "Invalid entry";
-
}
I'm done for the night, I won't have any computers left if I keep trying on this *&%^* program.
Joe
You have all of that code in playMovie. I think it should be in main. What do you want playMovie to do?
Second, why would you put "myMovie.choice " if the variable "choice" has nothing to do with movie objects. A switch performs different actions depending on the value of the variable in the parenthesis. In your case, the variable you want to check is "choice". Here is the switch rewritten without using pointers: -
cout << "Choose a movie by entering 1 thru 6" << endl;
-
cout << "Selection :";
-
cin >> choice;
-
-
movie myMovie;
-
switch (choice)
-
{
-
case 1:
-
myMovie = movie1;
-
break;
-
case 2:
-
myMovie = movie2;
-
break;
-
case 3:
-
myMovie = movie3;
-
break;
-
case 4:
-
myMovie = movie4;
-
break;
-
case 5:
-
myMovie = movie5;
-
break;
-
case 6:
-
myMovie = movie6;
-
break;
-
default: // could be avoided if we used a do while as we used previously
-
cout << "Invalid movie number" << endl;
-
return 1;
-
break;
-
}
-
playMovie(myMovie); // would display the movie's member variables
-
Just as a note, playMovie shoudn't care what movie it is being passed, it only cares that you pass a movie. It should know nothing of the actual program, only that it should print out the variables of a movie object. Hope that helps.
Yes you are right, I am not thinking how the values are being passed using 'struct'.
This compiles but does not break, the code falls through to the default when choosing either 1 thru 6 to "Invalid movie number, why? I compared this switch to several I have written last term and see nothing that would allow the case to be ignored and program to drop through all breaks.
How do you know so much in 6 months? You've been coding longer than that?
Where is my logical error now, I thought syntax errors would make logical easy to find, I don't see it, maybe I should stop for a while, I've rewritten this code several dozen times tonight alone. Yikes.
Thanks You
- {
-
int choice = 0;
-
cout << "Choose a movie by entering 1 thru 6" << endl;
-
cout << "Selection :";
-
cin >> choice;
-
}
-
-
movie myMovie;
-
switch (choice)
-
{
-
case 1:
-
myMovie = movie1;
-
break;
-
case 2:
-
myMovie = movie2;
-
break;
-
case 3:
-
myMovie = movie3;
-
break;
-
case 4:
-
myMovie = movie4;
-
break;
-
case 5:
-
myMovie = movie5;
-
break;
-
case 6:
-
myMovie = movie6;
-
break;
-
default: cout << "Invalid movie number" << endl;
-
system ("pause");
-
return 1;
-
break;
-
-
}
It falls through all breaks no matter which case number you choose, why?
- {
-
int choice = 0;
-
cout << "Choose a movie by entering 1 thru 6" << endl;
-
cout << "Selection :";
-
cin >> choice;
-
}
-
-
movie myMovie;
-
switch (choice)
-
{
-
case 1:
-
myMovie = movie1;
-
break;
-
case 2:
-
myMovie = movie2;
-
break;
-
case 3:
-
myMovie = movie3;
-
break;
-
case 4:
-
myMovie = movie4;
-
break;
-
case 5:
-
myMovie = movie5;
-
break;
-
case 6:
-
myMovie = movie6;
-
break;
-
default: cout << "Invalid movie number" << endl;
-
system ("pause");
-
return 1;
-
break;
-
-
}
It falls through all breaks no matter which case number you choose, why?
You could try printing choice right before the switch to see what value it has, or you can use this loop: -
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);
-
// at this point choice will be 1 - 6
-
It would be much easier to use an array of movies than all those movie variables. That would eliminate the need for a switch.
After you declare the movies you just write this: -
movie movies[6] = {movie1, movie2, movie3, movie4, movie5, movie6};
-
Now you don't need a switch, you just call playMovie lke this: -
playMovie[movies[choice-1]);
-
- /*{
-
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;
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
by: pinkfloydhomer |
last post by:
Is it well-defined and portable to do something like:
typedef struct
{
int type;
char c;
} S1;
typedef struct
{
|
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.
|
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.)
| |
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.
|
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
|
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);
|
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.
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |