Jump to content

Using cout to display an array in C++

CookieMaster
Go to solution Solved by CookieMaster,
10 hours ago, Mr_KoKa said:

getline function reads line from stream into string so putting it in while loop like this, you keep overwriting the previous line with the next line from your text file. You're ending up with img containing last line of your text file.

 

To save every line in array you would need to enlarge the array after you read line. You could enlarge it by greater number then 1 so it wouldn't be enlarged every time you read new line, but it could take more memory then it needs to, you would then need to fit the array to elements count. So I will make example that will enlarge array one by one.

 


#include <iostream>
#include <fstream>

using namespace std;

int main(){
  ifstream File("cookies.asc");
  
  string img;
  string *array = nullptr;
  int arraySize = 0;
  
  while(getline(File, img)){
    string *newArray = new string[arraySize + 1];
    for(int i = 0; i < arraySize; i++){
      newArray[i] = array[i];
    }
    newArray[arraySize] = img;
    arraySize++;
    delete[] array;
    array = newArray;
  }
  
  for(int i = 0; i < arraySize; i++){
    cout << array[i] << std::endl;
  }
  
  delete[] array;
}

You can see that inside a loop I create newArray of size arraySize + 1, so it is one element larger then it was. Then I copy old array values into new array and at the last array position I set value of newly read line. Then I increment the arraySize to keep its new size, I delete old array and assign newArray pointer to array. After this array is enlarged and keeps line that has been read. After reading I iterate over array to print its elements, then I delete it to free memory.

 

I guess your assignment makes you to use arrays, but using stl containers would be easier and safer.

Thank you very much, and yes the assignment is making me use arrays. 

 

7 hours ago, Unimportant said:

And pray to God no exceptions are thrown.

 

This is not the way to do it, but as this seems a school assignment and the (artificial) limitations on what techniques may or may not be used are unclear it's hard to give a correct answer (normally, one would use std::vector, or at least use a smart pointer to cure the potential leaks).

Thankfully no exceptions where thrown. And you are right it is an assignment, an array was required. Probably a better way to do this but it worked so I'm happy.

 

Now i need to figure out how to save the edits to a new file. I think I have it but I don't know what exactly to save. You can see here what I tried.

 

cout << "Enter a file name to save." << '\n';
			cin >> newname;
			ofstream OutFile(newname + type);
			OutFile << array << '\n';


Edit: Just figured it out.

 

Thanks guys. 

I looked up on how to display an array and that didn't seem to help due to the fact the array is a string. It seems the array just displays the first line. What would I have to do so it reads off the array. BTW, its required that an array is in here, if it wasn't i wouldn't put it in here, kinda pointless.

 

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	int response;
	string filename = "cookies"; // Change this if different file name.
	string type = ".asc"; // Change this if different file type.
	string img; // File later needs to be passed to a string so it can be seen.

	cout << "Welcome to Tx Paint (Tunxis Paint)" << '\n';
	cout << "[1] Load image" << '\n';
	cout << "[2] Draw line in image" << '\n';
	cout << "[3] Draw box in image" << '\n';
	cout << "[4] Save image" << '\n';
	cout << "[5] Exit" << '\n';
	cin >> response; 

	if (response == 1)

	{
		ifstream File(filename + type); // Load file and file type stated above.
		while (getline(File, img)) // Read the entire file
	    //string array[] = { img };
		cout << img << '\n'; // Display the image.
	}

    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

getline function reads line from stream into string so putting it in while loop like this, you keep overwriting the previous line with the next line from your text file. You're ending up with img containing last line of your text file.

 

To save every line in array you would need to enlarge the array after you read line. You could enlarge it by greater number then 1 so it wouldn't be enlarged every time you read new line, but it could take more memory then it needs to, you would then need to fit the array to elements count. So I will make example that will enlarge array one by one.

 

#include <iostream>
#include <fstream>

using namespace std;

int main(){
  ifstream File("cookies.asc");
  
  string img;
  string *array = nullptr;
  int arraySize = 0;
  
  while(getline(File, img)){
    string *newArray = new string[arraySize + 1];
    for(int i = 0; i < arraySize; i++){
      newArray[i] = array[i];
    }
    newArray[arraySize] = img;
    arraySize++;
    delete[] array;
    array = newArray;
  }
  
  for(int i = 0; i < arraySize; i++){
    cout << array[i] << std::endl;
  }
  
  delete[] array;
}

You can see that inside a loop I create newArray of size arraySize + 1, so it is one element larger then it was. Then I copy old array values into new array and at the last array position I set value of newly read line. Then I increment the arraySize to keep its new size, I delete old array and assign newArray pointer to array. After this array is enlarged and keeps line that has been read. After reading I iterate over array to print its elements, then I delete it to free memory.

 

I guess your assignment makes you to use arrays, but using stl containers would be easier and safer.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Mr_KoKa said:

getline function reads line from stream into string so putting it in while loop like this, you keep overwriting the previous line with the next line from your text file. You're ending up with img containing last line of your text file.

 

To save every line in array you would need to enlarge the array after you read line. You could enlarge it by greater number then 1 so it wouldn't be enlarged every time you read new line, but it could take more memory then it needs to, you would then need to fit the array to elements count. So I will make example that will enlarge array one by one.

 


#include <iostream>
#include <fstream>

using namespace std;

int main(){
  ifstream File("cookies.asc");
  
  string img;
  string *array = nullptr;
  int arraySize = 0;
  
  while(getline(File, img)){
    string *newArray = new string[arraySize + 1];
    for(int i = 0; i < arraySize; i++){
      newArray[i] = array[i];
    }
    newArray[arraySize] = img;
    arraySize++;
    delete[] array;
    array = newArray;
  }
  
  for(int i = 0; i < arraySize; i++){
    cout << array[i] << std::endl;
  }
  
  delete[] array;
}

You can see that inside a loop I create newArray of size arraySize + 1, so it is one element larger then it was. Then I copy old array values into new array and at the last array position I set value of newly read line. Then I increment the arraySize to keep its new size, I delete old array and assign newArray pointer to array. After this array is enlarged and keeps line that has been read. After reading I iterate over array to print its elements, then I delete it to free memory.

 

I guess your assignment makes you to use arrays, but using stl containers would be easier and safer.

And pray to God no exceptions are thrown.

 

This is not the way to do it, but as this seems a school assignment and the (artificial) limitations on what techniques may or may not be used are unclear it's hard to give a correct answer (normally, one would use std::vector, or at least use a smart pointer to cure the potential leaks).

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Mr_KoKa said:

getline function reads line from stream into string so putting it in while loop like this, you keep overwriting the previous line with the next line from your text file. You're ending up with img containing last line of your text file.

 

To save every line in array you would need to enlarge the array after you read line. You could enlarge it by greater number then 1 so it wouldn't be enlarged every time you read new line, but it could take more memory then it needs to, you would then need to fit the array to elements count. So I will make example that will enlarge array one by one.

 


#include <iostream>
#include <fstream>

using namespace std;

int main(){
  ifstream File("cookies.asc");
  
  string img;
  string *array = nullptr;
  int arraySize = 0;
  
  while(getline(File, img)){
    string *newArray = new string[arraySize + 1];
    for(int i = 0; i < arraySize; i++){
      newArray[i] = array[i];
    }
    newArray[arraySize] = img;
    arraySize++;
    delete[] array;
    array = newArray;
  }
  
  for(int i = 0; i < arraySize; i++){
    cout << array[i] << std::endl;
  }
  
  delete[] array;
}

You can see that inside a loop I create newArray of size arraySize + 1, so it is one element larger then it was. Then I copy old array values into new array and at the last array position I set value of newly read line. Then I increment the arraySize to keep its new size, I delete old array and assign newArray pointer to array. After this array is enlarged and keeps line that has been read. After reading I iterate over array to print its elements, then I delete it to free memory.

 

I guess your assignment makes you to use arrays, but using stl containers would be easier and safer.

Thank you very much, and yes the assignment is making me use arrays. 

 

7 hours ago, Unimportant said:

And pray to God no exceptions are thrown.

 

This is not the way to do it, but as this seems a school assignment and the (artificial) limitations on what techniques may or may not be used are unclear it's hard to give a correct answer (normally, one would use std::vector, or at least use a smart pointer to cure the potential leaks).

Thankfully no exceptions where thrown. And you are right it is an assignment, an array was required. Probably a better way to do this but it worked so I'm happy.

 

Now i need to figure out how to save the edits to a new file. I think I have it but I don't know what exactly to save. You can see here what I tried.

 

cout << "Enter a file name to save." << '\n';
			cin >> newname;
			ofstream OutFile(newname + type);
			OutFile << array << '\n';


Edit: Just figured it out.

 

Thanks guys. 

Edited by CookieMaster
Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, CookieMaster said:

Thankfully no exceptions where thrown.

Exceptions are thrown at runtime when some things go wrong, so you can't really say "Thankfully no exceptions where thrown", it depends on each run:

 

new can throw std::bad_alloc when the memory allocation fails.

std::string can throw std::bad_alloc when it's internal memory allocation fails.

std::string's assignment operator can throw, etc etc...

 

When any exceptions are thrown you leak all the allocated memory because the delete[]'s never run and the pointers are lost when the function exits. (And all those strings's destructors won't be called either, so their memory is leaked as well.)

That's not really a problem for a small program like this (if it throws, it'll terminate anyway because the exceptions are never handled), but it will be a problem in a bigger project and just plain bad practice. I'm just making sure you understand this because it can be hard to unlearn bad habits when you've grown used to them.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×