Jump to content

Need some help - C++

CookieMaster
Go to solution Solved by CookieMaster,

Just another edit:

 

I will have to change the code around due to it now being an image(jpg) file instead of a readable text file. I'll post a new thread for future help. 

 

I have the file selector working, I will bounce off of everyone's code that was posted here to hopefully get the project working.

 

Thanks everyone

 

Very big thanks to you @Unimportant

Please scroll down to other posts, don't read this first op. 

 

 

 

 

Hi, I'm stuck on one part of my code. I want it to draw a horizontal and vertical line when given the choice through the image (please see code bellow) I have no clue how to even start it or do it. I also want in option 4 to save any changes made to the image from option 2 and 3. I will share you my cookies if you help me... well maybe that might be too pushy... 

 

Asc File: https://pastebin.com/n9iGn6kq

 

 

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

using namespace std;

int main()
{
	int response;
	int choice;
	int longestLine = 0;
	int arraySize = 0; // Used for array.
	string newname; // This is for the user to enter a new name when saving a file.
	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.
	string *array = nullptr; // Used for array.
	ifstream File(filename + type); // Load file and file type stated above.
	// The following is an array, the file is loaded into the array before any option is chosen. This is so option 1 and 4 can both occur. 
	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++;
		array = newArray;
	}
	// What the user sees.
	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)
	{
		for (int i = 0; i < arraySize; i++)
		{
			cout << array[i] << '\n'; // Display from array.
		}
		main();
	}

	if (response == 2)
	{
		cout << "Enter 1 for Vertical Line" << '\n';
		cout << "Enter 2 for Horizontal Line" << '\n';
		cin >> choice;
		
		if (choice == 1)
		{

		}

		if (choice == 2)
		{

		}

		else if (response > 0 || (response < 2))
		{
			cout << "Error, please choose 1 or 2!" << '\n';
		}
		main();
	}
		
	if (response == 3)
	{
		{
			for (int i = 0; i < arraySize; ++i)
				if (array[i].length() > longestLine)
					longestLine = array[i].length();
			cout << string(longestLine + 2, '*') << '\n';
		}

		{
			for (int i = 0; i < arraySize; ++i)
			{
				cout << '*' << setw(longestLine) << left << array[i] << "*\n";
			}
			cout << string(longestLine + 2, '*') << '\n';
		}
		main();
	}

	if (response == 4)
	{
		cout << "Enter a file name to save." << '\n';
		cin >> newname;
		ofstream OutFile(newname + type);
		{
			for (int i = 0; i < arraySize; i++)
			{
				OutFile << array[i] << '\n'; // Save from array. Also needs to save any changes made in option 2 and 3
			}
		}
		main();
	}

	if (response == 5)
	{
		exit;
	}

	else if (response > 5 || (response < 1))
	{
		cout << "Error, please choose a number 1-5!";
		main();
	}
	return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Your canvas is that string array, each string may be different length, so when you draw horizontal line you are changing only one line.

You will need to check if starting X position is within existing string length, and as you drawing each character, you check if you need to swap or add a character.

 

Examples:

Original line:
#########
123456789

Draw line that will fit inside original string length:
###-----#
123456789

Draw line that will start inside original string length:
#######-----
123456789012
000000000111

Draw line that will start after original string length:
#########   -----
12345678901234567
00000000011111111

When line fits inside original string then all you do is just changing characters.

When it exceeds original string length at some point you append instead of swapping, you can see that at position 10 so only 2 characters were swapped and 3 were added

When line starts beyond original string length you need to add space like in last example, where there 3 spaces were added and then line was drawn by appending line-character.

 

Drawing vertical line would be as you would draw N * vertical lines of length of 1 character, you need to check for every row if you can just swap character or if you need just append it, or if you need to add space and then append it.

Link to comment
Share on other sites

Link to post
Share on other sites

First thing you have to do is to understand that you can separate how the data is stored, read and written into the file, from how you actually store those characters in memory.

 

