Help please

Pages: 123
Hi everyone!

So I am trying to teach myself programming and I am really beginning to grasp c++ but there is a practice exercise I don't understand.
I was hoping somebody could start the code off for me?



This is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Practice 
//


#include <fstream>
#include <iostream>

int main ()
{
	
	
	
	
	
	system("pause");
	return 0;
}


Thanks
Last edited on
Prompting the user for the same of an input file would be a good place to start.

You will want to #include iostream, fstream, and probably string for this program.
I/O with files : http://www.cplusplus.com/doc/tutorial/files/

Hints:
1.Input a sting from user as the file-name,open it.
2.Read the first line using getline() , or since you know its a number use extraction operator.
1
2
int array_size ;
file >> array_size;

3.For a dynamically sized array , you can use std::vector , but I suspect you are not supposed to here.
So allocate memory from the free-store or heap:
1
2
3
double *my_array = new double[array_size];
//do whatever with the array.
delete[] my_array;//give it back,otherwise it may cause a memory leak in the application 

Reference : http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
Hmm, I don't seem to understand how to us the getline.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
#include <iostream>

using namespace std;

int main ( )
{
	char filename [30]; // char array to hold file name


	cout << ( "Please enter the file name: " ); // you forgot a semicolon here

	cin.getline ( filename, 30 );


	// this creates a double array of size 100
	double *my_array = new double[100];

	// this deletes the dynamically created array
	delete [] my_array;
}
Last edited on
You are correctly using get line , its a member function of cin but when you want to use std::string you have to use it other way.

Also the question said dynamically allocate memory by reading the first line of the file.
1
2
3
4
5
6
ifstream file(filename);
int size;
file >> size ;
double *array = new double[size];
...
delete array[];

Last edited on
thank you for the help everyone! This is what I have so far but I am getting an error message. "error message: error allocation 834349034 bytes" Any ideas what I am doing wrong?

Last edited on
You're trying to allocate more memory in line 20 than you have on your computer. Try reading in a smaller number.
I'm sorry, how would I allocate a smaller amount of memory? I tried changing the 30 to a 1 but i still get the same error message.
closed account (2UD8vCM9)
Please post the text file if possible.
Hi, I would but there isn't a text file being outputted.
closed account (2UD8vCM9)
Well in your code you're trying to read from a textfile not create one.

Open and read the first line of this user
- specified input file .
Last edited on
Im really confused.
Last edited on
closed account (2UD8vCM9)
Your problem is you're trying to read from a file that doesn't exist. You need a file to read from if you're going to try to put information from a file into a variable.
ok I know what you mean.

I have a file named "numbers" with the below values in them.

45
1
34.3
12
0.0
0.0
78.53
3
7
46
86.3
5
78.53
3
7
46
86.3
5
Last edited on
closed account (2UD8vCM9)
Try changing your code to see if the file is even being opened
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
#include <fstream>
#include <iostream>

using namespace std;

int main ( )
{
	char filename [30]; 


	cout << ( "Please enter the file name: " );

	cin.getline ( filename, 30 );


	
	ifstream file(filename);
        if (file.is_open())
        {
	int size;
	file >> size ;
	double *array = new double[size];

	// this deletes the dynamically created array
	delete [] array;
        } else
         {
              cout << "Error! Could not open file " << filename << endl;
          }

	
	//console controls
	cout<<"\nPress 'enter' to exit: ";
	system("pause");

    return 0;
}
Thanks for going back and forth with me Pindrought. I copied the code above ou gave me and i am still getting that error message. Should I change the word "filename" to the "numbers" file?
Yes, for testing you can just change line 17 to
ifstream file( "numbers.txt" );
(I'm assuming it's a .txt file)
i still cant get it to work.


After I run the program and enter a name in, I get an error message.
Last edited on
line 8: 30 specifies the number of characters in that array. filename is an array of 30 characters.

line 13: should be cin.getline(filename,29)
This will attempt to read 29 characters from cin into filename. It doesn't name any files, it just gets a string of characters. It needs to be 29, not 30, because c-strings have to have a null terminator. That is, the last character has to be '\0'. To make sure that this is true...

line 8: you should change this to char filename [30] = { 0 };
This will set every character in the array to the null character.
Pages: 123