C
C#9mo ago
9.h

for loop casuing error

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ConsoleApp14

{

    internal class Program

    {

        static void Main(string[] args)

        {

            bool valid = false;

            while (!valid)

            {

                Console.WriteLine("Enter a string: ");              

                string sentence = Console.ReadLine();

                int length = sentence.Length;

                char[] sentence2 = sentence.ToCharArray();

                bool isupper = true;

                bool notrepeating = true;

                int valueascii = 0;

                for (int i = 0; i < length; i++)

                {

                    char b = sentence2[i];

                    if (Char.IsUpper(b) == false)

                    {

                        isupper = false;

                    }

                    valueascii = valueascii + Convert.ToInt32(b);

                }

                Array.Sort(sentence2);

                for (int i = 0; i < length; i++)

                {


                    if (sentence2[i] == sentence2[i++])

                    {

                        notrepeating = false;

                    }

                }

                Console.WriteLine(sentence2);

                if (length > 4 && length < 8 && isupper && valueascii > 419 && valueascii < 601 && notrepeating)

                {

                    Console.WriteLine("This is between 4 and 8 and is upper and has the correct ascii values and contains no repeating characters");

                    valid = true;

                }


                Console.ReadLine();

            }

        }

    }

}
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ConsoleApp14

{

    internal class Program

    {

        static void Main(string[] args)

        {

            bool valid = false;

            while (!valid)

            {

                Console.WriteLine("Enter a string: ");              

                string sentence = Console.ReadLine();

                int length = sentence.Length;

                char[] sentence2 = sentence.ToCharArray();

                bool isupper = true;

                bool notrepeating = true;

                int valueascii = 0;

                for (int i = 0; i < length; i++)

                {

                    char b = sentence2[i];

                    if (Char.IsUpper(b) == false)

                    {

                        isupper = false;

                    }

                    valueascii = valueascii + Convert.ToInt32(b);

                }

                Array.Sort(sentence2);

                for (int i = 0; i < length; i++)

                {


                    if (sentence2[i] == sentence2[i++])

                    {

                        notrepeating = false;

                    }

                }

                Console.WriteLine(sentence2);

                if (length > 4 && length < 8 && isupper && valueascii > 419 && valueascii < 601 && notrepeating)

                {

                    Console.WriteLine("This is between 4 and 8 and is upper and has the correct ascii values and contains no repeating characters");

                    valid = true;

                }


                Console.ReadLine();

            }

        }

    }

}
9 Replies
9.h
9.h9mo ago
The for loop in question is the last one, the sentence2[i] == sentence2[i++] one. When I remove it, the program works completely fine. With it, it suddenly does not print the last writeline saying it is correct even if it is GFEDCBA is a good testing string as it should theoretically satisfy all the requirements The for loop is meant to check if there are any repeating characters. My logic is that, once I sort the array, if there are any repeating characters, they will be next to each other, and so the IF should turn the NOTREPEATING boolean into false I think the root of my issue is the ++, I should put a + 1 instead, I'll check when I get home
SinFluxx
SinFluxx9mo ago
So for your example string:
GFEDCBA
length = 7 So in your loop:
for (int i = 0; i < length; i++)
{
if (sentence2[i] == sentence2[i++])
{
notrepeating = false;
}
}
for (int i = 0; i < length; i++)
{
if (sentence2[i] == sentence2[i++])
{
notrepeating = false;
}
}
i will go up to 6, what happens then, what will sentence[i] and sentence[i++] be?
9.h
9.h9mo ago
There is no value 1 above the last Hmmm Thank you, I will check this when I get home I am not sure how to fix that yet but I'll figure it out What if I add another condition to the if, stating that i must not be equal to the length? So it will stop once i = length which would be fine as position length was already checked with length - 1
for (int i = 0; i < length; i++)
{
if (sentence2[i] == sentence2[i+1] && i != length)
{
notrepeating = false;
}
}
for (int i = 0; i < length; i++)
{
if (sentence2[i] == sentence2[i+1] && i != length)
{
notrepeating = false;
}
}
SinFluxx
SinFluxx9mo ago
Well i shouldn't reach length anyway, because your for loop already has the condition i < length, the problem is that when i reaches its maximum value (length - 1) sentence[i+1] tries to access an index that doesn't exist
9.h
9.h9mo ago
So doesn't my change fix that as it prohibits i from reaching the maximum value?
SinFluxx
SinFluxx9mo ago
no, because if i == length - 1 then it will still satisfy the condition i != length but sentence2[i+1] is still trying to access an index that doesn't exist your for loop is letting i increase to too high a value i.e. if you've reached the last character, there's no next character to compare it to, so your for loop should stop before it reaches the last character
9.h
9.h9mo ago
In that case, is the solution to - 1 from length in the for loop? So,
for (int i = 0; i < length-1; i++)
for (int i = 0; i < length-1; i++)
and the rest the same as at the start
for (int i = 0; i < length-1; i++)
{
if (sentence2[i] == sentence2[i++])
{
notrepeating = false;
}
}
for (int i = 0; i < length-1; i++)
{
if (sentence2[i] == sentence2[i++])
{
notrepeating = false;
}
}
SinFluxx
SinFluxx9mo ago
You forgot your change of:
sentence2[i++]
to:
sentence2[i+1]
9.h
9.h9mo ago
Oh yeah that too Ty
Want results from more Discord servers?
Add your server