8 Replies - 269 Views - Last Post: 25 January 2013 - 12:05 AM Rate Topic: -----

#1 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Error with input, very basic. Words vs numbers.

Posted 24 January 2013 - 11:44 PM

Hi, im brand new to C#, as of today actually. Im learning it in a class and im struggling. I will post my code but i need to know how to make it so it accepts word input not just number input. As of now the program only responds to the questions i have in there if i put numbers in. Please help, thank you.

static void Main()
    {
        //Declare Variables
        double name, course, age, value;
        //This program will....
        Console.WriteLine("This program will....");
        //Have user put enter their full name.
        Console.WriteLine("What is your full name?");
        name = double.Parse(Console.ReadLine());
        //Have user put enter their course and section.
        Console.WriteLine("What is your course and section?");
        course = double.Parse(Console.ReadLine());
        //Have user put enter their age.
        Console.WriteLine("What is your age?");
        age = double.Parse(Console.ReadLine());
        //Have user put enter how much money they have.
        Console.WriteLine("How much money do you have?");
        value = double.Parse(Console.ReadLine());
        //Display all the information
        Console.WriteLine("Thank you {0}", name);
        Console.WriteLine("In {0}", course);
        Console.WriteLine("Your age is {0}", age);
        Console.WriteLine("And you have {0:C}", value);" 


Also i DO have the ConsoleReadLine(); at the bottom, forgot to put that in.

Is This A Good Question/Topic? 0
  • +

Replies To: Error with input, very basic. Words vs numbers.

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Error with input, very basic. Words vs numbers.

Posted 24 January 2013 - 11:45 PM

Well, that is because you are using double.parse() function for your strings which only require string values.

You need to remove the double.parse() for your name and course inputs.

And of course, you need to declare name and course as string.

regards,
Raghav

This post has been edited by raghav.naganathan: 24 January 2013 - 11:47 PM

Was This Post Helpful? 1
  • +
  • -

#3 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Error with input, very basic. Words vs numbers.

Posted 24 January 2013 - 11:47 PM

And what do i put in its place? Or do i simply delete it?
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Error with input, very basic. Words vs numbers.

Posted 24 January 2013 - 11:50 PM

Well, you could do like this.

String name = Console.ReadLine(); // This will read string from console


similarly for your course.

regards,
Raghav

This post has been edited by raghav.naganathan: 24 January 2013 - 11:51 PM

Was This Post Helpful? 1
  • +
  • -

#5 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Error with input, very basic. Words vs numbers.

Posted 24 January 2013 - 11:52 PM

Strings, i havent encountered that yet. How would i enter the 2 of those as strings?

Ok, i will give that a shot right now.
Was This Post Helpful? 0
  • +
  • -

#6 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Error with input, very basic. Words vs numbers.

Posted 24 January 2013 - 11:57 PM

Alright. So i put the strings in (correctly i hope). Then i went to delete the double.Parse on the 2 and when i ran the program it was empty. Is there something i need to put in its place?
class Program
{
    static void Main()
    {
        //Declare Variables
        double age, value;
        string name = Console.ReadLine();
        string course = Console.ReadLine();
        //This program will....
        Console.WriteLine("This program will....");
        //Have user put enter their full name.
        Console.WriteLine("What is your full name?");
        name = double.Parse(Console.ReadLine());
        //Have user put enter their course and section.
        Console.WriteLine("What is your course and section?");
        course = double.Parse(Console.ReadLine());
        //Have user put enter their age.
        Console.WriteLine("What is your age?");
        age = double.Parse(Console.ReadLine());
        //Have user put enter how much money they have.
        Console.WriteLine("How much money do you have?");
        value = double.Parse(Console.ReadLine());
        //Display all the information
        Console.WriteLine("Thank you {0}", name);
        Console.WriteLine("In {0}", course);
        Console.WriteLine("Your age is {0}", age);
        Console.WriteLine("And you have {0:C}", value);


        Console.ReadLine();

Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Error with input, very basic. Words vs numbers.

Posted 24 January 2013 - 11:59 PM

Well, not really...here are the changes you need to make.

Replace line 13 by line 7 and line 16 by line 8

That should work. :)

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#8 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Error with input, very basic. Words vs numbers.

Posted 25 January 2013 - 12:04 AM

fantastic, works like a charm! Thank you so much for your help!
Was This Post Helpful? 0
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Error with input, very basic. Words vs numbers.

Posted 25 January 2013 - 12:05 AM

Most welcome :) Glad to help :)

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1