SolvedHelp me with simple code

Posts 15 of 5 · Page 1 of 1
Help me with simple code
I am learning c# and i attempted to do a console application so i can set my first name, second name and age and when i say " my information " i would like for it to display my information but i am having problem with my If statement and creating and also having a problem creating a variable.

Errors
For if statement : No overload for method 'ReadLine' takes 1 argument.
For variable: Cannot implicitly convert type 'void' to 'string'.

Code of Errors

If statement: if (Console.ReadLine("my information"))
{
Console.WriteLine(Info);
}

Variable: string Info = Console.WriteLine ("Your Name is: " + Fname + " " + Sname + " And your Age is: " + Age);



Full code


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Fname;
            string Sname;
            int Age;
            string Info = Console.WriteLine ("Your Name is: " + Fname + " " + Sname + " And your Age is: " + Age);
            string lol = Console.ReadLine();

            
            Console.WriteLine("What is your Name?");
            Console.Write("My First Name is: ");
   Fname = (Console.ReadLine());
            Console.WriteLine("What is your Second Name?");
            Console.Write("My Second Name is: ");
   Sname = (Console.ReadLine());
            Console.WriteLine("What is your Age?");
            Console.Write("My Age is: ");
      Age = Convert.ToInt32 (Console.ReadLine());
            
            if (Console.ReadLine("my information"))
            {
                Console.WriteLine(Info);
            
            }
        }
    }
}
Edit: I have fixed one of the error, still having the error for the variable.

Updated code with same variable error

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Fname;
            string Sname;
            int Age;
            string Info;
            string lol = Console.ReadLine();

            
            Console.WriteLine("What is your Name?");
            Console.Write("My First Name is: ");
   Fname = (Console.ReadLine());
            Console.WriteLine("What is your Second Name?");
            Console.Write("My Second Name is: ");
   Sname = (Console.ReadLine());
            Console.WriteLine("What is your Age?");
            Console.Write("My Age is: ");
      Age = Convert.ToInt32 (Console.ReadLine());
     Info = (Console.WriteLine ("Your Name is: " + Fname + " " + Sname + " And your Age is: " + Age));
            
                if (lol == ("my information"))
                {
                    Console.WriteLine(Info);
                }
        }
    }
}
Hmm, I'll try to update the code a bit, I'm not working with consoles at all, but I think this should do the trick:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string FName = "";
            string SName = "";
            int Age = 0;
            Console.WriteLine("Hello, please fill out your Name & Age! :)");
            Console.WriteLine("What is your first name?");
            FName = Console.ReadLine();
            Console.WriteLine("Ok, " + FName + " now your surname please!"); 
            SName = Console.ReadLine();
            Console.WriteLine("Great work so far! So far we know that your name is " + FName + " " + SName + ".");
            Console.WriteLine("Ok, final step, how old are you?");
            int.TryParse(Console.ReadLine(), out Age); //convert string > int
            Console.WriteLine("Ok, lets verify it! :)");
            if (FName != "" && SName != "" && Age != 0)
            {
                  Console.WriteLine("Nice, you did it!"); /// do something else...
            }
            Console.Read(); //prevent it from closing
    }
}
This really should do the trick, wrote the code directly to the post, not in Visual Studio, so there might be some typo or something, but it should give you the idea on how it could look like. Of course it could be done better like checking if username was entered right on the step when it asks for it instead of checking all of the values at the end of process, but thats up to you on how you want it to work.



Have a nice day,
UniveX
Quote Originally Posted by Yoddo View Post
I am learning c# and i attempted to do a console application so i can set my first name, second name and age and when i say " my information " i would like for it to display my information but i am having problem with my If statement and creating and also having a problem creating a variable.

Errors
For if statement : No overload for method 'ReadLine' takes 1 argument.
For variable: Cannot implicitly convert type 'void' to 'string'.

Code of Errors

If statement: if (Console.ReadLine("my information"))
{
Console.WriteLine(Info);
}

Variable: string Info = Console.WriteLine ("Your Name is: " + Fname + " " + Sname + " And your Age is: " + Age);



