Pointers and Dynamic Memory

I'm writing a program that uses pointers and dynamic memory. It will prompt the user for a filename, how many numbers to read (with a max of 20), and a number to find in the array. Then it will output whether the number exists in the numbers read from the file. I'm stuck on how to get my program to calculate how many numbers to read and a number to find in the array from a text file that contains this list of 20 numbers:

11
12
2
29
35
36
44
54
56
6
72
75
8
84
88
89
90
97
3
9

I provided my code I have so far including my current output and the expected output. If anyone could give me some hints on how to solve my problem, I would appreciate it.

My Code:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream inputFile;
    string filename;
    int num1;
    int num2;
    const int MAXNUM = 20;
    int *numArray = new int[MAXNUM];

    cout << "Enter a filename: ";
    cin >> filename;

    // Open the file.
    inputFile.open(filename);

    if (inputFile)
    {
        cout << "How many numbers to read from the file [1 - 20]? ";
        cin >> num1;

        while (inputFile >> num1)
        {
            for (int i = 0; i < MAXNUM; i++)
            {
                num1 = numArray[i];
            }
        }
        if (num1 < *numArray)
        {
            cout << "Invalid input detected. Numbers [1 - 20] only!" << endl;

            cout << "How many numbers to read from the file [1 - 20]? ";
            cin >> num1;
        }

        cout << "Read " << num1 << " numbers from "
             << filename << endl;

        cout << "Which number should I look for? ";
        cin >> num2;

        while (inputFile >> num2)
        {
            for (int i = 0; i < MAXNUM; i++)
            {
                num2 = numArray[i];
            }
        }
        if (num2 < *numArray)
        {
            cout << "Number cannot be found. "
                 << "Enter another number." << endl;

            cout << "Which number should I look for? ";
            cin >> num2;
        }

        cout << num2 << " is in the array." << endl;
    }

    // Close the file.
    inputFile.close();

    // Free the memory;
    delete [] numArray;
    return 0;
}


My Output:
1
2
3
4
5
6
7
Enter a filename: numbers.txt
How many numbers to read from the file [1 - 20]? 10
Read 1296125778 numbers from numbers.txt
Which number should I look for? 12
Number cannot be found. Enter another number.
Which number should I look for? 12
12 is in the array.


Expected Output:
1
2
3
4
5
Filename? numbers.txt
How many numbers to read from the file? 10
Read 10 numbers from numbers.txt
Which number should I look for? 12
12 is in the array.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <string>

int get_num_to_read( int max_value )
{
    int num_to_read = 0 ;
    std::cout << "How many numbers to read from the file [1 - " << max_value << "]? ";
    std::cin >> num_to_read ;

    if( num_to_read > 0 && num_to_read <= max_value ) return num_to_read ;

    std::cout << "invalid input. try again\n" ;
    return get_num_to_read(max_value) ; // try again
}

// return count of numbers actually read
int read_numbers_into_arrray( int array[], std::istream& stm, int num_to_read )
{
        int num_read = 0 ;

        while( num_read < num_to_read && stm >> array[num_read] ) ++num_read ;

        return num_read ;
}

// return true if num_to_look_for was found in the array
bool find_number_in_array( const int array[], int array_sz, int num_to_look_for )
{
    for( int i = 0 ; i < array_sz ; ++i )
        if( num_to_look_for == array[i] ) return true ;

    return false ;
}

int main()
{
    std::string filename;
    std::cout << "Enter filename: ";
    std::cin >> filename;

    if( std::ifstream inputFile{filename} ) // if input file was opened successfully
    {
        const int MAXNUM = 20;
        int numArray[MAXNUM] ; // array to hold MAXNUM items
        // note that there is no good reason to allocate this array dynamically

        const int num_to_read = get_num_to_read(MAXNUM) ;

        const int num_actually_read = read_numbers_into_arrray( numArray, inputFile, num_to_read ) ;

        std::cout << "Read " << num_actually_read << " numbers from file ' " << filename << '\n' ;

        if( num_actually_read > 0 ) // if at least one number was read
        {
            int num_to_look_for = 0 ;
            std::cout << "Which number should I look for? ";
            std::cin >> num_to_look_for ;

            const bool found = find_number_in_array( numArray, num_actually_read, num_to_look_for ) ;

            if(found) std::cout << num_to_look_for << " is in the array\n" ;
            else std::cout << "did not find number " << num_to_look_for << " in the array\n" ;
        }
    }

    else // failed to open the input file
    {
        std::cout << "failed to open input fil;e '" << filename << '\n' ;
        return 1 ;
    }
}
@JLBorges; Thank you for your help! However, is there a way to do it without using functions?
> is there a way to do it without using functions?

Yes. But what is wrong with using functions?
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
   const int MAXNUM = 20;

   string filename;
   cout << "Input filename: ";
   cin >> filename;
   ifstream in( filename );

   int num;
   cout << "How many to read [1 - " << MAXNUM << "] ? ";
   cin >> num;

   int *A = new int[num];
   for ( int i = 0; i < num; i++ ) in >> A[i];

   int x;
   cout << "What number to search for: ";
   cin >> x;

   bool found = false;
   for ( int i = 0; i < num && !found ; i++ ) found = ( A[i] == x );
   cout << x << ( found ? " is " : " is not " ) << "found in the array\n";

   delete [] A;
}
Last edited on
@JLBorges; There is nothing wrong with using functions. I think they are very useful in writing a program. Because my original program didn't use any functions, I wanted advice on any alternatives that could fix the problem in my program in order to show the expected output I provided.
Thank you to those who helped me! I provided my new code and output below. It works perfectly!

My New Code:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream inputFile;
    string filename;
    int num1 = 0;
    int num2 = 0;
    const int MINNUM = 0;
    const int MAXNUM = 20;
    int *numArray = new int[MAXNUM];

    cout << "Enter a filename: ";
    cin >> filename;

    // Open the file.
    inputFile.open(filename);

    if (inputFile) // If file successfully opened.
    {
        cout << "How many numbers to read from the file "
             << "[1 - " << MAXNUM << "]? ";
        cin >> num1;

        while (num1 < MINNUM || num1 > MAXNUM) // Count numbers from 1 - 20 only!
        {
            cout << "Invalid number detected! Try Again." << endl;

            cout << "How many numbers to read from the file "
                 << "[1 - " << MAXNUM << "]? ";
            cin >> num1;
        }

        for (int i = 0; i < num1; i++)
            inputFile >> numArray[i];

        cout << "Read " << num1 << " numbers from "
             << filename << endl;

        cout << "Which number should I look for? ";
        cin >> num2;

        bool found = false;
        for (int i = 0; i < num1 && !found; i++) // Calculates whether the number
              found = (numArray[i] == num2);     // can be found in the array.

        cout << num2 << (found? " is " : " is not ")
             << "in the array." << endl;
    }
    else // If file couldn't be found.
    {
        cout << "Failed to open file " << filename << "." << endl;
    }

    // Close the file.
    inputFile.close();

    // Free the memory;
    delete [] numArray;
    return 0;
}


My New Output:
1
2
3
4
5
6
7
Enter a filename: numbers.txt
How many numbers to read from the file [1 - 20]? 22
Invalid number detected! Try Again.
How many numbers to read from the file [1 - 20]? 10
Read 10 numbers from numbers.txt
Which number should I look for? 12
12 is in the array.
Last edited on
Topic archived. No new replies allowed.