For example, you can make your program assume that the "image" will NEVER be more than 256 characters wide and it will never have more than 256 horizontal lines.

So, you can then create an array and set it to a fixed size that would allow for the maximum characters - call it char canvas[65535]  = 256x256 = 65536 , and that's how much that array can hold, because arrays start from 0. 

Now besides that array, you also need a variable that would memorize the actual number of lines - let's call it int LineCount, and you'd also want to memorize the width of each line so you could have a separate array for that, let's call it  int LineWidth[255];

 

So whenever you load an "image" from disk, you need to reset the LineCount to 0 because you don't know how many lines you'll read, set the LineWidth of each line to 0 because you don't know how many characters each line would have, and you'd have to reset the canvas to a neutral character ... so set all 65536 characters to space which is not visible on screen.

Now you can use functions like read() to read the whole text file in memory into an array. Here's some example code : http://www.cplusplus.com/reference/istream/istream/read/

You have the number of characters read from the text file, and you have all the characters stored into an array, so now you can simply go from the first character to the last and copy them into your canvas. You only have to pay attention to your ENTER characters which on Windows are actually made up of two separate characters CR (value=13) and LF (value=10) , and on Linux there's just LF (value = 10) .. so when you go from the first character to the last you simply ignore the CR character and if your character is LF you know you have to copy the following data into the next line of your canvas.

Here's some code (not sure if it's correct, writing it directly here on the forum.


 

char Canvas[65535];  // maximum 64k characters, max 256x256 characters

int LineWidth[255];  // our canvas has maximum 256 lines 0..255

int LineCount;

int i; // some temporary variables we reuse when we need one
int j;
int k;

LineCount = 0; // reset all information about the "picture"
for (i=0;i<256;i++) {
	LineWidth[i]=0;
}
for (i=0;i<65536;i++) {
	Canvas[i]=' ';
}
// here you open the file, read data and put it in an array char FileBufer[] , up to FileSize characters / bytes

// now we transfer the characters from the FileBuffer into our Canvas array and memorize the lengths of each line 
// and update the LineCount variable whenever we detect an ENTER (LF, 0x0A , 10 in decimal)
j=0;
for (i=0;i<FileSize;i++) {
	if ( FileBuffer[i] == 0x0D ) {
      // ignore CR character, carriage return , 0x0D in the ENTER combination
    } else {
      if ( FileBuffer[i] == 0x0A) {
        // new line, the next characters should be copied in the Canvas array on the next line
        LineCount++;
        j=0; // reset j to 0
      } else {
        LineWidth[LineCount]++; // increase the number of characters for that line
        Canvas[LineCount*256+j] = FileBuffer[i];
        j++;
      }
    }
}

When you want to save the "image" to disk, you basically open the file and then for each line in the "image", you write as many characters as memorized in LineWidth[line number] and if it's not the last line, you also add the ENTER characters.. something like this

You could use put() or write() functions for this:  http://www.cplusplus.com/reference/ostream/ostream/put/

for (j=0;j<=LineCount;j++) {
  if (LineWidth[j] > 0) {
    for (i=0;i<LineWidth[j];i++) {
      // print Canvas[j*256 + i] to disk
    }
  }
  if (j!=LineCount) { // this is not the last line of the image, so print ENTER before printing the next line
    // print CRLF , 0x0D and 0x0A , or just 0x0A - see the put() function : http://www.cplusplus.com/reference/ostream/ostream/put/
  }
}

 

So now when you create a horizontal line, you just have to be careful to update the LineWidth[line number] variable if you're going to draw the line more to the right than what was read from disk before.

Same, when you create a vertical line, if you go from top to bottom and say draw a vertical line 6 lines long, each time you draw a character, if the x position on a line is bigger than the value stored in LineWidth[line number] for that line you simply update the variable.

