CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Threaded View

  1. #1
    Join Date
    Jun 2011
    Posts
    33

    [Resolved] Cannot implicitly convert type "string" to "int"

    Since I'm still fairly new to C#, I decided to go back to the basics.

    Here I have a fairly simple problem with a basic number game I'm programming in C#. Basically, the game will generate a random number and if the user guesses the correct number, it will state that the user has won. Otherwise, it tells the user that it must guess again and it restarts the program.

    This is the source code so far. I have just one bug and it's the classic "cannot implicitly convert type "string" to "int" bug. "Guess" is a string, but the program has to decide whether it equals the randomly generated number. I tried implementing a ConvertToInt32 statement but it did nothing. Can anybody help me with this?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int number;
                string numberstring;
                string guess;
                Random rand;
                rand = new Random();
    
                number = rand.Next(1, 3);
    
            Start:
                do
                {
                    Console.WriteLine("Guess the number from 1 to 3.");
                    guess = Console.ReadLine();
    
                    if (guess = number)
                    {
                        Console.WriteLine("You've won.");
                    }
    
                    else
                    {
                        Console.WriteLine("Sorry, guess again.");
                        goto Start;
                    }
                } while (guess != "Q");
            }
        }
    }
    Last edited by Krunchyman; December 7th, 2011 at 10:34 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured