Having some major troubles with my script

I am taking a C++ programming class in college and need to make a 10X10 grid of numbers that go from 0-9 and after the number reaches 9 it resets back at 0. Like
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345

When I type in a number like 5, I get:
567891011121314









I dont know what to do. I have until May 3rd to figure it out. Ive been doing C++ for about a month. Please any help would be very helpful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// This program makes a 10x10 grid of numbers from 1 number that the user inputs.

#include <iostream>

using namespace std;
int main()
{
    int startNum;  // This is for the number for the user to input.
    int digitCount = 0;  // This is the number of digits in a row.
    int loopCount = 0;  // This is for the number of rows.
    cout << "Hello player. Please input a number from 0-9." << endl;
    cin >> startNum;  // Gets user input from these 2.

          while (loopCount < 10)  // I think this is where my problem is.
          {
                while (digitCount < 10)  // I think I have another problem here.
                {
                      digitCount++;  // If I delete the digitCount++;, the program goes into an infinate loop. I also dont know how to make the program reset the numbers from 9-0.
                      cout << startNum;
                      startNum++;
                }
                loopCount++;
                cout << startNum;
                startNum++;
          }
    
    system("pause");
    
    return 0;
}


Sorry if the last comment is a little long.
i have the answer if you want me to post it for you. anyhow try just using one for loop. i made an array that had the numbers 1-9. then in the for loop i have the variable i set to 0 and incrementing by 1 each time. inside the loop you just need to set i to 0 if the last number you printed was 9 and use the break command to get out of the for loop if the number in i is equal to the number entered by the user. hope this helps :)
What is your desired goal? What do you want help with exactly? You want to make a 10x10 grid? But what is the intended output? Is there any specifics?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
    int Input = 0;
    int Grid[10][10];
    cout << "Hello player, please input a number from 0 - 9: ";
    cin >> Input;
    for (int X = 0; X < 10; X++)
    {
        for (int Y = 0; Y < 10; Y++)
        {
        if (Input > 9) Input = 0;
        cout << (Grid[X][Y] = Input);
        Input++;
        }
        Input--;
        cout << endl;
    }
    system("pause");
    return 0;
}


That doesn't make the output you show but it does make it in the opposite direction.
Sadly, none of these will work :( I should have put alot more detail. The whole assignment is

Using while loops, output a sequential grid of integers. It should be 10x10, and contain only the digits 0-9. When the sequence hits 9, in either a row or column, it should restart at zero. The starting point for the first row should be supplied by the user, and each subsequent row should be increased by one.
Besides the user instructions, you are only allowed to output one number at a time, and only variables may be sent to the console.
For example, if the user input 7, the grid would be as follows:
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345


We can only use 2 While loops and 1 If statement and 3 Variables. Thats it.
This is as far as I have gotten. It does everything you asked except add 1 to the beginning of each row (after the first row). I am a bit stumped as to how to do that without breaking the given limitation of 1 If statement. But maybe this will get you or someone else thinking on the right track.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main()
{
int start, x=0;

cout << "Enter Starting Number: ";
cin >> start;

while(x < 10)
{
    int y=0;
    while(y <10)
    {
        cout << start++ << " ";
        if(start == 10)
        {
            start = 0;
        }
        y++;
    }
    cout << endl;
    x++;
}
}
Last edited on
i see that you have tried to use the loop in loop but have u tried some thing like the noob code that i just wrote this isnt a 10x10 array cus this isnt a 2d array but im shure you would get some results
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int x=0;x<10;x++)
{
   for(int y=0;y<10;y++)
      {
        arr[y]=y;
       }

for(int z=0;z<10;z++)
      {
       cout<<arr[z];
       }
cout<<endl;

}
Thanks mwforrest7! That helped alot. I can figure out the rest
That looks very much like a task for the modulo operator '%' like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// This program makes a 10x10 grid of numbers from 1 number that the user inputs.

#include <iostream>

using namespace std;
int main()
{
    int startNum;  // This is for the number for the user to input.
    int digitCount = 0;  // This is the number of digits in a row.
    int loopCount = 0;  // This is for the number of rows.
    cout << "Hello player. Please input a number from 0-9." << endl;
    cin >> startNum;  // Gets user input from these 2.

    for(int i = 0; i < (10*10); ++i)
    {
      if((i > 0) && ((i % 10) == 0))
      {
        cout << endl;
        ++startNum;
      }
      cout << (i + startNum) % 10;
    }
    cout << endl;
    
    system("pause");
    
    return 0;
}
Ok. Thanks to mwforrest7, I was able to fix the problem of the next row starting the next number in line. Now there is only 1 thing left to fix That I looked at for over an hour trying different things. When I input a number, it looks fine. Except it skips all the zeros.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// This program makes a 10x10 grid of numbers from 1 number that the user inputs.

#include <iostream>