With my example, you can access any "pixel" of your image with the formula  Canvas[y*256+x]  or Canvas[j*256+i]  where i is the horizontal position and j is the vertical position can be from 0 to 255 (because in this example, I limited at the start the maximum width and height to 256.

ex if you want to draw a vertical line from (2,3 ) to (2,8) you say  for (y=3;y<=8;y++) { Canvas[y*256 + 2] = "#" ; if (LineWidth[y]<3) LineWidth[y]=3;  }  //3 because LineWidth holds number of characters, and (2,3) actually means 3rd character from left, 4th line because arrays work starting from 0

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 11.4.2017 at 11:00 PM, CookieMaster said:

Hi, I'm stuck on one part of my code. I want it to draw a horizontal and vertical line when given the choice through the image (please see code bellow) I have no clue how to even start it or do it. I also want in option 4 to save any changes made to the image from option 2 and 3. I will share you my cookies if you help me... well maybe that might be too pushy... 

 

Asc File: https://pastebin.com/n9iGn6kq

 

 

 

Seeing as you keep struggling, I've had a little 15 minute stab at it so you have something concrete to study:

#include <iostream>
#include <limits>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <cctype>

int 
Menu()
{
	std::cout << "\nWelcome to Tx Paint (Tunxis Paint)" << '\n';
	std::cout << "[1] Print image" << '\n';
	std::cout << "[2] Draw line in image" << '\n';
	std::cout << "[3] Draw box in image" << '\n';
	std::cout << "[4] Save image" << '\n';
	std::cout << "[5] Exit" << '\n';

	while (true)
	{
		int response;
		std::cin >> response;

		//std::cin >> response can fail, for example when the user enters text in stead 
		//of a number. Check for failure and reset stream if so...
		if (std::cin.fail())
		{
			//clear stream status...		
			std::cin.clear();
			//ignore the characters in the stream that caused the failure.
    			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');	
		}
		else
		{
			//check response for beeing a valid choice and return if so.
			if (response >= 1 && response <= 5)
			{
				return response;
			}
		}
		
		std::cout << "Invalid input, try again: ";
	}
}

std::vector<std::string>
LoadImage(const std::string& fileName)
{
	std::vector<std::string> image;
	std::ifstream file(fileName);
	int longestLine = 0;

	//only attempt to read if successfully opened file.
	if (file.is_open())
	{
		//keep getting lines as long as the stream is good.
		for (std::string line; std::getline(file, line); )
		{
			//remove those pesky control characters (newline, carriage return).
			line.erase(std::remove_if(line.begin(), line.end(), [](char ch){ return std::iscntrl(ch); }));
			if (line.length() > longestLine)
			{
				//Keep track of the longest line length we read so far.
				longestLine = line.length();
			}
			//store line in vector.
			image.push_back(line);
		}

		//extent all lines shorter then longestLine by appending spaces
		for (auto& line : image)
		{
			line.resize(longestLine, ' ');
		}
	}
	return image;
}

void
PrintImage(const std::vector<std::string>& image)
{
	for (const auto& line : image)
	{
		std::cout << line << '\n';
	}
}

void
DrawLine(std::vector<std::string>& image)
{
	for (auto& line : image)
	{
		line.insert(line.begin() + (line.length() / 2), '|');
	}
}

void
DrawBox(std::vector<std::string>& image)
{
	for (auto& line : image)
	{
		//insert * at begin and end of each line.
		line.insert(line.begin(), '*');
		line.insert(line.end(), '*');
	}	
	//insert sting filled with ***... at begin and end of image.
	const auto lineLength = image[0].length(); 
	image.insert(image.begin(), std::string(lineLength, '*'));
	image.insert(image.end(), *image.begin());	
}

bool
SaveImage(const std::vector<std::string>& image, const std::string& fileName)
{
	std::ofstream file(fileName);
	if (file.is_open())
	{
		for (const auto& line : image)
		{
			if (!(file << line << '\n'))
			{
				//write failed.
				return false;
			} 
		}
		return true;
	}
	//open failed.
	return false;
}

int 
main()
{
	const std::string fileName = "cookies_asc.txt";
	const std::string saveFileName = "cookies_saved_asc.txt";
	
	//load image and check vector size, if vector is empty loading failed...
	auto image = LoadImage(fileName);
	if (!image.size())
	{
		std::cout << "Loading failed!\n";
		return 1;
	}

	while (true)
	{
		switch(Menu())
		{
			case 1:
			{
				PrintImage(image);
				break;
			}

			case 2:
			{
				DrawLine(image);
				PrintImage(image);
				break;
			}

			case 3:
			{
				DrawBox(image);
				PrintImage(image);
				break;
			}

			case 4:
			{
				if (SaveImage(image, saveFileName))
				{
					std::cout << "saved as " << saveFileName << '\n';
				}
				else
				{
					std::cout << "save failed!\n";
				}
				break;
			}

			case 5:	
			default:
			{
				return 0;
			}	
		}
	}

	//Should never get here, but return 0 to shut up some dumber compilers complaining about no return value...
	return 0;
}

To keep things simple, I've refrained from using classes, exceptions, etc.

There's a single lambda in there to remove control characters from the lines, pretty straightforward for the rest, enjoy.

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/11/2017 at 6:38 PM, mariushm said:

First thing you have to do is to understand that you can separate how the data is stored, read and written into the file, from how you actually store those characters in memory.

 

For example, you can make your program assume that the "image" will NEVER be more than 256 characters wide and it will never have more than 256 horizontal lines.

So, you can then create an array and set it to a fixed size that would allow for the maximum characters - call it char canvas[65535]  = 256x256 = 65536 , and that's how much that array can hold, because arrays start from 0. 

Now besides that array, you also need a variable that would memorize the actual number of lines - let's call it int LineCount, and you'd also want to memorize the width of each line so you could have a separate array for that, let's call it  int LineWidth[255];

 

So whenever you load an "image" from disk, you need to reset the LineCount to 0 because you don't know how many lines you'll read, set the LineWidth of each line to 0 because you don't know how many characters each line would have, and you'd have to reset the canvas to a neutral character ... so set all 65536 characters to space which is not visible on screen.

Now you can use functions like read() to read the whole text file in memory into an array. Here's some example code : http://www.cplusplus.com/reference/istream/istream/read/

You have the number of characters read from the text file, and you have all the characters stored into an array, so now you can simply go from the first character to the last and copy them into your canvas. You only have to pay attention to your ENTER characters which on Windows are actually made up of two separate characters CR (value=13) and LF (value=10) , and on Linux there's just LF (value = 10) .. so when you go from the first character to the last you simply ignore the CR character and if your character is LF you know you have to copy the following data into the next line of your canvas.

Here's some code (not sure if it's correct, writing it directly here on the forum.


 


char Canvas[65535];  // maximum 64k characters, max 256x256 characters

int LineWidth[255];  // our canvas has maximum 256 lines 0..255

int LineCount;

int i; // some temporary variables we reuse when we need one
int j;
int k;

LineCount = 0; // reset all information about the "picture"
for (i=0;i<256;i++) {
	LineWidth[i]=0;
}
for (i=0;i<65536;i++) {
	Canvas[i]=' ';
}
// here you open the file, read data and put it in an array char FileBufer[] , up to FileSize characters / bytes

// now we transfer the characters from the FileBuffer into our Canvas array and memorize the lengths of each line 
// and update the LineCount variable whenever we detect an ENTER (LF, 0x0A , 10 in decimal)
j=0;
for (i=0;i<FileSize;i++) {
	if ( FileBuffer[i] == 0x0D ) {
      // ignore CR character, carriage return , 0x0D in the ENTER combination
    } else {
      if ( FileBuffer[i] == 0x0A) {
        // new line, the next characters should be copied in the Canvas array on the next line
        LineCount++;
        j=0; // reset j to 0
      } else {
        LineWidth[LineCount]++; // increase the number of characters for that line
        Canvas[LineCount*256+j] = FileBuffer[i];
        j++;
      }
    }
}

When you want to save the "image" to disk, you basically open the file and then for each line in the "image", you write as many characters as memorized in LineWidth[line number] and if it's not the last line, you also add the ENTER characters.. something like this

You could use put() or write() functions for this:  http://www.cplusplus.com/reference/ostream/ostream/put/


for (j=0;j<=LineCount;j++) {
  if (LineWidth[j] > 0) {
    for (i=0;i<LineWidth[j];i++) {
      // print Canvas[j*256 + i] to disk
    }
  }
  if (j!=LineCount) { // this is not the last line of the image, so print ENTER before printing the next line
    // print CRLF , 0x0D and 0x0A , or just 0x0A - see the put() function : http://www.cplusplus.com/reference/ostream/ostream/put/
  }
}

 

So now when you create a horizontal line, you just have to be careful to update the LineWidth[line number] variable if you're going to draw the line more to the right than what was read from disk before.

Same, when you create a vertical line, if you go from top to bottom and say draw a vertical line 6 lines long, each time you draw a character, if the x position on a line is bigger than the value stored in LineWidth[line number] for that line you simply update the variable.

With my example, you can access any "pixel" of your image with the formula  Canvas[y*256+x]  or Canvas[j*256+i]  where i is the horizontal position and j is the vertical position can be from 0 to 255 (because in this example, I limited at the start the maximum width and height to 256.

ex if you want to draw a vertical line from (2,3 ) to (2,8) you say  for (y=3;y<=8;y++) { Canvas[y*256 + 2] = "#" ; if (LineWidth[y]<3) LineWidth[y]=3;  }  //3 because LineWidth holds number of characters, and (2,3) actually means 3rd character from left, 4th line because arrays work starting from 0

 

 

On 4/12/2017 at 6:28 PM, Unimportant said:

Seeing as you keep struggling, I've had a little 15 minute stab at it so you have something concrete to study:


#include <iostream>
#include <limits>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <cctype>

int 
Menu()
{
	std::cout << "\nWelcome to Tx Paint (Tunxis Paint)" << '\n';
	std::cout << "[1] Print image" << '\n';
	std::cout << "[2] Draw line in image" << '\n';
	std::cout << "[3] Draw box in image" << '\n';
	std::cout << "[4] Save image" << '\n';
	std::cout << "[5] Exit" << '\n';

	while (true)
	{
		int response;
		std::cin >> response;

		//std::cin >> response can fail, for example when the user enters text in stead 
		//of a number. Check for failure and reset stream if so...
		if (std::cin.fail())
		{
			//clear stream status...		
			std::cin.clear();
			//ignore the characters in the stream that caused the failure.
    			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');	
		}
		else
		{
			//check response for beeing a valid choice and return if so.
			if (response >= 1 && response <= 5)
			{
				return response;
			}
		}
		
		std::cout << "Invalid input, try again: ";
	}
}

std::vector<std::string>
LoadImage(const std::string& fileName)
{
	std::vector<std::string> image;
	std::ifstream file(fileName);
	int longestLine = 0;

	//only attempt to read if successfully opened file.
	if (file.is_open())
	{
		//keep getting lines as long as the stream is good.
		for (std::string line; std::getline(file, line); )
		{
			//remove those pesky control characters (newline, carriage return).
			line.erase(std::remove_if(line.begin(), line.end(), [](char ch){ return std::iscntrl(ch); }));
			if (line.length() > longestLine)
			{
				//Keep track of the longest line length we read so far.
				longestLine = line.length();
			}
			//store line in vector.
			image.push_back(line);
		}

		//extent all lines shorter then longestLine by appending spaces
		for (auto& line : image)
		{
			line.resize(longestLine, ' ');
		}
	}
	return image;
}

void
PrintImage(const std::vector<std::string>& image)
{
	for (const auto& line : image)
	{
		std::cout << line << '\n';
	}
}

void
DrawLine(std::vector<std::string>& image)
{
	for (auto& line : image)
	{
		line.insert(line.begin() + (line.length() / 2), '|');
	}
}

void
DrawBox(std::vector<std::string>& image)
{
	for (auto& line : image)
	{
		//insert * at begin and end of each line.
		line.insert(line.begin(), '*');
		line.insert(line.end(), '*');
	}	
	//insert sting filled with ***... at begin and end of image.
	const auto lineLength = image[0].length(); 
	image.insert(image.begin(), std::string(lineLength, '*'));
	image.insert(image.end(), *image.begin());	
}

bool
SaveImage(const std::vector<std::string>& image, const std::string& fileName)
{
	std::ofstream file(fileName);
	if (file.is_open())
	{
		for (const auto& line : image)
		{
			if (!(file << line << '\n'))
			{
				//write failed.
				return false;
			} 
		}
		return true;
	}
	//open failed.
	return false;
}

int 
main()
{
	const std::string fileName = "cookies_asc.txt";
	const std::string saveFileName = "cookies_saved_asc.txt";
	
	//load image and check vector size, if vector is empty loading failed...
	auto image = LoadImage(fileName);
	if (!image.size())
	{
		std::cout << "Loading failed!\n";
		return 1;
	}

	while (true)
	{
		switch(Menu())
		{
			case 1:
			{
				PrintImage(image);
				break;
			}

			case 2:
			{
				DrawLine(image);
				PrintImage(image);
				break;
			}

			case 3:
			{
				DrawBox(image);
				PrintImage(image);
				break;
			}

			case 4:
			{
				if (SaveImage(image, saveFileName))
				{
					std::cout << "saved as " << saveFileName << '\n';
				}
				else
				{
					std::cout << "save failed!\n";
				}
				break;
			}

			case 5:	
			default:
			{
				return 0;
			}	
		}
	}

	//Should never get here, but return 0 to shut up some dumber compilers complaining about no return value...
	return 0;
}

To keep things simple, I've refrained from using classes, exceptions, etc.

There's a single lambda in there to remove control characters from the lines, pretty straightforward for the rest, enjoy.

Thank you both so much. They both helped me a lot. 

 

BTW, do you recommend using case over if response equals this number? 

 

Again thank you both!

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, CookieMaster said:

BTW, do you recommend using case over if response equals this number?

When there are more then a few possible values to test for a switch statement is preferred:

- It looks cleaner and is easier to follow then a bunch of if -else if's.

- For cases like in my example, where the case values are contiguous, there's a larger chance the compiler will optimize the switch block into a branch table/calculated jump.

Link to comment
Share on other sites

Link to post
Share on other sites

On 12.4.2017 at 0:38 AM, mariushm said:

For example, you can make your program assume that the "image" will NEVER be more than 256 characters wide and it will never have more than 256 horizontal lines.

So, you can then create an array and set it to a fixed size that would allow for the maximum characters - call it char canvas[65535]  = 256x256 = 65536 , and that's how much that array can hold, because arrays start from 0.

Noticed this a bit late but that's wrong:

char canvas[65535]; 
//Declares a array of 65535 elements, with indexes from 0 to and including 65534.
//Trying to access element 65535 is going out of bounds.

//What you need is:
char canvas[65536];
//Declares a array of 65536 elements, 0 - 65535.

 

Link to comment
Share on other sites

Link to post
Share on other sites

True, my bad (though it's not like in real world he'll actually paint a 256x256 picture to hit that problem but nevertheless it is one byte/char short).

 

There's probably a couple other small issues with the code, also related to arrays starting from 0, which probably wouldn't affect the functionality (but may make the code a bit more confusing) ... would probably be a good exercise in debugging and learning how arrays work when/if OP goes through the code and understands it ...

 

Like I said I wrote the code directly in the forum text box and didn't test it, and C++ is not my preferred programming language - i'm coding a lot in PHP which isn't that strict.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/19/2017 at 5:40 AM, Unimportant said:

When there are more then a few possible values to test for a switch statement is preferred:

- It looks cleaner and is easier to follow then a bunch of if -else if's.

- For cases like in my example, where the case values are contiguous, there's a larger chance the compiler will optimize the switch block into a branch table/calculated jump.

Thanks for all your help, I hate to ask but...

 

would you know how to transfer this to an app? I'm basically replacing the input numbers with buttons but I'm not sure where I would put certain things. Maybe you know (ps going off your code that you posted. (Also I just need  #1 and #2 and #5.)(Don't need options 3,4, translated)

 

It's about 6 am as i post this and I've been up all night figuring how to translate this to an app. I think just having things all over the place now and the fact that there is extra code for the ui... I just don't know. Hopefully you can help, thanks again. 

 

Edit: Also need to get that vector to an array, just a special request, working on final for class rn, code also requires using classes (Witch I don't see how you could use at least loading the image from array and displaying 

 

 

You can tell I'm just lost right now I have no clue how to start. :/

 

Rip sleep. 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, CookieMaster said:

would you know how to transfer this to an app? I'm basically replacing the input numbers with buttons but I'm not sure where I would put certain things. Maybe you know (ps going off your code that you posted. (Also I just need  #1 and #2 and #5.)(Don't need options 3,4, translated)

 

It's about 6 am as i post this and I've been up all night figuring how to translate this to an app. I think just having things all over the place now and the fact that there is extra code for the ui... I just don't know. Hopefully you can help, thanks again. 

 

Edit: Also need to get that vector to an array, just a special request, working on final for class rn, code also requires using classes (Witch I don't see how you could use at least loading the image from array and displaying 

 

Transfer to app ? I assume you mean GUI application ? What framework are you using ? Most frameworks like, for example Qt, would allow you to have a handler function for a event like a button click. One would simply remove the Menu function and the switch block. And each button-click event handler function would do what it's counterpart would have done in the switch block.

 

As for using classes, you could make the image itself a class, being able to load, save and print itself.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Unimportant said:

Transfer to app ? I assume you mean GUI application ? What framework are you using ? Most frameworks like, for example Qt, would allow you to have a handler function for a event like a button click. One would simply remove the Menu function and the switch block. And each button-click event handler function would do what it's counterpart would have done in the switch block.

 

As for using classes, you could make the image itself a class, being able to load, save and print itself.

Yes, sorry I did mean GUI application. I am currently using Qt, although I really like VS better but I guess this will be better. Anyways, the code for the button if I did set up the application correctly is as followed. 

 

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_load_clicked()
{
    
}

Please Note everything before Void MainWindow On_load_clicked was generated by the app default.

 

I guess my question is where to put the code, and for learning case lets just use response 1 as the load button (the only button in code currently)

 

I tried fooling around and putting the code in different spots but I always seem to fail. That's why I was up late. 

 

Also, getting rid of all the couts aka the main menu display for the options as this is a gui application. 

 

So I guess really my main question is what goes inside of the button click and what goes on the outside of it,

Thanks! 

 

Edit: I'm guessing that 

case 1:
               {
                   PrintImage(image);
                   break;
               }

goes into the button clicked event. 

Edit again: How would I get the button to trigger that response 1 was picked.

 

Another edit: I would use a clr in vs for this project but my only problem that the button click event likes to go inside the header instead of the source file, ffs 

 

I really wish this project didn't involve a GUI...

Link to comment
Share on other sites

Link to post
Share on other sites

Just another edit:

 

I will have to change the code around due to it now being an image(jpg) file instead of a readable text file. I'll post a new thread for future help. 

 

I have the file selector working, I will bounce off of everyone's code that was posted here to hopefully get the project working.

 

Thanks everyone

 

Very big thanks to you @Unimportant

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

×