3 Replies - 669 Views - Last Post: 26 January 2012 - 08:05 AM Rate Topic: -----

#1 jim01  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 26-January 12

Passing a method containing user input to Main(), send to new method

Posted 26 January 2012 - 07:33 AM

I have created a method named FavoriteSayings()that asks the user to input their favorite saying. I call that saying to the main menu. I am supposed to send the user input from FavoriteSayings() to a new method I have created named PassUserInput() and then display that input surrounded by asterisks.

The problem I am having is that I cannot figure out how to pass just the user input so that the request for user input is not asked twice. I think I have to somehow use the ref or out parameters but the book is pretty vague on how they are used and the example it gives does not help me understand it at all.

Here is the code I have:


using System;

namespace Exercise3_2
{
    class Exercise3_2
    {
        static void Main(string[] args)
        {
            
            FavoriteSayings(); //call FavoriteSayings() method to the Main() method
            PassUserInput();  //call PassUserInput to display user message
        }
              
        //create method requesting user to input favorite saying
        public static void FavoriteSayings()
        {
            Console.WriteLine("Please input your favorite saying: ");
            Console.ReadLine();
            return;
        }

        public static void PassUserInput()
        {
            Console.WriteLine("******************");
            FavoriteSayings();
            Console.WriteLine("******************");
             
            return;
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Passing a method containing user input to Main(), send to new method

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Passing a method containing user input to Main(), send to new method

Posted 26 January 2012 - 07:38 AM

Reminder to new folks: This is homework and we don't provide completed code for homework. Rather we try to help the student comprehend the concept they are struggling with so they can write their own code. They are being graded on their skill not ours.

All of your methods currently return void.

Just change the input method to return the string the user entered, then use that returned value.

for example:

string yogobear = someMethod();

string someMethod()
{
   return "This is a string";
}


Now that you have an example of how a method can return a value and how you can receive that returned value, you should be able to make use of the concept in your homework.


Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.
Was This Post Helpful? 1
  • +
  • -

#3 jim01  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 26-January 12

Re: Passing a method containing user input to Main(), send to new method

Posted 26 January 2012 - 07:56 AM

Thank you for your help and your suggestions. I will take them and figure out where I am going wrong. My apologies if I made it appear as if I was trying to get you to do my homework. That was not my intention. I read the rules and thought I presented my question so that it would not appear that way. Obviously I was wrong. I plan on being a software developer in the future and having other people do my work for me will not help me be successful in that goal.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Passing a method containing user input to Main(), send to new method

Posted 26 January 2012 - 08:05 AM

Relax: You weren't wrong nor did I think you were asking for someone to do you homework for you. Don't read anything into that comment that wasn't really there. It was no more than as stated: A note to newer members.

Often times our newest members are very anxious to help and get themselves actively participating, which we love. But being new they don't always know that we don't provide complete code solutions for homework. YOU read the rules but not everyone does.

Quote

I plan on being a software developer in the future and having other people do my work for me will not help me be successful in that goal.


That is EXACTLY the mindset we like and try to promote. Very happy to have you join us!
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1