First time in programming

equizolt

Member
Joined
Feb 5, 2014
Messages
7
Programming Experience
Beginner
Hi I am new to programming; please bear with me :friendly_wink:
I am currently debugging this program for school.
I'm assuming the way it is structured is by calling the method - forexample(i dont know , what to put here)
I just cant understand it, can someone help me get this program working

C#:
// This program uses the Array.BinarySearch method
// to find a target within an array.
// Fix both the logic and syntax errors.

using Systems;

class DebugSeven
{
    static void Main(string[] args)
    {
        // WARNING: The array firstNames cannot be changed
        //  in any way.
        // ------------------------------------------------
        string[] firstNames = { "Johnny", "Alice", "Cory",
                                "Steve", "Dennis" };
        // ------------------------------------------------

        int index;
        string searchedName = PromptName();
        while (searchedName.Equals("end"))
        {
            index = Array.BinarySearch(firstNames, searchedName);
            Display(index, searchedName);
        }

        Console.Read();
    }

    private void Display(int index, string name)
    {
        Console.WriteLine();
        if (index >= 0)
        {
            Console.WriteLine("Yes, {0} is in our course.", searchedName);
        }
        else
        {
            Console.WriteLine("No, {0} is not in our course.", name);
        }
        Console.Write();
    }

    private string PromptName()
    {
        Console.Write("To terminate the program, enter 'end' for the student name.");
        Console.Write("Enter the first name of the student to be searched in our course: ");
        return Console.ReadLine();
    }
}
 
Well, how about you explain what you think the code is trying to achieve in the first place? After that, explain exactly what you do understand of what the code is doing and what parts are causing you an issue. If your teacher thinks that you can do this then you have obviously been taught a few things so if you don't even know how to call a method then I'd suggest that you haven't been paying attention. I'm certainly not saying that I won't help but you've if you're just saying "I have no idea" then you need to go back to square one and get an idea.
 
It is a program where in you input a name and it would tell you if its valid or not by looking up the database. in this program it should be one of the names in the array.
-its a while loop so itll keep asking till you say the sentinel value end
-it'll verify the input by going through an if statement (Display()) if the value inputted is in the array or not

here's what i got so far.
i managed to run it by removing the Display() on the main but it'll stop right after the first input . program will terminate i mean.
obviously thats wrong, but i dont know how to make that display work. i dont know what parameters i should put on Display()

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab4DebugSevenFixed
{
    class DebugSeven
    {
        static void Main(string[] args)
        {
            // WARNING: The array firstNames cannot be changed
            //  in any way.
            // ------------------------------------------------
            string[] firstNames = { "Johnny", "Alice", "Cory",
                                "Steve", "Dennis" };
            // ------------------------------------------------

            int index;
            string searchedName = PromptName();
            while (searchedName.Equals("end"))
            {
                index = Array.BinarySearch(firstNames, searchedName);
                Display();
            }

            Console.Read();
        }

        private void Display(int index, string name, string searchedName)
        {
            Console.WriteLine();
            if (index >= 0)
            {
                Console.WriteLine("Yes, {0} is in our course.", searchedName);
            }
            else
            {
                Console.WriteLine("No, {0} is not in our course.", name);
            }
        }

        private static string PromptName()
        {
            Console.WriteLine("To terminate the program, enter 'end' for the student name.");
            Console.Write("Enter the first name of the student to be searched in our course: ");
            return Console.ReadLine();
        }
    }
}

this wont run and it gives me an error .
NO overloard for method 'Display' takes 0 arguments
 
Forget the rest for the moment. Start with this:
    private string PromptName()
    {
        Console.Write("To terminate the program, enter 'end' for the student name.");
        Console.Write("Enter the first name of the student to be searched in our course: ");
        return Console.ReadLine();
    }
Explain what each line of that is doing.
 
Forget the rest for the moment. Start with this:
    private string PromptName()
    {
        Console.Write("To terminate the program, enter 'end' for the student name.");
        Console.Write("Enter the first name of the student to be searched in our course: ");
        return Console.ReadLine();
    }
Explain what each line of that is doing.

That's the prompt for every loop.
if you run the last code i pasted . that will pop up and let you enter a name. however after you did the program will close
 
