Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
C++
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.

}
Posted
Updated 7-Sep-16 2:37am
v4
Comments
Richard MacCutchan 6-Sep-16 13:06pm    
Please try again, and show us the code, and any error details that you see.
Member 12723446 6-Sep-16 19:10pm    
Hi,
This is what I have

#include <iostream>
using namespace std;
main() {
int first, second;
cout<<"Quantity + cost."<<endl;
cin>>first>>second;
}
[no name] 6-Sep-16 13:08pm    
We can't teach you programming in a forum posting. Show us what you have tried and describe a problem with your code.
Member 12723446 6-Sep-16 19:11pm    
I have this but I dont know how to continue with boolian

#include <iostream>
using namespace std;
main() {
int first, second;
cout<<"Quantity + cost."<<endl;
cin>>first>>second;
}
[no name] 7-Sep-16 9:15am    
That is because there is no boolian in your code anywhere.

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?[^]
 
Share this answer
 
v3
Comments
Member 12723446 6-Sep-16 19:06pm    
Has it occurred to you that I could be trying to solve this problem from a textbook on my own? I will not go about how naive it is to think every question is home work.
Philippe Mori 6-Sep-16 20:41pm    
Solving a problem in a book is as much an homework than solving a problem asked by a professor.
Member 12723446 6-Sep-16 21:59pm    
Uhm no it's not.
[no name] 7-Sep-16 9:16am    
Uhm, yes it is.
Member 12723446 7-Sep-16 9:18am    
Nuhuh
C++
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.
 
Share this answer
 
Comments
Member 12723446 7-Sep-16 7:15am    
It's suppose to add the high or low to the output in the end which I didn't add because I was not sure and it doesn't say in the guides. Did you even read the question?

Anyways turns out the issue was I had to declare ints before if and else and I had indentations missing in else. Thanks anyways folks.
Richard MacCutchan 7-Sep-16 7:25am    
Yes I did read the question, but the code that you presented was confusing and did not address the issues in the question.
Member 12723446 7-Sep-16 7:48am    
Then you should've asked what you were confused about.
Philippe Mori 7-Sep-16 8:34am    
You should write question that are not confusing! And you did not answer all our questions. For example, where do you answer to the question "what is this expression supposed to calculate?" in the above answer.

Do you really thing that we can guess what you have not written? Put some effort in asking good questions.
Member 12723446 7-Sep-16 9:15am    
Ok relax. So much for reading from my own solution.
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.
 
Share this answer
 
Comments
Member 12723446 7-Sep-16 9:15am    
K

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900