Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am currently taking Intro C++ in my college. This was a very simple homework which I had to program using 3 different methods to output "1 2 3 4". Being ahead of my current skills, how can I add visual interfaces to this program that I just wrote? I mean, I want to change colors, add buttons or picture or whatever. Is that possible?
//Jaehyuk Oh
//Professor Kan, Shaobai
// 2/9/2014
// HWK. 2.17
// (Printing) Write a program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers separated bt one space. Do this several ways:
// a) using one statement with one stream insertion operation.
// b) using one statement with four stream insertion operators.
// c) using four statements.
#include <iostream>
int main()
{
std::cout<<"1 2 3 4\n"; // ------> a)
std::cout<<"1 "<<"2 "<<"3 "<<"4\n"; // ------> b)
std::cout<<"1 "; // ----------> c)
std::cout<<"2 ";
std::cout<<"3 ";
std::cout<<"4"<<std::endl;
system("PAUSE");
return 0;
}
C++, as a language, has no idea of what is a color or a button.
The standard input is just a stream of characters and the standard output is a stream of characters. Nothing fancy.
There are environments in which you can control a few the visual aspects (like the color of text) using just special control characters, "escape sequences", but they work only if you run the program in a terminal that supports these (e.g. a Linux terminal).
Running the program in an environment where these escape sequences are not interpreted would just confuse the output (e.g. in a Windows console).
I'll start by saying that you should only turn into your instructor what is expected of you, and nothing more. For example, if you use a GUI, you might end up implementing your above code in such a way that you don't use stream insertions. Since that is the whole point of the assignment, your professor might give you a 0 for not using stream insertions, regardless of whether you can do something more complex or not.
However, as a project in your free-time, you can look into WinForms. WinForms is a library in .Net that has many GUI elements built into it - such as buttons and various text views, and it is built in C++. As one commenter said, this is a broad question, but WinForms is a fairly easy-to-use API for GUIs in C++, so it's probably the most natural option to use, especially if you already have a Visual Studio compiler.
Since you have vs2010 express tagged, it sounds like you're set to give it a shot.
Writing GUIs can take a while to get used to, so don't be discouraged if you don't 'get it' immediately. There are good reasons why GUI programming isn't a typical intro to programming topic.
Good luck!
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I’m building a maze game in C++ and I’m trying to use shapes to identify characters in the game i.e. walls as squares, triangles as enemies and a star shape as the player. How can I print such elements in the console cmd? I have been researching a lot on http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073%28v=vs.85%29.aspx but I’m not able to figure it out.
If there isn’t a way then is it possible to print an svg?
There are several solutions to this problem, each with its own drawbacks and advantages.
Emoji (or more precisely Unicode)
This is my recommendation for simple games and shapes as it is the most simple to implement and relies on the underlying console to do the heavy lifting. Also how the other program was most likely written (though with other characters than listed here)
Since the windows terminal (and linux/macOS) supports Unicode, it is possible to use unicode characters such as 🔲, ⭐, and 🔺 to represent characters.
Note: you may need to enable utf8 support with setlocale(LC_ALL, "en_US.utf8"); in cpp
Pros
Simple to implement
Doesn't rely on external libraries
Cons
Confined to the standard text alignment
Hard to resize
Need to clear characters manually
What happens if the console is resized or too small?
ConPTY
Similar to the console approach, though this gives the advantage of more colors and the capability to set console size, etc. by the program.
Pros
Color support
Ability to control the terminal more simply
Cons
Still need to choose another method to actually print
Requires Windows 10
Console
This is takes control of the console windows. You should use a library such as https://github.com/Bill-Gray/PDCursesMod to abstract this method.
Pros
Capable of full control without leaving the terminal
Less resource intensive than a full window
Cons
Complex
Windows
You can use frameworks such as Qt and co to implement a window environment. This approach gives the most flexibility, though at the cost of complexity, with at least a couple months of development time (from scratch), though this issue can be overlooked with the proper framework/library.
Pros
More common nowadays
Most control
A number of libraries are available for games already.
Cons
Most resource intensive
Hardest to implement from scratch
Not terminal based (though you can make it seem as though it is)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am a beginner Clojure programmer.
In a Book, it is an advantage of the Clojure that the code that is not evaluated is data.
But I do not understand.
So, I want an example code and an explanation so I can understand.
I am Korean. For that reason, I would appreciate it if you could understand even if I wrote an awkward sentence or failed to keep my manners.
I think the question you are asking is: why is it an advantage for a programming language that its source code is available within the language as data (and in particular as structured data, not just as strings)?
The reason it is an advantage is simple: once you have access to a data structure representing the source of a program you can manipulate that structure: you can write programs that manipulate other programs.
A particularly good case is where the surface form of the language and the data structure which represents it is rather 'low-commitment': it doesn't encode much meaning of the program, just its syntactic form.
Then you can write programs in this low-commitment language which include constructs which don't yet exist in the language. And you write other programs which take these programs and turn them into other programs which only use constructs which already exist in the language.
And you can keep doing this. So you can start with a language which is whatever language you are given, and incrementally build it into a language which is the language you want.
Of course you can do this with almost any language: in almost any language that lets you read files into strings, parse those strings into some representation of a language, with extensions, and then process that representation into the original language which you then hand off to the compiler or interpreter.
But Lisp-family languages make this much easier for you:
they do the parsing-into-a-data-structure for you, so 'source code is data';
they have an intentionally low-commitment source form, leaving it up to you what given constructs mean;
they provide facilities to let you do this processing-of-source-code-into-other-source-code in a fairly painless way, by defining macros.
That's the advantage of having source code available as structured data in the language: it's a crucial part of the things you need to make implementing your own programming language (which is often really a programming jargon – a language which inherits all of a base language but adds something of its own on top of it) easy.
I think it is in "Mastering Clojure Macros" that I saw an example of "code is data" that made a lot of sense (at least to me).
Syntactically speaking this is valid Clojure code: it's a simple list of numbers and symbol.
(1 + 1)
But it is not a valid program! If you evaluate this in your REPL it will throw an error because 1 isn't a function.
When Clojure reads this text, it produces a list (the same list) and allows things such as a macro to receive it (unevaluated), transform it and send it back. Perhaps that macro could simply swap the first two elements and return (+ 1 1)?
This would not have been possible in JavaScript for example:
var a = + 1 1; // Syntax error
The engine would have exploded way before you could try anything!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I just started to get into some basics of C++, but there were two things I've realized:
The difference between Macros and Functions is that macros are preprocessed while Functions are compiled afterwards
In every code I've read (also some with defined macros), no one ever defined a macro for a console output
Now my question is, should you even do such a thing?
Without Macro:
//...
cout << "This is a test";
cout << "This is another test";
cout << "This is a third test" << " with two strings in one";
//..
With Macro:
#define OUTP(x) (cout << x)
//...
OUTP("This is a test");
OUTP("This is another test");
OUTP("This is a third test" + " with two strings in one");
//...
First, I can't see a big difference, except I don't get so confused with the macro, cause normally "<<" is bitshifting, but here it's used for insertion, while "OUTP("Test")" looks better for me, as it works like a normal method.
But as I've never seen someone using that, is there a reason behind that?
"Better" is a really vague term, it really depends on what you are trying to achieve.
Every implementation decision has its pros and cons, so it is really up to the single project.
From what I can see, the pros of abstracting the output behind a macro could be:
Easier way to swap out output method in favor of more manageable logging structures in case the need comes
less typing
more uniformity if the macro takes charge in adding some common formatting options
Cons could be:
you lose flexibility, as inevitably the macro will not allow some operations that directly using cout would have allowed
you add a layer of indirection and get away from a common practice of the language. People with experience in the language will pick up cout faster, otherwise they will have to look for the content of the macro and get used to it.
There are surely other reason for or against such a choice, but in the end I think there is no right choice.
P.S.
so confused with the macro, cause normally "<<" is bitshifting, but here it's used for insertion,
While surely a good reason, I suggest you look at it the other side. "bitshifting" operator for output is a pattern common in C++, so you should start being a little more flexible in how you interpret the operator due to operator overloading
I am using macros for outputs. It has certain advantages over conventional output, namely:
You can change lately all outputs at one place - at macro definition. For example, if you want to prefix all outputs with a timestamp, with macro employment you need to change only your macro once.
You can introduce output level, like DEBUG or INFO or ERROR. Depending on the given output level (e.g. ERROR), your matching outputs will be compiled (e.g. ERROR and FATAL_ERROR) and performed, all other irrelevant outputs (e.g. DEBUG) will be even not compiled and not included into the binary code. Moreover, many IDEs will even grey out irrelevant code lines.
You can also introduce variable number of input arguments, code line number, date and time of compilation and so in macros. And even employ mutex for multi-threading output, since cout << "first" << "second"; is not thread-safe (another thread can output something between your "first" and "second" strings).
etc. There are plenty articles in Internet about this topic.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm sorry if this is long. I feel I need to be very thorough as to where I am actually at.
Right so first off I do have experience with JavaScript.
Which is not the same thing, But it sure looks familiar to me.
I want to make music with C.
I got his big book called "The audio programming book".
It was like $60.
It dose have an introduction to C in it. It all makes good sense to me so far.
nothing really new.
So here's my problem...
And its very simple. So simple in-fact that it doesn't seem to be covered in the book.
I don't understand the relationship between my program and the speakers.
my computer's audio device.
I thought to myself "Alright lets start with the very basics. Let's see if we can just make a noise."
And so...
I have something that will make a beep.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
printf("\a\n");
return 0;
}
So that's cute.
But that's seemingly all that function is capable of.
And honestly That would be rather confusing if... the thing that is meant for printing text to your screen... was also the thing you use to talk to your speakers....
I've been looking through the book. I've been looking online. I've been looking for hours.
And clearly searching in all the wrong places... because literally all I can find is more of how to make your computer beep...
where is the "send to speaker"??
where do I put in the frequency?
Heaven forbid the part were I tell the thing what device I would like to talk to.
Dose C simply not have any built-in functionality for sending signals to the speakers?
Do I need a Library? Do need C++? What am I missing.
I don't know anything about desktop applications.
All my experiences is with Internet technologies.
Dose C simply not have any built-in functionality for sending signals to the speakers?
Indeed, C does not have any built-in functionality for sending signals to the speakers.
Do I need a Library?
No, but I would highly recommend to use one.
Do need C++?
No. You don't need C++ for doing anything in C. Besides, C++ also has no built-in audio functionality.
where is the "send to speaker"?? where do I put in the frequency? Heaven forbid the part were I tell the thing what device I would like to talk to.
All these and other things related to audio are platform (operating system) specific. In order to interact with a sound card (which is the device that sends signals to the speakers), you must use platform specific API. Some operating systems may have multiple different APIs for audio. You can, as I already recommended, use a (cross-platform) library that abstracts the platform specific API.
You say you want to "make music with C". This seems to imply that you want to be able to feed data into the sound buffer in real-time through something like an ASIO driver, with low-lantency, and that you want to write your own audio synthesis. This is all quite complicated stuff, but you should start by getting some library/API that gives you that kind of access. Sadly, most of them do not tend to be free, but there probably are some free options, if you look around.
Since all of this depends so much on what library/API you use, I sadly cannot say anything more precise.
Another option is to study up and learn how to program VST-instruments (plug-ins for DAWs), which will probabably be even more useful for what it sounds like you're trying to do.
And no, you don't need C++, unless the library/API you want to use only supports C++, for some reason. That doesn't tend to be the case.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to provide a binary-only program written in C or C++ where the user has to pass a valid code via command line to be able to use some extra features of the program itself. The idea is to implement some verification strategy in the program which compares the passed code against a run-time generated code which univocally identifies the system or hardware on which the program is being run.
In other words, if and only if the run-time check:
f(<sysinfo>) == <given code>
is true, then the user is allowed to use the extra features of the program. f is the function generating the code at run-time and sysinfo is an appropriate information identifying the current system/hardware (i.e. MAC address of the first ethernet card, Serial Number of the processor, etc..).
The aim is to make it as much difficult as possible for the user to guess or (guess the way to calculate) a valid code without knowing f and sysinfo a priori. More importantly, I want it to be difficult to re-implement f by analyzing the disassembled code of the program.
Assuming the above is a strong strategy, how could I implement f in C or C++ and what can I choose as its argument? Also what GCC compiler flags could I turn on to obfuscate f specifically? Note that, for example, things like MD5(MAC) or MD5(SHA(MAC)) would be too simple for evident reasons.
EDIT: Another interesting point is how to make it difficult for the user to attack the code directly by removing or bypassing the portion of the code doing the check.
If you are on Windows, a standard strategy is to hash the value of the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid
If you're worried that a user might "guess" the hash function, take a standard SHA256 implementation and do something sneaky like change the algorithm initialization values (one of the two groups of these uses binary representations of the cube roots of the primes to initialize - change it to 5th or 7th or whatever roots, starting at the nth place, such that you chop off the "all-zero" parts, etc.)
But really if someone is going to take the time to RE your code, it's much easier to attack the branch in the code that does the if (codeValid) { allowExtraFeatures(); } then to mess with the hashes... so don't worry too much about it.