That's the prompt for every loop.
if you run the last code i pasted . that will pop up and let you enter a name. however after you did the program will close

I asked you to explain what each line does. One of the reasons that you're having trouble with this is because you're trying to look at the whole thing at once instead of breaking it down. This method has a purpose and can fulfil that purpose in any application so is independent of the rest of the code. As such, you can work on this method in isolation, understand it and make sure that it works as it should, then forget about it when you work on the rest of the code. The same goes for the Display method, i.e. you can work on it in isolation and then, when you know that it is working correctly, forget about it as you then concentrate on the code that makes use of it. This is simple "divide and conquer" and is how you should approach any complex problem, programming or otherwise.

So, if you can't describe what each line of that PromptName method is doing then you can't know whether it is doing what it should. You can't fix logic and syntax errors in the code if that's the case. So, I ask again, what does each line of that code do and does each of those actions make sense?
 
C#:
private string PromptName()

{

    Console.Write("To terminate the program, enter 'end' for the student name."); [B]// its a prompt telling the user the sentinel value to end the program[/B]

    Console.Write("Enter the first name of the student to be searched in our course: "); [B]// its another prompt to ask the user to type in a name[/B]

    return Console.ReadLine(); [B]// I never had a return statement exercise. i know the way where in like ..... studentName = Console.ReadLine()[/B]

}
 
Last edited:
C#:
 private void Display(int index, string name, string searchedName) [B]// I DONT KNOW WHAT THIS FOR[/B] [B]OR WHAT TO WRITE IN IT TO MAKE IT WORK[/B] (DIFFICULTY IN UNDERSTANDING ARGUES AND PARAMETERS)
        {
            Console.WriteLine();
            if (index >= 0) [B]// I DONT EXACTLY UNDERSTAND THIS IF STATEMENT. I MEAN THE WAY IT SHOULD WORK. I ASSUME INDEX ARE THE SUBSCRIPT IN THE ARRAY {0,1,2,3,4} <<<<<<INDEXES?[/B]
            {
                Console.WriteLine("Yes, {0} is in our course.", searchedName); [B]//SOMETHING OFF HERE IDK IF I SHOULD USE SEARCHEDNAME OR NAME[/B]
            }
            else
            {
                Console.WriteLine("No, {0} is not in our course.", name); [B]//SOMETHING OFF HERE IDK IF I SHOULD USE SEARCHEDNAME OR NAME[/B]
 
C#:
private string PromptName()

{

    Console.Write("To terminate the program, enter 'end' for the student name."); [B]// its a prompt telling the user the sentinel value to end the program[/B]

    Console.Write("Enter the first name of the student to be searched in our course: "); [B]// its another prompt to ask the user to type in a name[/B]

    return Console.ReadLine(); [B]// I never had a return statement exercise. i know the way where in like ..... studentName = Console.ReadLine()[/B]

}

There's more to it than just that. The first line writes out an instruction to the user and does not add a line break at the end. That means that the next line is going to be written on the end of the previous one. Does that actually make sense? There's no space on the end of that line or the beginning of the next one so it is not going to look right to the user. I would suggest that it would make sense to write the first prompt on one line and the next on another, which means that the first prompt should be written using WriteLine rather than Write. That's the first of the logic errors that you should have picked up.

It makes sense for the next prompt to be written using Write though. Notice that it has a colon and a space at the end of the line. Whatever the user types in response to the prompt will appear immediately after that, which is quite logical.

The last line is a return statement. If you don't know what that is then you really don't have much of a grasp of the basics. Maybe you haven't been taught what a return statement is but I find that hard to believe. I would suggest that you follow the Tutorial link in my signature below and work your way through that. It should provide all you need to get a good grasp of the basics and equip you to make a decent fist of this assignment. If you're still having issues after that then by all means post back again.
 
oh yea.... thats true, i thought i fixed that already. but those i understand since those I can see if i run the program visual error and common sense. i would learn that by experience; the more i get familiar with it.

but that doesn't fix my problem. i need to know how to use different methods not just by using the MAIN method
 
i need to know how to use different methods not just by using the MAIN method

Calling a method is the same regardless of the method, so what you need to do is learn how to write and call methods. The tutorial link I already pointed out can show you how to do that, as well as various other aspects of C# basics.
 
Back
Top Bottom