C ++中的结构从一开始就使用原型 [英] Structs in C++ using prototypes from the beginning

查看:64
本文介绍了C ++中的结构从一开始就使用原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前三个月的第一周,我们已经从编写循环和开关的小代码片段到编写整个程序,并希望它能够正常工作,实现了巨大的飞跃!

我,也没有我班上的其他人都听说过我们的第一个任务。我已经被淘汰了几天了,它不会编译超出我的结构声明。任何对C ++有所了解的人都会喜欢你指出迄今为止在这个程序中超过100行代码的前十几行中的所有语法和逻辑错误。以下是指南:


CIS 247:编程作业#1


值得分配5分:第1周到期:第3周

您与一家处理CD,书籍,电影和视频游戏的媒体公司签约,并将帮助他们管理其中一种媒体的库存。


创建一个程序,在内部存储至少八个媒体项目。使用名为book,cd,movie或videoGame的全局(声明为main())结构。包括至少五个成员变量(选择适当的数据类型)来表示媒体的不同属性(一些示例:页面,成本,运行时间,格式,精装,流派,?等)。

●其中一个必须是价格,因为我会计算税率(税率8.5%)和总成本。

●一个必须是一个char数组(C-String)和

●一个必须是一个缩写的字符(例如r表示红色)。你为其他两个选择了任何东西。


在main()中,创建6个全局月结构变量来表示六种不同的媒体项。例如,衬衫库存的结构可能是:


struct shirt {

char size; // S,M,L,X;

char品牌[15]; //使用C风格的字符串而不是字符串对象

bool shortSleeve;

char color; //对于所需的开关语句(r =红色,u =蓝色,g =绿色,w =白色,b =黑色,y =黄色)

双倍价格;

};


注意:上面的代码是一个例子,不能逐字使用。请选择您自己的变量名称和数据集。您可以使用类似的名称。结构可以按任何顺序包含变量。


您可以在声明时将数据加载到结构中,或者从文件中读取。

struct shirt concert = {''M'',?Hanes ?, true,?b?,12.5}; //这将是1号衬衫

struct shirt work = {''L'',?Sears ?, false,?u?,21.95}; //这将是2号球衣



? //衬衫7号

? //衬衫编号8


注意:只选择一种媒体类型,为变量提供自己的命名并使用您自己的(唯一)数据。

编写一个程序,生成一个报告,显示所有六个媒体项目(我在这里使用衬衫)。要求:


●使用switch()块创建文本菜单,要求提供媒体编号,或者为所有媒体编号创建0,或者99要退出。所有其他输入将导致错误消息。

●编写一个接受媒体编号作为int(1-8)的函数并打印相应的媒体包信息(请参阅示例输出),方法是适当的媒体作为参数的功能。它将使用switch()语句显示适当的颜色(或任何你想用作编码项目的颜色),


我将使用以下键作为衬衫的颜色: (r =红色,u =蓝色,g =绿色,w =白色,b =黑色,y =黄色)


样本输出 - 显示6项中至少4项的输出在您的库存中

衬衫菜单


请输入1-8列出衬衫

请输入0以列出所有衬衫

请输入99结束程序

选择:2


您已选择显示Sears衬衫的信息:

尺码:L

长袖

颜色:蓝色

价格:$ 21.95

税:$ 1.87

总费用:23.82美元


请注意,输出将取决于您选择的媒体类型(不是衬衫)和变量。 />

现在,我已经为你要看的代码付出了很多努力所以请记住6 mont以前我从未抚摸过一个代码密钥。

我不确定我是否会在正确的区域发布此内容,如果不是某人,请直接转到C ++发帖主题。



//分配一个CIS247 Joseph Matzke

#include< cstdlib>

#include< iomanip>

#include< iostream>

#include< string>

使用命名空间std;


struct movie

{

字符串评级; // G,PG,PG13,R17,X,XXX

char title; // Oceans 13,Oceans 12,Oceans 11,Braveheart,Harry Potter,Caligula

bool releaseDate; //仍然在剧院发行

char语言; //英语,西班牙语,日语,中文,德语,Swaheely

双倍费用[2];

双重征税;

};

void playMovie(struct movie); // 它不会在这里编译

int main(void)

{

int choice = 0;

//电影anyMovie; //声明 - 创建对象?

struct movie1 = {" R17"," Oceans 13",true,''E'',19.99}; //电影号码1

电影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};


系统(暂停);

返回0;

}


do

{

cout<< 请输入0以列出所有电影 << ENDL; //显示选择的菜单是有意义的

cout<< 请输入99以结束该程序 << ENDL; //显示菜单或停止程序

cout<< 选择:;


还有更多的代码,我的交换机模块,我的阵列等等,但是如果我不能,它们对我没有好处编译超出我注意到的地方。

我已经从--void main(void)更改为 - int main() - 更改为int main(void) - 无论我做什么,实际上我的原始代码都做了在main中没有返回但编译器消息似乎要我返回一个int。谁能发现问题?谢谢

解决方案

21.95

税:


1.87

总计费用:


23.82

请注意,输出将取决于您选择的媒体类型(不是衬衫)和变量。


现在,我已经为你要看的代码付出了很多努力所以请记住6个月前我从来没有抚摸过一个代码密钥。

我不确定我是否会在正确的区域发布此内容,如果不是某人,请直接转到C ++发帖主题。



//分配一个CIS247 Joseph Matzke

#include< cstdlib>

#include< iomanip>

#include< iostream>

#include< string>

使用命名空间std;


struct movie

{

字符串评级; // G,PG,PG13,R17,X,XXX

char title; // Oceans 13,Oceans 12,Oceans 11,Braveheart,Harry Potter,Caligula

bool releaseDate; //仍然在剧院发行

char语言; //英语,西班牙语,日语,中文,德语,Swaheely

双倍费用[2];

双重征税;

};

void playMovie(struct movie); // 它不会在这里编译

int main(void)

{

int choice = 0;

//电影anyMovie; //声明 - 创建对象?

struct movie1 = {" R17"," Oceans 13",true,''E'',19.99}; //电影号码1

电影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};


系统(暂停);

返回0;

}


do

{

cout<< 请输入0以列出所有电影 << ENDL; //显示选择的菜单是有意义的

cout<< 请输入99以结束该程序 << ENDL; //显示菜单或停止程序

cout<< 选择:;


还有更多的代码,我的交换机模块,我的阵列等等,但是如果我不能,它们对我没有好处编译超出我注意到的地方。

我已经从--void main(void)更改为 - int main() - 更改为int main(void) - 无论我做什么,实际上我的原始代码都做了在main中没有返回但编译器消息似乎要我返回一个int。谁能发现问题?感谢

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

解决方案

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


这篇关于C ++中的结构从一开始就使用原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