Question Trying to figure this out

wingar

Member
Joined
Oct 19, 2009
Messages
5
Programming Experience
Beginner
Hi,
Just like alot of other people, I'm also new(about 2 months) to VB.NET. I have this program that your suppose to have it figure out how many chocolate bars & coupons you'll have left. I have gotten it this far but for the life of me, I'm missing something. Anyone see what I'm not doing?

Thanks

Suppose you can buy a chocolate bar from the vending machine for $1 each. Inside every chocolate bar is a coupon. You can redeem seven coupons for one chocolate bar from the machine. You would like to know how many chocolate bars you can eat, including from those redeemed via coupons, if you have n dollars.
For example, if you have 20 dollars, then you can initially buy 20 chocolate bars. This gives you 20 coupons. You can redeem 14 coupons for two additional chocolate bars. These two additional chocolate bars give you two more coupons, so you now have a total of eight coupons. This gives you enough to redeem for one final chocolate bar. As a result you now have 23 chocolate bars and two leftover coupons.



VB.NET:
 Sub Main()
        ' define variable
        Dim again As String = "y"
        Dim bars, coupons As Integer
        Dim money As Integer
        Dim chocolatebars As Integer

        Console.WriteLine("Enter Dollar Amount: ")
        money = Convert.ToInt32(Console.ReadLine())
        ' begin outer loop for money
        Do While again = "y" Or again = "Y"
            Do While coupons >= 7
                bars = money + coupons
                chocolatebars = money \ 7
            Loop
            Console.WriteLine("You Get this Amount of Chocolate Bars: " & Math.Round(chocolatebars, 1))
            Console.WriteLine("Do you want to do this again? (y(n)")
            again = (Console.ReadLine())
        Loop

    End Sub
 
The inner loop will only execute when you have more than 7 coupons. But you haven't set how many coupons you have. So your inner loop never executes. Also when you hit again, you are not going to re-execute everything, just the loop. That means you wont be able to get a new money amount.

I think you could do it a bit easier. most of your code needs to go in that first do loop.

Something like this should do the trick
VB.NET:
While....
    Get User Input
    declare couponCount and set to user input
    declare chocolateBarCount and set to user input

    while coups >= (?)
      couponCount  -= (?)
      chocolateBar  += (?)
    end while

    show chocolateBar and ask again  
End While

Translate that to working code and you will be all set. If this isn't for a homework assignment and you want working code ill be happy to provide it.
 
Thanks

Thanks alot for your help. I could sit here and say "it isn't for homework" , but I need to learn it. I understand what most things do, but it's the structure I need to work on. You would think that an instructor would have you build from small programs to more complex ones. This is my second program to write. The first one was a little easier but I still need help (structure). I took it to a another teacher in school (tutor) He couldn't figure it out. And he's a teacher!
I'll let you know when ever I figure this out.. THANKS !
 
This is very tricky and difficult to do if you try to redeem all the coupons at once. The problem doesn't state it clearly enough, but I believe the intention is to redeem only 7 coupons at a time with each repetition, reducing the number of leftover coupons by 7, and increasing the number of bars by 1.

You would start with an input of the number of bars purchased and assign the number of coupons to the same number of bars.

Then you would start your loop and continue as long as the number of coupons is >= 7.

Inside the loop, all you have to do is subtract 7 from the number of coupons remaining, then add 1 to the number of bars, and 1 more to the number of coupons. You could also display the results either inside the loop to keep track of the numbers, or output the final results after the loop ends.
 
Last edited:
In the loop you can just subtarct by six from the coupons rather than subtracting 7 then adding 1 for the new bar.
 
Thanks

You guys make it sound so easy..lol
Once I get the structure of it down, I'll do all right with it. I keep saying the structure is because I got most of how the types of codes works. "Loops", "Case", etc.
Example:
Inside the loop, all you have to do is subtract 7 from the number of coupons remaining,
then add 1 to the number of bars, and 1 more to the number of coupons.

I know exactly what you guys are saying to do, but it's just putting it together.
It's like I have a test this week. I don't have a problem with a test because I can read and understand the codes. It's just putting it down on paper.

