如何编写这个初学者C ++问题? [英] How do I code this beginner C++ problem?

查看:95
本文介绍了如何编写这个初学者C ++问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个邮寄物品的邮资取决于物品的基本费用:如果基本费用低于100,则收取低额费用;否则,收取高额费用。物品的总成本是其基本成本和邮资的总和。该程序输入2个项目的数量和基本成本作为整数,并输出它们的总成本之和,再次作为整数。



什么我试过了:



我试过,如果,别的东西,但我不知道怎么去解决这个问题。我也试过投入一笔费用和数量。

我添加了这个但我得到了语法错误  如果  否则。所以这个就是我所拥有的:

if (sum< 100)
cout<< << endl;
int 费用=总和+总和<100;
else
cout<< << ENDL;
int 成本=总和+总和> = 100 ;
cout<< 费用<<<<< endl;

其他 我改进了这个
此外,我希望输出显示完整的金额加上低高邮资价格。

}

解决方案

我们不做你的家庭作业。

HomeWork是不会在乞求其他人做你的工作时测试你的技能,它将帮助你的老师检查你对所学课程的理解以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,开始工作吧。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



[更新]

< blockquote class =quote>

引用:

你是否想过我可以尝试自己从教科书中解决这个问题?

教科书中的练习是与以前的课程相关,所以,可能重读课程可以提供帮助。

如果你遇到困难,你可以找到教程和视频(在YouTube上)。



以下是语言作者对C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2。 pdf [ ^ ]

http://www.ime.usp。 br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]



C ++编程语言 [ ^ ]



[更新]

阅读此内容并注意{}用法:

什么是花括号? [ ^ ]


  if (sum< 100)
cout<< << endl; // if语句在此处结束
int 费用=总和+总和<100; // 这个表达式应该计算什么?
else // 此else未连接到任何if语句
cout<< High<< endl;
int 成本=总和+总和> = 100 ; // 这个表达式应该计算什么?
cout<< Cost<<<<< endl;
}



查看上述内容,您需要在尝试编写任何代码之前,返回学习指南并学习该语言的基本语法。


解决方案2报告代码中的大多数问题,并指出我们不理解的代码。



我们读了这个问题但没有公式和代码,很难知道实际的公式,没有任何公式,这个问题不会导致明确的答案。

显然,从上面的代码中,你不知道在C ++中, {} 用于分隔范围。在尝试编写代码之前,你需要学习基本的语法。



我们学习一门语言,你应该有附近的文档,而不是尝试随机的东西,并期望它工作。正如您在以下链接中看到的那样,当 if else 子句中有多个语句时,您需要把它们放在一个块语句中。块由 {} 分隔。



if-else Statement(C ++) [ ^ ]

如果C ++中的语句 - Cprogramming.com [ ^ ]

语句和流程控制 - C ++教程 [ ^ ]



事实上,许多程序员都会建议使用荣誉,即使你有一个声明,因为它代码更容易阅读和维护(因为它不容易出错)。



在C ++中,缩进不会影响解析但是你总是应该有正确的缩进。它使代码更容易阅读(并发现不平衡的代码)。



但是你甚至应该在Code Project上缩进你的代码。事实上,那些懒得缩进代码的人不值得任何帮助。所以,我们真的很感激我们会帮助你。


The postage for each mailed item depends on the base cost of the item: if the base cost is less than 100, then an amount lowpostage is charged; otherwise, highpostage is charged. The total cost of an item is the sum of its base cost and its postage. This program inputs the quantity and base cost of 2 items as integers, and outputs the sum of the total costs for them, again as an integer.

What I have tried:

I have tried if, else stuff but im not sure how to go about solving this. I have also tried putting in a cost and the quantity.

I added this but I get syntax error in if and else. So this is what I have:

if (sum<100)
cout<<"Low"<<endl;
int Charge=sum+sum<100;
else
cout<<"High"<<endl;
int Cost=sum+sum>=100;
cout<<"Cost "<<Cost<<endl;

There is a syntax error in else how do I improve this ?
Also I would like the output to show the complete sum plus low or high postage price.

}

解决方案

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

[Update]

Quote:

Has it occurred to you that I could be trying to solve this problem from a textbook on my own?

Exercises in textbooks are related to previous lessons, so, may be rereading the lesson can help.
If you are stuck, you can find tutorials and videos (on YouTube).

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]

[Update]
read this and pay attention to {} usage:
What is curly bracket?[^]


if (sum<100)
cout<<"Low"<<endl;      // your if statement ends here
int Charge=sum+sum<100; // what is this expression supposed to calculate?
else                    // this else is not connected to any if statement
cout<<"High"<<endl;
int Cost=sum+sum>=100;  // what is this expression supposed to calculate?
cout<<"Cost "<<Cost<<endl;
}


Looking at the above you need to go back to your study guides and learn the basic syntax of the language before attempting to write any code.


Solution 2 report most problems with your code and also point out code we don't understand.

We read the question but without formula nor code, it is hard to know actual formula and without any formula, the question does not lead to definitive answer.
Obviously, from the code above, you do not know that in C++, { and } are used to delimit scope. You need to learn basic syntax before trying to write code.

We you learn a language, you should have documentation near instead of trying random things and expect it to work. As you would see in the following links, when there are multiple statements in an if or else clause, you need to put them in a block statement. A block is delimited by { and }.

if-else Statement (C++)[^]
If Statements in C++ - Cprogramming.com[^]
Statements and flow control - C++ Tutorials[^]

In fact, many programmer would recommend to always use accolades even when you have a single statement as it make the code somewhat easier to read and maintain (as it is less error prone).

In C++, indentation does not affect parsing but you should always have the correct indentation anyway. It make the code much easier to read (and to spot unbalanced code).

But you should really indent your code even on Code Project. In fact, people that are too lazy to indent their code do not deserve any help. So you should really be grateful that we try to help you.


这篇关于如何编写这个初学者C ++问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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