using namespace std;
int main()
{
    int startNum, digitCount = 0;
    cout << "Hello player. Please input a number from 0-9." << endl;
    cin >> startNum;

          while (digitCount < 10)
          {
                int loopCount = 0;
                while (loopCount < 10)
                {
                      cout << startNum++ << "";
                      if (startNum == 10)
                      {
                                   startNum = 0;  // Having trouble with these 2 lines.
                                   startNum++;
                      }
                      loopCount++;
                }
                cout << endl;
                digitCount++;
          }
          system("pause");
          
          return 0;
}


I tried

1
2
3
4
5
6
                      if (startNum == 10)
                      {
                                   startNum = -1;
                                   startNum++;
                      }


and

1
2
3
4
5
6
      if (startNum == 10)
                      {
                                   startNum = 0;
                                   startNum--;
                      }
I don't have a solution at the moment, but I can point out an error in what you are attempting. By placing the startNum++ inside of the if statement, you are only increasing the 0 by 1, which is why each line is starting with a different number. The error there, is that the assignment says that the first number of each line should be increased. The zeros can be anywhere in the line, and therefore you are increasing the line by 1 at random points.

My line of thinking is that you need to somehow increase startNum by 1 outside of the nested while loop. The problem here is that if the nested loop ends with startNum = 9 then you increase it to 10 in the outer loop, which then causes it to print out a 10 and messes everything up. A second if statement could fix this, but the assignment does not allow that.

Coder777 suggested using modulus. This may work, although I haven't figured out how yet. He uses to a for loop which is not allowed in this assignment. But for loops are essentially while loops anyway with a couple extra components. Anyway, I will be thinking more about this... But maybe that will get you back on the right track.
Alright, I finished this sooner than I thought. The solution does indeed involve modulus. I don't know if there is another way to do it without modulus given the limitations, but your welcome to explore that! Basically what I did was convert Coder777's for loop into a single while loop.

Here it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;
int main()
{
    int startNum, digitCount = 0;
    cout << "Hello player. Please input a number from 0-9." << endl;
    cin >> startNum;

        while(digitCount < 100)
        {
            if((digitCount > 0) && ((digitCount%10)==0))
            {
                cout << endl;
                ++startNum;
            }
            cout << (digitCount + startNum) %10;
            digitCount++;
        }


          return 0;
}


Modulus is not the easiest thing to logically use. I had to use some paper and write out exactly what happens just to understand how this works, so here it is:

 
cout << (digitCount + startNum) %10;

(This is the key line thats spitting out your numbers)

The first row is unaltered by modulus 10 (%10) because anything less than ten will just have a remainder of itself.

I.E. 6%10, 10 doesn't go into 6 at all, and therefore there is a remainder of 6 and so it prints 6.

So for the first row we have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 for digitcount.
Sample calculations will be:

Let 5 be our startnum.

(0+5) % 10 = 5.
(1+5) % 10 = 6.
(2+5) % 10 = 7.

.etc


The reason we use
 
 while(digitCount < 100)


is because when you get above a total of 9 it will start cycling through 0-9.

I.E.

Let startnum= 5.

(4+5) % 10 = 9.
(5+5) % 10 = 0.
(6+5) % 10 = 1.

Fast forwarding digitcount...

(76+5) % 10 = 1.

This next line of code serves a purpose of first, making sure the first row remains unchanged, and secondly, starting a new line and adding 1 after every 10 digits.

1
2
3
4
5
if((digitCount > 0) && ((digitCount%10)==0))
            {
                cout << endl;
                ++startNum;
            }


digitCount > 0 makes it so that your first number will not get the ++startNum found inside the if statement, because your first number has a digitcount = 0 and therefore 0%10 = 0. If the digitCount > 0 was not there, it would skip a line and add 1 for your very first number.

digitCount%10==0 just means, that after every 10 digits, it should perform the following actions. Because 10%10 =0, 20%10=0, 30%10=0 .etc

The actions are pretty straight forward. After 10 digits it ends the line, and then adds 1 to the first digit of the new line.



Last edited on
i feel like everyone is overcomplicating the hell out of this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main(void)
{
	int number = 0;
	cin >> number;
	for (int i = 0; i < 9; i++)
	{
		for (int y = number, counter = 0; counter < 9; counter++)
		{		
			cout << y++;
			if (y > 9)
				y = 1;
		}
		cout << endl;
		if (++number > 9)
			number -= 9;
	}
	return 0;
}
Ascii, your code does not work because the assignment has given us specific limitations. He can only use 2 while loops, 1 if statement, and 3 variables. That means, no for loops, and you got one too many if statements.

This is why you feel like everyone is over complicating things. We had to solve it given the assignment directions. Thanks to coder777 I was able to figure out how to solve it within the given limits by using modulus. See my above post for the explanation if you are curious.
haha sorry didnt read that post. mine still takes up less lines, especially if i just use
using namespace std;

yours might be simpler though
Topic archived. No new replies allowed.