Mynotoar
Active member
Hey guys - I came across a programming problem that I wanted to solve, and I've almost got a program working that does what I want it to do, but not quite.
What I want to do is to create a digital river - this is where you add up every digit within a number, and then add that result to the number. For example, for the river of 456, you get 471, then 493, then 509. 4+5+6 = 15, and 456 + 15 = 471. The code I've got to get it working is, I reckon, stupidly overcomplicated, as you'll see from the code - I start by using a For loop to iterate through every number using a Mid. But because I couldn't get the Mid to read from a number, I had to convert River (the number you put in to generate a River,) into a string, and then for all ten numbers, convert it back into an integer value. So for every digit inside the number, it goes through it and adds it together, then once it's finished going through the number, it adds that sum to the first number declared as river.
For the first iteration, it works wonderfully. If I type in 48, it will correctly tell me that the next number in the River is 60, because 4+8 is 12, and 48+12 = 60. But then it keeps adding 12 to the sequence to get 72, 84, etc... it should go through the For loop again for however many iterations you specified and each time give a new value for the AddToRiver variable (which is what is added onto River,) but no, it defines AddToRiver once and then uses that value all the way through. I've tried setting it to 0 at various places throughout the For loop and it hasn't worked. It may be something to do with the Index variable, which I used to iterate through the digits, but when I tried setting that to 0 in various places that also didn't work.
Can anyone help me to understand what's gone wrong here?
What I want to do is to create a digital river - this is where you add up every digit within a number, and then add that result to the number. For example, for the river of 456, you get 471, then 493, then 509. 4+5+6 = 15, and 456 + 15 = 471. The code I've got to get it working is, I reckon, stupidly overcomplicated, as you'll see from the code - I start by using a For loop to iterate through every number using a Mid. But because I couldn't get the Mid to read from a number, I had to convert River (the number you put in to generate a River,) into a string, and then for all ten numbers, convert it back into an integer value. So for every digit inside the number, it goes through it and adds it together, then once it's finished going through the number, it adds that sum to the first number declared as river.
VB.NET:
Sub Main()
Dim Index As Integer
Dim DigitInRiver As String
Dim River, IterationsOfRiver As Integer
Dim AddToRiver As Integer
Console.Write("Enter a number to make a Digital River from: ")
River = Console.ReadLine
Console.Write("How many iterations of the river would you like to display: ")
IterationsOfRiver = Console.ReadLine
Console.Write("River: ")
Console.ForegroundColor = ConsoleColor.Cyan
Console.Write(River & " ")
For h = 1 To IterationsOfRiver
For i = 1 To Len(River)
Index += 1
River.ToString()
DigitInRiver = Mid(River, Index, 1)
If DigitInRiver = "1" Then
AddToRiver += 1
ElseIf DigitInRiver = "2" Then
AddToRiver += 2
ElseIf DigitInRiver = "3" Then
AddToRiver += 3
ElseIf DigitInRiver = "4" Then
AddToRiver += 4
ElseIf DigitInRiver = "5" Then
AddToRiver += 5
ElseIf DigitInRiver = "6" Then
AddToRiver += 6
ElseIf DigitInRiver = "7" Then
AddToRiver += 7
ElseIf DigitInRiver = "8" Then
AddToRiver += 8
ElseIf DigitInRiver = "9" Then
AddToRiver += 9
ElseIf DigitInRiver = "0" Then
AddToRiver += 0
End If
Next
River += AddToRiver
Console.Write(River & " ")
Next
Console.ResetColor()
Console.ReadKey()
Sub Main()
For the first iteration, it works wonderfully. If I type in 48, it will correctly tell me that the next number in the River is 60, because 4+8 is 12, and 48+12 = 60. But then it keeps adding 12 to the sequence to get 72, 84, etc... it should go through the For loop again for however many iterations you specified and each time give a new value for the AddToRiver variable (which is what is added onto River,) but no, it defines AddToRiver once and then uses that value all the way through. I've tried setting it to 0 at various places throughout the For loop and it hasn't worked. It may be something to do with the Index variable, which I used to iterate through the digits, but when I tried setting that to 0 in various places that also didn't work.
Can anyone help me to understand what's gone wrong here?