0

I'm trying to build a program that lets the user choose between 3 options.

  1. Seeing how many coins they have.
  2. Adding coins to their collection.
  3. Taking coins from their collection.

I made a while loop with the menu first. After that I made a switch statement with different cases depending on what the user chose. If the user chooses option nr 2 and adds for example 3 coins. It is saved in collection for the next loop. But if the user wants to add more coins again. Then the value of the collection resets. I want the user to be able to add more coins to their collection everytime the while loop runs. So the user first can add 2 coins, then choose to see the menu again, then choose to add 2 more coins, then choose to see the menu again, then if the user wants to see the collection, it should be 4 coins. Thanks

float add, take, collection;

collection = 0;

bool on = true;

while (on)
{
    Console.WriteLine("See collection press 1");
    Console.WriteLine("Add to collection press 2");
    Console.WriteLine("Take from collection press 3");

    string choice = Console.ReadLine();
    
    switch (choice)
    {
        case "1":
            Console.WriteLine("You have " + collection + " coins");
            Console.Write("Want to see the menu again? Press K ");
            string str = Console.ReadLine();
            if (str != "K")
            {
                on = false;
            }
            break;

        case "2":
            Console.Write("How many coins do you want to add?: ");
            string str1 = Console.ReadLine();
            add = float.Parse(str1);
            collection =+ add;
            Console.WriteLine("You now have " + collection + " in your collection");
            Console.Write("Want to see the menu again? Press K"); 
            string str2 = Console.ReadLine();
            if (str2 != "K")
            {
                on = false;
            }
            break;

        case "3":
            Console.Write("How many coins do you want to take?: ");
            string str3 = Console.ReadLine();
            take = float.Parse(str3);
            collection =- take;
            Console.WriteLine("You now have " + collection + " in your collection");
            Console.Write("Want to see the menu again? Press K");
            string str4 = Console.ReadLine();
            if (str4 != "K")
            {
                on = false;
            }
            break;
    }

}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • What exactly you need help with? Please re-read https://stackoverflow.com/help/how-to-ask (pay special attention to the [mre] guidance on posting code) and [edit] the question to clarify what you have problem with, what is expected/actual output or maybe compile/runtime errors you observe . – Alexei Levenkov Oct 31 '22 at 20:10
  • while is does not solve your issue which I don't understand what your question is. PLEASE don't use a type of float -> https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate – Scott Mildenberger Oct 31 '22 at 20:17
  • I edited the question now. I'm not sure how to explain it better than that. – 200cansofcatfood Oct 31 '22 at 20:28

1 Answers1

1

This:

collection =+ add;

Should be this:

collection += add;

Otherwise, =+ is two separate operators (assignment and positive), so you are assigning the positive value of add to collection, completely overwriting what was there before.

Same with the "take" operation:

collection =- take;

should be:

collection -= take;

Otherwise, you are assigning the negation of take to collection, overwriting what there before.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794