Anyone know any sites that has codes to study or practice with?
 
Here is the solution, complete with comments and lots of output so you can clearly see how it works. If you don't want all those output strings to display, then comment out the first Console.WriteLine that tells how many coupons were used and the If-Then block.

I could have simply subtracted 6 instead of subtracting 7 and adding 1, but I wanted it to be perfectly clear where those numbers were coming from.

I also have the solution to the original problem, which redeems all the coupons at once instead of 7 at a time. Let me know if you want to see that version.

If you still need to do it for homework, then just study the code briefly and put it away. Then try to do it your own way. You probably haven't learned how to use TryParse() or ToUpper() as yet.


VB.NET:
	Sub Main()
		Dim bar, coupon As Integer, ans As String
		Do
			Console.Clear()
			Console.WriteLine("Each candy bar contains one coupon.")
			Console.WriteLine("You may redeem 7 coupons to get an extra bar containing another coupon.")
			Console.WriteLine("Continue redeeming coupons till you have less than 7 left.")
			Console.WriteLine()
			Console.Write("Enter number of bars purchased:  ")
			ans = Console.ReadLine
			Integer.TryParse(ans, bar)
			coupon = bar			'one coupon in each bar
			Console.WriteLine()
			Do While coupon >= 7	'Continue as long as at least 7 coupons are available.
				coupon = coupon - 7	'Redeem 7 coupons (reducing the number of leftovers)
				bar = bar + 1		'to get an extra bar.
				Console.WriteLine("You used 7 coupons and have " & coupon & " left, but got an extra coupon with the bar.")
				coupon = coupon + 1	'You also have an extra coupon.
				Console.WriteLine("You now have " & coupon & " coupons and " & bar & " bars.")
				If coupon >= 7 Then
					Console.WriteLine("You may continue redeeming the leftover coupons.")
				End If
				Console.WriteLine()
			Loop
			Console.WriteLine("Not enough coupons left to continue.")
			Console.WriteLine()
			Console.Write("Do you want to start over (Y/N)?  ")
			ans = Console.ReadLine()
		Loop While ans.ToUpper = "Y"
End Sub
 
"wow" thanks

This is the way they should be doing it in school. Study some code as to what you are doing and then work on it. They have us studying on one type and then working on something completly different. I do understand why they do it that way, to get you to think, but until someone knows so much, it makes it even harder.
That's what I'll do, study it some and put it in the way I know how. It's a little more than what we've been doing. The (IF) has to come out. I originaly had that in mine but the instructor said to take it out.

I wouldn't mind see how the other code works:
I also have the solution to the original problem, which redeems all the coupons at once instead of 7 at a time. Let me know if you want to see that version.

I will work on this tonight.. Thanks for all your guy's help !
 
Solution to original problem, redeeming all coupons at once. This still needs to go into a loop, since more coupons have been added after getting the extra bars:


VB.NET:
	Sub Main()
		Dim bar, coupon, extra, used As Integer, ans As String
		Console.Write("Enter number of bars purchased:  ")
		ans = Console.ReadLine
		Integer.TryParse(ans, bar)
		coupon = bar			'one coupon in each bar
		Console.WriteLine()
		Do
			Console.WriteLine()
			extra = coupon \ 7				'# of extra bars you can get with coupons
			used = extra * 7				'# of coupons you need to use
			coupon = coupon - used			'# of leftover coupons
			Console.WriteLine("You will use " & used & " coupons to get " & extra & " more bars.")
			Console.WriteLine("You had " & coupon & " coupons left + " & extra & " more.")
			coupon = coupon + extra			'total # of coupons
			bar = bar + extra				'total # of bars acquired
			Console.WriteLine("You now have " & coupon & " coupons and a total of " & bar & " bars.")
		Loop Until coupon < 7
		Console.ReadLine()
	End Sub
 
Thanks

I'll check this one also. I did an exam. C+ The instructor is going to have to answer a few questions about it, some of them didn't seem right (not for what we've been doing). I need to work on this bonus code. If I get it, it'll shoot me up to a B+..
Thanks Again

I'll let you know how this code comes out later...
 
Back
Top