Home > Mobile >  Extra numbers are being added to my randomly generated pseudo number c
Extra numbers are being added to my randomly generated pseudo number c

Time:10-07

When i generate a random number that is eight integers long a bunch of other random numbers are added to it with a dash.

This generates the random number

void passGen(int p[])
{
    int iTest;

    for (int i = 0; i <= 8; i  )
    {
        iTest = rand() % 8;
        p[i] = iTest;
    };
};

The list that is used comes from here

Info Register()
{
    Info NewPerson;

    int pass[8];
    passGen(pass);

    string password;
    for (int i = 0; i <= 8; i  )
    {
        password  = to_string(pass[i]);
    }

    string name;
    string surname;
    int age;
    int lengthOfStay;
    cout << "Please enter your name: ";
    cin >> NewPerson.name;
    cout << "\n";

    cout << "Please enter your surname: ";
    cin >> NewPerson.surname;
    cout << "\n";
    cout << "Please enter your age: ";
    cin >> NewPerson.age;
    cout << "\n";
    cout << "How long will you be staying: ";
    cin >> NewPerson.lengthOfStay;
    cout << "\n";

    ofstream myfile(NewPerson.name   password   ".txt");

    if (myfile.is_open())
    {
        myfile << NewPerson.name << "\n"
               << NewPerson.surname << "\n"
               << NewPerson.age << "\n"
               << NewPerson.lengthOfStay;
    }
    else
    {
        cout << "Something went wrong";
    }

    myfile.close();
    return NewPerson;
};

instead of the file name having an eight digit long number it reads: name61660260-1413481856 the eight digit number is there but a -1413481856 is added. I am generally new to c so I'm not really sure what the problem is

CodePudding user response:

Your array size is 8 and your for loop is doing 9 iterations(0 to 8).

Since array indexing starts from 0, your iterations should be till 7 only.

void passGen(int p[])
{
    int iTest;

    for (int i = 0; i <= 7; i  )
    {
        iTest = rand() % 8;
        p[i] = iTest;
    };
};

Info Register()
{
    Info NewPerson;

    int pass[8];
    passGen(pass);

    string password;
    for (int i = 0; i <= 7; i  )
    {
        password  = to_string(pass[i]);
    }

    string name;
    string surname;
    int age;
    int lengthOfStay;
    cout << "Please enter your name: ";
    cin >> NewPerson.name;
    cout << "\n";

    cout << "Please enter your surname: ";
    cin >> NewPerson.surname;
    cout << "\n";
    cout << "Please enter your age: ";
    cin >> NewPerson.age;
    cout << "\n";
    cout << "How long will you be staying: ";
    cin >> NewPerson.lengthOfStay;
    cout << "\n";

    ofstream myfile(NewPerson.name   password   ".txt");

    if (myfile.is_open())
    {
        myfile << NewPerson.name << "\n"
               << NewPerson.surname << "\n"
               << NewPerson.age << "\n"
               << NewPerson.lengthOfStay;
    }
    else
    {
        cout << "Something went wrong";
    }

    myfile.close();
    return NewPerson;
};

CodePudding user response:

int pass[8];
for (int i = 0; i <= 8; i  )

Your array only have 8 elements, yet you iterate 9 times over it (from 0 to 8 included).

From there, undefined behaviour makes everything possible. Including demons from noses yada yada.

  •  Tags:  
  • c
  • Related