-2

I'm beginner in C++. Learning mostly from YouTube courses. I was doing the task when I came across the problem which stops me from finishing. I would like to ask for help with the part of code.

#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <sstream>
#include <ostream>

using namespace std;

int main()
{
    srand(time(0));
    ofstream file;

    cout<<"TASK"<<"\n";

    file.open("TASK.txt", fstream::in | fstream::out | fstream::trunc );
    
    if(file.good() == true)
    {
        cout<<"TRUE"<<"\n";
        char ch[] = {'#','@','*'}; // Array of three chars.

        for(int i=0; i<10; i++)
        {
            for(int j=0; j<10; j++)
            {
                file<<ch[rand()%3]; //Random character
            }
            file<<endl;
        }
        file.seekp(rand()%120);
        file.put('P');
        file.close();
    }
    else cout<<"FALSE"<<"\n";
    return 0;
}

What I've done:

  1. Opened a file and created array for 3 characters from task description.

  2. Created 2 for loops which write and format the file data.

  3. Found out by using file.seekp(0,file.end) that the amount of characters in file equals 120.

  4. Used knowledge of the amount of characters to file.seekp(rand()%120) which sets position in output sequence to random place.

  5. Used file.put('P') to place character in that place.

  6. Closed file.

What I don't know:

  1. How to get rid of (or do something else to) these 20+ characters to change only these: '#', '@', '*'.

  2. To visualise the problem:

OUTPUT: when it works as intended.

@#*#*##@*@
@@*##*@@@*
@@**@#*@##
#@@*@##P**
****##@@*#
#@#@*@####
**#@#@#@*@
@*#*@###*#
*@###*#@*#
@@*#@@#@@@

When it doesn't work as intended:

@@@#@@@*#*
##*##*@@#*
@@##@#*#*@P
*@@@**###*
##*@*#*@**
**##@#*@**
@###@*#***
@#@**##@@*
#@*###@@@*
@#@#*#*#@#

If I wasn't clear enough I can try to explain more.

Solution (thanks to rustyx)

int r_number = 0;

        for(int i=0; i<=120; i++)
        {
            int r_number2 = rand()%120;
            if((r_number2%12)>=10)
            {
                r_number2 = rand()%120;
            }
            else r_number=r_number2;
            }

Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CyTruSeK
  • 13
  • 2
  • 2
    Please post your code as text directly in your question, not as an image. – MikeCAT Jun 08 '20 at 23:21
  • 5
    "I'm begginer in C++. Learning mostly from YouTube courses." - Don't. YouTube (and many other websites) is a horrible way to learn C++ because there are so many people who don't know what they're talking about and teach all sorts of crap. Instead, get a [good book](https://stackoverflow.com/q/388242/9254539). – eesiraed Jun 08 '20 at 23:24
  • I couldn't paste your image into my IDE. No code pasted as text == no help. – Thomas Matthews Jun 08 '20 at 23:34

1 Answers1

3

The problem is that there are CR, LF characters (\r\n) at the end of each line, which shouldn't be touched. But you generate a random number between 0 and 119, which can hit those and damage the file.

I can think of 2 possible solutions:

  • loop and get a new random number if the remainder of dividing by 12 is >= 10 (you see why?)
  • get a random number between 0 and 99 and pad it for CR, LF: x += (x/10)*2

I don't know how VC++ handles file position in text mode, it might treat CR, LF as a single position. In that case adjust the padding logic from 12 to 11.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • I went with 1 solution. After running the code for at least 50 times, I think it's working. Thank you. – CyTruSeK Jun 09 '20 at 00:49