-1

I have a problem with my class where it displays a comma before it displays the string and I cant find a way to take the comma out in the front and keep it in all the other places

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Carnival
    {
        class Player
        {
            public string Name { get; set; }
            public double SpendingMoney { get; set; }
            public string PrizesWon { get; set; }


            //constructors
            public Player( string n, double sp)
            {
                Name = n;
                SpendingMoney = sp;
                PrizesWon = "";

            }
            //methods

                public string Play(GameBooth aGames)
            {
                string newPrize;
                if (SpendingMoney >= aGames.Cost)
                {
                    newPrize = aGames.Start();
                    //here is what I need to fix                    
                    PrizesWon =  "," + newPrize  + PrizesWon ;
                    return newPrize;
                     }
                else
                {
                    return "error no money fool";
                }
            }

        }
    }

here is the main code if you need it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carnival
{
    class Program
    {
        static void Main(string[] args)
        {
            //local variables
            string name;
            double money;
            int choice = 0;
            string newPrize;

            //Game objects.
            GameBooth balloonDartToss = new GameBooth(2, "tiger         plush", "sticker");
            GameBooth ringToss = new GameBooth(2, "bear keychain", "pencil");
            GameBooth breakAPlate = new GameBooth(1.5, "pig plush", "plastic dinosaur");

            Console.ForegroundColor = ConsoleColor.Cyan;

            //asking player name
            Console.Write("Welcome to the Carnival. What is your name? ");
            name = Console.ReadLine();

            //asking how much spending money the player has
            Console.Write("How much money do you have? ");
            money = Convert.ToDouble(Console.ReadLine());
            Console.ResetColor();

            //Creating player object.
            Player Gambler = new Player(name, money);

            //main program loop
            while (choice != 4)
            {
                //print menu. Check out the local method below
                printMenu();

                //retrieve user choice. See the local method below
                choice = getChoice();

                //main switch. User Choices:
                //  1 - we play Baloon Darts Toss and show prize
                //  2 - we play ring Toss and show prize
                //  3 - we play Break a Plate and show prize
                //  4 - Show users all his prizes
                switch (choice)
                {
                    case 1:
                        newPrize = Gambler.Play(balloonDartToss);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(newPrize);
                        break;
                    case 2:
                        newPrize = Gambler.Play(ringToss);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(newPrize);
                        break;
                    case 3:
                        newPrize = Gambler.Play(breakAPlate);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(newPrize);
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("{0}, here is your prize list: {1}", Gambler.Name, Gambler.PrizesWon);
                        Console.ResetColor();
                        break;
                }//end switch
            }//end while

            //next line simply pauses the program until you hit Enter.
            Console.ReadLine();
        }//end main

        //this method prints the main menu
        public static void printMenu()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine();
            Console.WriteLine("Select the game you would like to play:");
            Console.WriteLine("1. Balloon Dart Toss");
            Console.WriteLine("2. Ring Toss");
            Console.WriteLine("3. Break a Plate");
            Console.WriteLine("4. Get Prizes and Leave Carnival");
            Console.Write("Enter Your Choice: ");
            Console.ResetColor();
        }//end printMenu

        //this methods accepts user input 1-4
        public static int getChoice()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            int input = 0;
            do
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                input = Convert.ToInt32(Console.ReadLine());
                if (input < 1 || input > 4)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Out of range. Input 1-4 only");
                    Console.Write("Enter your choice: ");
                    Console.ResetColor();
                }
            } while (input < 1 || input > 4);
            Console.ResetColor();
            return input;
        }
    }
}

here is my other class if you need that too but you probably don't

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carnival
{
    class GameBooth
    {
        //properties
        public double Cost { get; set; }
        public string FirstPrize { get; set; }
        public string ConsolationPrize { get; set; }
        public int w { get; set; }
        public int l { get; set; }
        //constructors
        public GameBooth(double c, string fp, string cp)
        {
            Cost = c;
            FirstPrize = fp;
            ConsolationPrize = cp;

        }

        //methods
        public string Start()
        {
            Random r = new Random();
            w = 1;
            l = 1;
            for (int i = 1; i < 3; i++)
            {
               int randomChoice = r.Next(0, 400);
                if ( randomChoice == 1)
                {
                     w++;
                }

            }
            if (w == 3)
            {
                return FirstPrize;
            }
            else
            {
                return ConsolationPrize;
            }
        }
    }
}
Morgan
  • 115
  • 8
  • 1
    which string are you talking about? Can you narrow down your question a bit more to where you're experiencing the problem? – sous2817 Jan 27 '17 at 21:59
  • if (PrizesWon.startswith(',') ) PrizesWon=PrizesWon.substrng(1);... Off top of my head, but should get you there. – Trey Jan 27 '17 at 21:59
  • That is a lot of code to go through. –  Jan 27 '17 at 22:09
  • 1
    Please read the [MCVE] guidance on posting the code. Most likely if you implement things correctly (like list of prizes instead of stringly-typed code) http://stackoverflow.com/questions/14959824/convert-list-into-comma-separated-string is the (duplicate) answer. – Alexei Levenkov Jan 27 '17 at 22:12
  • string.Join(list of string, ",") => something like this – Laurent Lequenne Jan 27 '17 at 22:23

2 Answers2

1

Update the line to:

PrizesWon = string.IsNullOrEmpty(PrizesWon) ? newPrize : newPrize + "," + PrizesWon;

This uses the conditional operator to return a different result depending on whether or not PrizesWon already has something in it.

Jon Davies
  • 487
  • 4
  • 11
  • how does this help? can you explain it? – Morgan Jan 27 '17 at 22:54
  • 1
    The condition `string.IsNullOrEmpty(PrizesWon)` returns **true** when **PrizesWon** is empty - if so **PrizesWon** is set to `newPrize` (the expression between the `?` and `:`) otherwise the condition returns **false** and **PrizesWon** is set to `newPrize + "," + PrizesWon` (the expression after the `:`). In other words - the first time you add a prize you don't add a comma; for each subsequent prize you do. – Jon Davies Jan 27 '17 at 23:00
0

In your code you had put (,) before newPrize. For solving this, you have to just put it after PrizesWon variable and it will look like this :

**PrizesWon =  newPrize + PrizesWon + ",";**

I hope your problem will be solved by this.

Pedo
  • 194
  • 1
  • 2
  • 14