True or False? Step 13, store the user’s boolean answer in responses and increment askingIndex by 1

Hello,

Cannot really get further with this, because cannot find how to store users input in that array responses.

Have added code and commented the place which is not correct. Have googled this, did not find anything reasonable, please assist!

using System;

namespace TrueOrFalse
{
class Program
{
static void Main(string args)
{
Console.WriteLine(“Welcome to ‘True or False?’\nPress Enter to begin:”);
string entry = Console.ReadLine();
//.SetUpInputStream(entry);

        string[] questions = { "Is Aubameyang the best forward for Arsenal now?",
            "Is Arsenals home ground called the Emirates?", "Can you do 10 strict muscleUps?" };

        bool[] answers = { true, true, false };
        bool[] responses = new bool[questions.Length];

        if (questions.Length != answers.Length)
        {
            Console.WriteLine("Error, question amount must match answer amount");
        }

        int askingIndex = 0;

        foreach (string looping in questions)
        {

            Console.WriteLine(looping);
            Console.WriteLine("True or false?");
            string input = Console.ReadLine();
            bool isBool;
            bool inputBool;

            isBool = Boolean.TryParse(input, out inputBool);

            

            while (isBool != true)
            {
                Console.WriteLine("Please respond with 'true' or 'false'.");
                input = Console.ReadLine();
                
                isBool = Boolean.TryParse(input, out inputBool);
            }
            // all is fine till here, step 13, store users input in responses array
            responses[input] = isBool; 
            askingIndex++;
        }
        foreach (bool temp in responses)
        {
            Console.WriteLine(temp);
        }
    }
}

}

What is the value of input, and is that a valid value to pass as an index to an array?

1 Like

input is true or false and need to store those values in responses array so I could later compare them as it says in the task. I just do not know how to write it correctly so it actually is stored.
Otherwise, if I run the code, last foreach loop for testing purposes prints out False False False instead of what I have typed.

Hello, @viesisvalmiera. Welcome to the forum.
As @thepitycoder pointed out, it appears your issue is right here:

You assigned an index variable, but you aren’t using it:

Also, in the same assignment to you responses array:

You aren’t using the variable that the user’s answer was assigned to. You are using the variable that contains the returned value of testing whether the user’s answer can be parsed into a bool. It will always be ‘True’ at this point. I believe inputBool is the variable containing the user’s parsed answer:

One last tip:

while (isbool != true)

Is the same as:

while(!isbool)

Happy Coding!

2 Likes

Hi, I am learning as well. I know there has got to be a better way, however this will work.

This will answer question 13. This will store the users input in an array.

I did this.

bool responses = new bool[questions.Length];

    int i = 0;
    responses[i]=inputBool;
    i++;
1 Like

I think that is pretty much what the exercise expected you to do. :+1:

1 Like

I know this works, because I can test it like this:

int i = 0;
responses[i]=inputBool;
i++;
Console.WriteLine(responses[0]);
break;

However, on line 14. It wants you to print the results outside of the 2 main loops.

So I created a simple for loop outside of the 2 main loops, to print the array out, this is the logic:

 Console.WriteLine("The users Input");
 Console.WriteLine("===============");
 for (int i = 0; i <responses.Length; i++)
 {
    Console.WriteLine(responses[0]);
 }

However, I get a console error:

Program.cs(69,27): error CS0103: The name ‘responses’ does not exist in the current context
[/home/ccuser/workspace/csharp-true-or-false/TrueOrFalse.csproj]

This almost reads like the array ‘responses’ is out of scope?? Not sure how, I am still in the main method?

Again, I am learning too. It probably is me, hmmmmm

Would need to see all of your code to determine the scope of your responses array, but that is what the error is telling you. Also, here: Console.WriteLine(responses[0]); you’ll want to change your index :wink:

2 Likes

You are right, I should do

Console.WriteLine(responses[i]);

Thank you!

1 Like

Thank you for looking.

using System;

namespace TrueOrFalse
{
class Program
{
static void Main(string args)
{
// Do not edit these lines
Console.WriteLine(“Welcome to ‘True or False?’\nPress any key to begin:”);
string entry = Console.ReadLine();
Tools.SetUpInputStream(entry);

  // Type your code below
  
   // Question Array
 string [] questions = new string[] {"Is it cold outside", "Is a Mustang built by Ford", "Is the sky blue"};
  
  // Answers Array
 bool [] answers = new bool[] {false, true, true};
  
  //Check to see that both arrays have the same number of elements
  
  if (questions.Length != answers.Length)
   {
    Console.WriteLine("Warming : The question and answer arrays are not equal");
    }
  
   int askingIndex = 0;
  
   // use a foreach loop to go through each item in the array
  
  foreach (string question in questions)
  {
    string input;
    bool isBool;
    bool inputBool;
    Console.WriteLine(question);
    Console.WriteLine("True or False?");
    input = Console.ReadLine();
    
    isBool = Boolean.TryParse(input, out inputBool);
    
    while (isBool != true)
    {
      Console.WriteLine("Please respond with a 'true or 'false'.");
      input = Console.ReadLine();
      isBool = Boolean.TryParse(input, out inputBool);
      
    }
    
    bool [] responses = new bool[questions.Length];
    
   
    int i = 0;
    responses[i]=inputBool;
    i++;
  //  Console.WriteLine(responses[0]);
  // break;
           
  }
 
  
 Console.WriteLine("The users Input");
 Console.WriteLine("===============");
 for (int i = 0; i <responses.Length; i++)
 {
    Console.WriteLine(responses[0]);
 }
  
}

}
}

I see a few issues.

First,

Your responses array should be declared before the for loop not inside of it.
You don’t actually need int i = 0;. You are using i instead of the intended index variable declared here:

If you’d rather use i that’s perfectly fine, but it needs to be declared outside of the for loop as well in place of askingIndex.

Because responses is declared inside the for loop it is limited in scope to the for loop. It should be declared right after your answers array.

This:

is in the correct place, but will need to match the index variable declaration whether you change askingIndex to i or change i++ to askingIndex++.

Implement these changes, and we’ll see what we have.

Edit:
Aside, these lines located inside the for loop:

    int i = 0;
    responses[i]=inputBool;
    i++;

will result in only the last response being stored. You reset i to 0 each time the loop iterates, so each response is written at the same index of 0. Making the corrections noted above will correct this behavior.

2 Likes

Thank you very much for taking the time to help. As a new programmer, you pound your head against the wall at times, trying to figure out why it will not run. I appreciate your time! God bless

1 Like

You’re very welcome. Happy to help!

1 Like