Put string into console, with ability edit [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 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.

Related

How can I add letters in a sentence? [closed]

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've asked to write code that gets a char array(sentence), if the there is an 'i' in the sentence I need to add the letter 'b' the letter 'i' again like this example:
pig -> pibig
I tried to use string.h functions but I didn't succeed to make it right.
Use std::string in string header file, and std::string::insert whenever you need to insert a char in string:
std::string my_string = "my satringa";
for (size_t i = 0; i < my_string.length(); ++i)
{
if (my_string.at(i) == 'a')
{
my_string.insert(i + 1, "b");
}
}
std::clog << my_string << std::endl;
Output:
> my sabtringab
If you are forced to use C-style strings, don't worry do all of your operations on std::string and then take the underlying stored string with std::string::c_str() as a C-style string (and don't forget to take a copy).

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

Making a password type program that needs to accept letter and number combinations? [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
I am making a shopping list program. For this program, I need to be able to type in a user input that accepts both number (1564, 121,1, etc) and word (hello, goodbye, etc) combinations. The program reads numbers just fine, but it cannot process words. Thank you in advance. The part of the code I am stuck with is below:
int code, option, count = 0;
double quantity, price, cost;
string description;
cin >> code;
while ((code != 123456789) && (count < 2))
{
cout << "Incorrect code, try again \n";
cin >> code;
count++;
if (count == 2)
{
cout << "max # of tries reached. Goodbye. \n";
system("pause");
}
}
Your code variable is now an int. If you wanted that to be a string, declare it so: std::string code;. Note that you might need to #include <string> in the very beginning. Also, if you want to compare it with numbers, either you call something like atoi() (string has .cstr()), or better yet, you might just compare it with "123456789". HTH.

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 detect spacebar? [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
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