cant figure this out

Ok so i have a function that outputs 7 numbers in a row, now i want to output 5 lines of thosenumbers but i cannot figure out how to do it i have been trying to do it for about 10 minutes now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string numbers = ("");
    vector<string> holdNumbers;
    bool getNumberLoopEnd = false;
    int loopCounter = 0;

    while(getNumberLoopEnd != true)
    {
        for(int i = 0; i < 7; i++)
        {
            numbers = '0' + rand() % 10;
            //cout << numbers;
        }
        holdNumbers.push_back(numbers);
        loopCounter++;
        if(loopCounter == 7)
        {
            break;
        }
    }

    for(int i = 0; i < 7; i++)
    {
        cout << holdNumbers[i];
    }
From what I gather, you just need to put it in a loop that iterates 5 times. Something like: http://ideone.com/08URzP

Maybe I gather wrong? Could you possibly provide us with a sample output?
well that code compiles but i just get a long line of characters, i want 5 lines of 7 characters in a row.
i tried something like this but it didnt work either

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
while(getNumberLoopEnd != true)
    {
        for(int i = 0; i < 7; i++)
        {
            numbers = '0' + rand() % 10;
            if(numbers.length() == 7)
            {
                holdNumbers.push_back(numbers);
            }
        }

        loopCounter++;
        if(loopCounter == 7)
        {
            break;
        }
    }
    for(int i = 0; i < 5; i++)
    {
        cout << holdNumbers[i];
    }
Last edited on
Write a nested loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand( std::time(nullptr) ) ;

    for( int i = 0 ; i < 5 ; ++i ) // 5 lines
    {
        for( int j = 0 ; j < 7 ; ++j ) // write 7 random digits per line
            std::cout << std::rand() % 10 << ' ' ;

        std::cout << '\n' ; // and end with a new line
    }
}

http://ideone.com/1mW9Wh
but i need to push back each line into a vector, so when i output the vector it will output 5 lines of 7 numbers. This only outputs 1 line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(getNumberLoopEnd != true)
    {
        for(int i = 0; i < 5; i++)
        {
            for(int i = 0; i < 7; i++)
            {
                numbers = '0' + rand() % 10;
            }
            holdNumbers.push_back(numbers);
        }

        loopCounter++;
        if(loopCounter == 7)
        {
            break;
        }
    }
Last edited on
> but i need to push back each line into a vector,
> so when i output the vector it will output 5 lines of 7 numbers.

A std::string would be adequate.

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>
#include <cstdlib>
#include <ctime>
#include <string>

int main()
{
    std::srand( std::time(nullptr) ) ;

    std::string five_x_seven ;

    for( int i = 0 ; i < 5 ; ++i ) // 5 lines
    {
        for( int j = 0 ; j < 7 ; ++j ) // write 7 random digits per line
        {
            five_x_seven += std::rand() % 10 + '0' ;
            five_x_seven += ' ' ;
        }
        five_x_seven += '\n' ; // and end with a new line
    }

    std::cout << five_x_seven ;
}

http://ideone.com/Tuo628
yeah but i need to use each line for something different so i need the vector.
Hi Chay,

Can you figure out how to adapt JLBorges code to do what you want? You have 2 things : push_back a char to a string; push_back the string to a vector.

When doing your own code, try to get the simple things working first, then add complexity afterwards - preferably in small amounts. In this case get the code to build 1 string of 7 numbers working first, then add code to do 5 lines of them.

For loops are good when the number of iterations is known. While loops are better when some end condition is satisfied - like if you had some sentinel value to end the input for example.

I know this sounds boring & I might have said it before, but try to write pseudo-code with comments first, then translate to actual code:

1
2
3
4
// loop 7 times:
//       make a random number 1-9
//       push_back number to string
//end loop. 


Now add the extra complexity:

1
2
3
4
5
6
7
8
9
//loop 5 times:
//
//   loop 7 times:
//       make a random number 1-9
//       push_back number to string
//   end 7 times loop.
//
//push_back string into vector
//end 5 times loop  


So code the first part, then it become easy to do the second part.

Hope this helps a bit.


Edit: I didn't see JLBorges last post or Chay's replies. Just trying to get Chay to think about the process a bit. Any way probably better in JLB's expert hands.
Last edited on
well that code compiles but i just get a long line of characters

When I compile and run the code (I provided) above, I get:

1389010
1917054
4520981
1709523
7169976


Which, to me, sounds like what you are asking for:

output 5 lines of 7 numbers


Again, could you provide us with a sample output?
so this

numbers = rand() % 10 + '0';

is being pushed into a char or a string? if its a char i then have to convert it to a string correct? after that i can push it back into the vector which should then output on seperate lines?
"Again, could you provide us with a sample output?"

the output you provided is what i want, but it wont do that.
the output you provided is what i want, but it wont do that.

Huh? It works for me (it even worked on the link I provided). How did you use it in your code (did you copy + paste or write it out yourself)? It almost sounds like you are not printing to a newline for each iteration of the outer loop.
@Chay

Why can't you use the code given to you? I am not purposely being harsh, but your own code is over complicated and full of contradictions. For example look at lines 15 and 16 - what is happening there?

You don't seem to understand the data structures you are using. It's simple - append a char to a string until the string has 7 chars, push_back that string to the vector. JLBorges code makes the string, so instead of appending the newline, just push that string into a vector, but make sure the string is reset to "" each time a new 7 digit string is started.

At the moment you a vector of 7 strings each of which has 1 char.

With your for loops, you have got away with using the index i for both of them, because i isn't used within the loops. This is bad practice and will cause you trouble one day.

This is why I mentioned the psuedo-code thing - it helps to think about the problem clearly, and can identify various things.

You are nearly there - just think clearly about what is going on.
Ok i got this pretty close to working i just am a little unsure of how to get it to break on a different line. keep in mind that each line has to be in a seperate vector element, so each vector element has to equal 1 line of 7 numbers, it cant be one long line of numbers that is new lined every 7 words.

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
void Player::hackingMethod()
{
    srand(time(0));

    char unconvertedNums;
    int convertedNums;
    vector<int> holdNumbers;
    stringstream convertToString;


        for(int i = 0; i < 5; i++)
        {
            for(int i = 0; i < 7; i++)
            {
                unconvertedNums = rand() % 10 + '0';
                unconvertedNums = ' ';
                convertToString << unconvertedNums;
                convertToString >> convertedNums;
            }
            holdNumbers.push_back(convertedNums);
            unconvertedNums = 0;
        }


    for(int i = 0; i < 7; i++)
    {
        cout << holdNumbers[i];
    }
    cout << "\n\n";
}
keep in mind that each line has to be in a seperate vector element, so each vector element has to equal 1 line of 7 numbers, it cant be one long line of numbers that is new lined every 7 words.


Your code contradicts this, you now have a vector of ints.

Seriously, get rid of your function, and try modifying JLBorges code.

Remember, you have 2 data structures:

1. A string to append chars to.
2. A vector to push_back the already created string.
Ok so i did this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Player::hackingMethod()
{
    srand(time(0));

    string five_x_seven;
    vector<string> numbers;

    for(int i = 0 ; i < 5 ; ++i) // 5 lines
    {
        for(int j = 0 ; j < 7 ; ++j) // write 7 random digits per line
        {
            five_x_seven += rand() % 10 + '0';
        }
        five_x_seven += '\n' ; // and end with a new line
        numbers.push_back(five_x_seven);
    }

    for(int i = 0; i < 3; i++)
    {
        cout << numbers[i];
    }
}


and when i output numbers it gives me 6 lines??? when i do it once it gives me just one but then i do 5 and it gives me 15 lines whats going on?
Last edited on
Line 14 is really unnecessary. Also, TheIdeasMan already said to make sure the string is cleared each time.
15
16
numbers.push_back(five_x_seven);
five_x_seven = "";


Edit:
Hopefully to give you a hint of what is going on with your code:
1 = 1
3 + 2 + 1 = 6
5 + 4 + 3 + 2 + 1 = 15
Last edited on
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
void Player::hackingMethod()
{
    // srand(time(0));
    // seed the C lcg *once* at the beginning of the program
    // probably at the start of main()

    // string five_x_seven; // *** move this

    vector<string> numbers;

    for(int i = 0 ; i < 5 ; ++i) // 5 lines
    {
        string line ; // *** here
        // (start with a brand new *empty* string for each new line) 

        for(int j = 0 ; j < 7 ; ++j) // write 7 random digits per line
        {
            line += rand() % 10 + '0';
        }
        line += '\n' ; // and end with a new line
        numbers.push_back(line);
    }

    for( int i = 0; i < /* 3 */ numbers.size() ; i++)
    {
        cout << numbers[i];
    }
}

Last edited on
Topic archived. No new replies allowed.