please help me please

hi guys^^
I wanna to write a program like this in c++:
get the weight of n bag and then calculate the minimum cost for n bag in a bag.
( in first line get n number and in second line get n numbers and then print the minimum cost)
If there is only one bag, integration cost is zero!

for example like this:

input:

5

4 23 9 17 2

output:

108

Please help me


I think this is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <iostream>
using namespace std;

int main() {
	{
    float price = 0; int num = 0;
    cin>> num;
	cout<<num<<endl;
	
    cin>> num;
	cout<<num<<endl;
    
    price = price * num;
	cout<< price <<endl;}
    getchar();
}
Last edited on
This will always cout 0 because price is initialized to 0 and youre not assigning anything else prior to price = price * num;
Can you please help me?
I don't know what should i do ((
Lines 6,9: Why are you doing a cin >> num twice? Did you mean to input price?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Can you please help me?
I don't know what should i do ((

You need to ensure the value of price is set to a value you want it to be set to. How do you want to set it? Is the user supposed to input it?
Last edited on
Just change the second
1
2
cin>> num;
cout<<num<<endl;

to cin/cout price instead. You also dont need the extra brackets
@AbstractionAnon
Hi, thank you , i got correct my post ^_^
i wrote two cin>>num because of this => in first line get n number and in second line get n numbers
and my mean of num is weight

my program not correct, i can't see correct output
please help me
thanks
@MikeyBoy

hi,the value of price is:
the cost of the size of the total weight of bags
@DrZoidberg
Hi, i changed to price but i can't see output
My program get out in immediately,i can't see result
even i wrote getchar but was not true ((
@DrZoidberg

i got correct like this but i can't see any result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {
	
    float price = 0; int num = 0;
        cin>> num;
	cout<<num<<endl;
	
    cin>> price;
	cout<<price<<endl;
    
    price = price * num;
	cout<< price <<endl;
    getchar();
}
for example like this:

input:

5

4 23 9 17 2

output:

108
get the weight of n bag and then calculate the minimum cost for n bag in a bag.
( in first line get n number and in second line get n numbers and then print the minimum cost) If there is only one bag, integration cost is zero!


Your problem statement is not clear. What does "for n bag in a bag" mean?
"integration cost" - What is this?

Please explain how you arrived at an output of 108 given the 5 numbers above.

Your program, as written, only reads two values (num and price).

Last edited on
@AbstractionAnon
I wanna to get sum of the weight of n bag in a bag
My mean of that example is first number print in first line and then other numbers print in second line and finally print sum of the weight of n bag
I suspect it exits because you're trying to enter multiple values but are only reading one when getting price. cin uses spaces and newlines as delimiters, so getchar() doesn't have to wait for additional input when you enter "4 23 ..." - it just gets the first character after the first space.

If you want to read multiple values in one line use the std::getline function

Running the program from the command prompt will allow you to see the output even after it terminates. Won't fix the problem, but it could help you understand what's going on

That said, I don't really understand how you got 108
You haven't really clarified the problem statement. Are you simply trying to find the sum of the weight of some arbitrary number of bags?

The sum of the 5 numbers you posted is 55. Like DrZoidberg, I don't understand how you arrived at 108 as the expected output.

You still have not explained what "integration cost" is.

Until you can be more clear about the problem, I'm afraid I can't offer any more help.

Last edited on
Topic archived. No new replies allowed.