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:
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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
ifstream inputFile;
string filename;
int num1;
int num2;
constint MAXNUM = 20;
int *numArray = newint[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.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
constint 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 = newint[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;
}
@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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
ifstream inputFile;
string filename;
int num1 = 0;
int num2 = 0;
constint MINNUM = 0;
constint MAXNUM = 20;
int *numArray = newint[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.