javascript
Brief description  about Online courses   join in Online courses
View garima  agrawal 's Profile

How to print user input based on if statement in c#

Can anyone help me to print the user input, like if user enter yes take more input and print that, else if he gives no than take some more info and print that.

But this all i want to print with all other information which i have asked user before the if statement as well.

I know the command string input = Console.Readline(); is the command to store user input.

But when I am trying this to print with Console.Write(input);
Its working for all input except if statement. I do not know why. plz help.

Thanks
Garima
Asked by garima agrawal | Apr 23, 2012 |  Reply now
Replies (4)
View  's Profile
See the code given in the last: First thing is that age is defined as a string not as a numeric, also you have entered in the if box to check for student or not but "y" is different from "Y", have you tried to Convert.ToInt32 and than asking 1 for student and 2 for else :), it may feel some code bloat , but it is a good version of assignment given above,

using System;
using System.Collections;
using System.Linq;
using System.Text;
// Assignment Submitted By Kanhiya Deswal, Student

// ver. 0.0.0.1

namespace assignment
{
class Program
{
static void Main(string[] args)
{


Console.WriteLine("Enter Your Name");// it will display message on the console
string name = Console.ReadLine(); // this will read from console

Console.WriteLine("Enter your Address");
string address = Console.ReadLine();

Console.WriteLine("Enter Your age");
int age = Convert.ToInt32(Console.ReadLine()); // change of variable from string to integer

Console.WriteLine("Are You a student, enter 1 for student else 2 if you are not a student");
int choice = Convert.ToInt32(Console.ReadLine());

if (choice == 1)
{
Console.WriteLine("Enter No. Of subjects you have");
int Subjects = Convert.ToInt32(Console.ReadLine());


string[] subjectname = new string[Subjects]; // array declaration with length equal to no. of subjects entered
Console.WriteLine("Enter Your subjects name");
for (int i = 0; i < Subjects; i++)
{
subjectname[i] = Console.ReadLine();
}

Console.WriteLine("The Total data You have entered is");// print of information captured on console
Console.WriteLine("Name:");
Console.WriteLine(name);
Console.WriteLine("Address:");
Console.WriteLine(address);
Console.WriteLine("Age:");
Console.WriteLine(age);
Console.WriteLine("Subjects:");

for (int i = 0; i < Subjects; i++)
{
Console.WriteLine(subjectname[i]);
}


}
else // in case person is not a student According to assignment
{
Console.WriteLine("Enter Your 3 Hobbies");
string[] hobby = new string[3];

for (int j = 0; j < 3; j++)
{
Console.WriteLine("Enter Your hobbie");
hobby[j] = Console.ReadLine();
}
Console.WriteLine("Total data you have entered");// print of data captured so far
Console.WriteLine("Name:");
Console.WriteLine(name);
Console.WriteLine("Address:");
Console.WriteLine(address);
Console.WriteLine("Age:");
Console.WriteLine(age);
Console.WriteLine ("Hobbies:");
for (int j=0; j<3 ; j++ )
{
Console.WriteLine(hobby[j]);
}

}















}
}
}

Jul 5, 2012
View kishan chakravarthy talasila 's Profile
Try this....
console.writeline("the text you have entered is {0}", the variable you are using)
May 15, 2012
View garima agrawal 's Profile
yes I tried, look at my code plz and do changes if you can do it..


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

namespace personal_info
{
class Program
{
public static void Main(string[] args)
{
string subjects;
string hobbies;

Console.WriteLine("Please enter your name");
string p_name = Console.ReadLine();

Console.WriteLine("Please enter your age");
string age = Console.ReadLine();

Console.WriteLine("Please enter your address");
string p_add = Console.ReadLine();

Console.WriteLine("Are you a student? press Y for yes or N for no");
string student = Console.ReadLine();

if (student == "y")
{

Console.WriteLine("Please Enter Your subjects with coma");
subjects = Console.ReadLine();
Console.WriteLine("Please confirm the below information, which you have entered");
Console.WriteLine("Your subjects are {0}", subjects);


}
else
{
Console.WriteLine("Please enter your 3 hobbies with comma");
hobbies = Console.ReadLine();
Console.WriteLine("Please confirm the below information, which you have entered");
Console.WriteLine("Your hobbies are {0}", hobbies);


}


Console.WriteLine("Your name is {0}", p_name );
Console.WriteLine("Your age is {0}", age);
Console.WriteLine("Your address is {0}", p_add);
Console.WriteLine("Are you a student? {0}", student);






}

}
}
May 4, 2012
View dan plazo 's Profile
i am just learning- but did you try something like Console.Write( "{0}, input);
thank you,
dan
May 2, 2012