Full code


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Fname;
            string Sname;
            int Age;
            string Info = Console.WriteLine ("Your Name is: " + Fname + " " + Sname + " And your Age is: " + Age);
            string lol = Console.ReadLine();

            
            Console.WriteLine("What is your Name?");
            Console.Write("My First Name is: ");
   Fname = (Console.ReadLine());
            Console.WriteLine("What is your Second Name?");
            Console.Write("My Second Name is: ");
   Sname = (Console.ReadLine());
            Console.WriteLine("What is your Age?");
            Console.Write("My Age is: ");
      Age = Convert.ToInt32 (Console.ReadLine());
            
            if (Console.ReadLine("my information"))
            {
                Console.WriteLine(Info);
            
            }
        }
    }
}
Edit: I have fixed one of the error, still having the error for the variable.

Updated code with same variable error

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Fname;
            string Sname;
            int Age;
            string Info;
            string lol = Console.ReadLine();

            
            Console.WriteLine("What is your Name?");
            Console.Write("My First Name is: ");
   Fname = (Console.ReadLine());
            Console.WriteLine("What is your Second Name?");
            Console.Write("My Second Name is: ");
   Sname = (Console.ReadLine());
            Console.WriteLine("What is your Age?");
            Console.Write("My Age is: ");
      Age = Convert.ToInt32 (Console.ReadLine());
     Info = (Console.WriteLine ("Your Name is: " + Fname + " " + Sname + " And your Age is: " + Age));
            
                if (lol == ("my information"))
                {
                    Console.WriteLine(Info);
                }
        }
    }
}
What are you trying to do?...
Try this:
Code:
 static string Fname;
        static string Sname;
        static int age;

        static void Main(string[] args)
        {
            Console.WriteLine("What is your name?");
            Fname = Console.ReadLine();
            Console.WriteLine("What is your second name?");
            Sname = Console.ReadLine();
            Console.WriteLine("What is your age?");
            string a = Console.ReadLine();
            if (IsNumeric(a)) // The routine IsNumeric returns a boolean value
            {
                age = Convert.ToInt32(a);
            }
            else
            {
                // Not numeric, do something...
            }

            Console.WriteLine("Do you want to calculate result? (y/n)");
            string reply = Console.ReadLine();
            if (reply.ToLower() == "y")
            {
                Console.WriteLine("Your name is: " + Fname + Environment.NewLine + "Your second name is: " + Sname + Environment.NewLine + "Your age is: " + age);
                Console.ReadLine();
            }

        }
        static bool IsNumeric(this string s)
        {
            float output;
            return float.TryParse(s, out output);
        }
@Raydenman and @univex thanks for replying but you both had the wrong idea of what i wanted, ty for trying to understand me

I have fixed the problem, and this is what i wanted to achieve, here is the full code.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Fname;
            string Sname;
            int Age;
            string Info;
            string lol;


            Console.WriteLine("What is your Name?");
            Console.Write("My First Name is: ");
            Fname = (Console.ReadLine());
            Console.WriteLine("What is your Second Name?");
            Console.Write("My Second Name is: ");
            Sname = (Console.ReadLine());
            Console.WriteLine("What is your Age?");
            Console.Write("My Age is: ");
            Age = Convert.ToInt32(Console.ReadLine());
            Info="Your Name is: " + Fname + " " + Sname + " And your Age is: " + Age;
            Console.WriteLine("If you would like to check your information, type my information.");

            lol = Console.ReadLine();
            if (lol == ("my information"))
            {
                Console.WriteLine(Info);
            }
            Console.ReadLine();
        }
    }
}
Quote Originally Posted by Yoddo View Post
@Raydenman and @univex thanks for replying but you both had the wrong idea of what i wanted, ty for trying to understand me

I have fixed the problem, and this is what i wanted to achieve, here is the full code.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Fname;
            string Sname;
            int Age;
            string Info;
            string lol;


            Console.WriteLine("What is your Name?");
            Console.Write("My First Name is: ");
            Fname = (Console.ReadLine());
            Console.WriteLine("What is your Second Name?");
            Console.Write("My Second Name is: ");
            Sname = (Console.ReadLine());
            Console.WriteLine("What is your Age?");
            Console.Write("My Age is: ");
            Age = Convert.ToInt32(Console.ReadLine());
            Info="Your Name is: " + Fname + " " + Sname + " And your Age is: " + Age;
            Console.WriteLine("If you would like to check your information, type my information.");

            lol = Console.ReadLine();
            if (lol == ("my information"))
            {
                Console.WriteLine(Info);
            }
            Console.ReadLine();
        }
    }
}
Use the button ' Thanks ' next time
Posts 15 of 5 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?