How detect spacebar? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program in c++ where I have to detect spacebar, how can I do? I see that I need the function getch(), but in my program I haven´t conio.h. Exist other solution?
With getchar I need press intro, exist other form that I press only spacebar?
For example, can I introduce a intro without press intro???

Simple Example
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
//code
}
while(getchar() != 32 || getchar() != ' ');
cout << "Space pressed" << endl;
return 0;
}
Compiled Code
Windows.h:
if(GetAsyncKeyState(VK_SPACE) & 0x80000000)
MessageBox(NULL, "Spacebar pressed!", "TEST", MB_OK);
See no conio.h

Related

how to write correctly into multidimentional char array unknown amount of values but fixed amound of chars [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm sick and tired of solving why my ch[0] is of value "Thomas EdisonÇ#", when it should be "Thomas Edison"
int main(){
using namespace std;
ifstream in("U2.txt");
int n;
in>>n; //n=rows, so in every line there will be "name surname", time, money
char ch[n][21]; //I'm trying to get Name+Surname which must be 20 char long
in.read(ch[0], 20);
cout << ch[0]; //but getting Thomas EdisonÇ#
return 0;}
It works on one dimentional ch[21], but there's gonna be lots of values so I want to use ch[n][21]
Any other out of my box solution is welcome, I'm tired
You are forgetting that C strings need to be nul terminated
in.read(ch[0], 20);
ch[0][20] = '\0'; // add the nul terminator
cout << ch[0]; // now correct output

Just input a number in cpp [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm need to create id just number. For example: 12345678
If user inputs fail( contain char), delete char immediately.
For example: 123a =>input gain!
Please help me!
I think this is what you are looking for:
#include <iostream>
#include <string>
#include <cctype>
int main () {
std::string input;
bool valid;
do {
valid = true;
std::cin >> input;
for (char c : input)
if (! std::isdigit( static_cast<unsigned char>(c) ) )
valid = false;
} while (! valid);
// Here the string is guaranteed to be valid
}
Be aware though, that whatever you are trying to do, this does not look like the proper way to do it. There are ways to read numbers in c++, and this is not what I would recommend.

Put string into console, with ability edit [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What function in C++ puts string into console, with ability edit? I have such state of console:
After work of needed function I want to see this:
but not this:
It cannot be done natively on the terminal, you have to do it in your control flow.
A little example
string text("Hello, World")
cout << text;
char x = getch();
while (x != '\n') { //loop breaks if you press enter
if (x == 127 || x == 8) { //for backspace(127) and delete(8) keys
cout << "\b \b"; //removes last character on the console
text.erase(text.size() - 1);
}
else {
cout << x;
text.append(x);
}
x = getch();
}
"\b" is nondestructive backspace. i.e it moves the cursor backwards but doesn't erase.
"\b \b" is destructive backspace.

Accessing data in a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?
Simply:
#include <fstream>
#include <iostream>
int main()
{
std::fstream file("table1.txt");
std::string word;
while (file >> word)
{
// do whatever you want, e.g. print:
std::cout << word << std::endl;
}
file.close();
return 0;
}
word variable will contain every single word from a text file (words should be separated by space in your file).

how to create a new file in c++ using windows.h [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to know if there are any methods other than file.open(); in <windows.h>
As you specifically state windows.h, the WINAPI function CreateFile() can used to create a file. At the end of the link there are multiple examples of using CreateFile(), but here is a simple one:
#include <windows.h>
#include <iostream>
int main()
{
HANDLE h = CreateFile("test.txt", // name of the file
GENERIC_WRITE, // open for writing
0, // sharing mode, none in this case
0, // use default security descriptor
CREATE_ALWAYS, // overwrite if exists
FILE_ATTRIBUTE_NORMAL,
0);
if (h)
{
std::cout << "CreateFile() succeeded\n";
CloseHandle(h);
}
else
{
std::cerr << "CreateFile() failed:" << GetLastError() << "\n";
}
return 0;
}