wchar_t user input with spaces - c++

Im currently using wchar_t for a username, however, I'm having an issue when the username contains a space when using std::wcout as it will just get the first word typed.
I am aware of getline for when your user input contains a space but I can't get it to work with the wchar_t. I don't suppose anyone could help or point me in the right direction?
Here is the code I have currently:
                
wchar_t window_title[50] = L"Krogex: ";                
std::cout << "Input window " << i + 1 << "s username: " << std::endl;
wchar_t username[20];
std::wcin >> username;

Here is how to use getline with a wstring; this will allow you to enter a username with spaces.
wchar_t window_title[50] = L"Krogex: ";
std::cout << "Input window " << i + 1 << "s username: " << std::endl;
std::wstring username;
std::getline(std::wcin, username);

Related

getline truncating input in buffer overflow control example

So I'm coming up with a simple solution to control and notify the user if a buffer overflow is detected.
First, the input char array is set up using a size variable, and then the getline function takes in input up to that size.
const int charMax = 20;
const std::string account_number = "CharlieBrown42";
char user_input[charMax];
std::cout << "Enter a value: ";
std::cin.getline(user_input, charMax);
Then I check and see if there is anything left in the buffer -- if so, I let the user know that buffer overflow was attempted, but only the right amount of input was read.
if ((std::cin.rdbuf()->in_avail() - 2) > 0) {
std::cout << "Buffer overflow was attempted, but input was automatically trimmed" << std::endl;
}
std::cout << "You entered: " << user_input << std::endl;
std::cout << "Account Number = " << account_number << std::endl;
However, when tested, the printed final user_input is always only 19 characters. This is problematic because I am supposed to be able to take in up to 20 characters before buffer overflow occurs ( > charMax). getline() is always truncating one character more than I need it to.
Where am I going wrong?

C++ won't print trailing whitespace

In my simple program to learn C++, I am asking the user their name and then greeting them.
#include <iostream>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!\n";
}
However, here is my result in CLion:
I expected the program to print out a trailing whitespace after the prompt for my name. However, it prints the space after I give the program input. I have only experienced this in CLion, but not in other IDEs. Why is this happening, and how can I fix this?
You need to flush your stream:
std::cout << "Enter your name: " << std::flush;
std::cout is a buffered stream which means that while your write to it isn't immediately written to the underlying device, but it is stored in a buffer. This is done for performance reasons. std::endl has an implicit flush operation, that's why you don't notice this if you always add a std::endl before you request input. Otherwise, like you've seen that can happen.

How do I extract characters in a specific position from an input string?

For my homework project I'm expected to create a program that asks the user their favorite city and which character they would like to display. The user inputs a number representing the position of the character within the city they would like to display, and the program is supposed to display the letter at this position.
We have not yet learned how to extract characters from a string, but this part of our project is supposed to show that we can properly google to find solutions for our coding. I have found a void function that would extract the character from a specific position for me, but am entirely lost on how to use it. I've tried several different methods and typed out every way I could possibly think to implement this function and it has not worked.
I've tried copying the example code I found online exactly as is (first example found at this address: https://www.geeksforgeeks.org/string-at-in-cpp/) but even the example would not run in visual studio 2017.
#include <iostream>
#include <string>
using namespace std;
void at(string);
int main()
{
//variables for favorite city & display character
string favCity;
int dispChar;
//asking user for favorite city
cout << "Input your favorite city: ";
cin >> favCity;
cout << "Which character would you like to display: ";
cin >> dispChar;
cout << endl << endl;
cout << "The user entered: " << favCity << endl;
cout << "The character at position " << dispChar << " is: " << at();
}
The expected result is that the computer will display "The character at position (dispChar) is: (whatever letter is at the user input position dispChar)"
EX: "The character at position 2 is: e //If the user input the city Detroit
I get the error that at is undefined, when I tried using str.at(); I would get str is undefined, etc.
There is no need of using an external function in order to extract a character from a string by its index. std::string itself implements an std::string::at function and also overloaded [] operator.
So two ways for doing that:
1.
cout << "The character at position " << dispChar << " is: " << favCity.at(dispChar);
2.
cout << "The character at position " << dispChar << " is: " << favCity[dispChar];
std::string::at can be used to extract characters by characters from a given string.
char& string::at (size_type idx)
string::at function returns the character at the specific position(idx). You can directly use string::at as you have included class.Learn More Here
So, in your solution you declared void at(string); therefore you need to define it too.
I have made some changes in your code I think that should do it.
#include <string>
using namespace std;
void extract_char(string str, int pos)
{
cout<<str.at(pos);
}
int main(void)
{
int dispChar;
string favCity;
cout<<"Input your favorite city: ";
cin>>favCity;
cout<<"Which position would you like to extract the character from(0 to size of city): ";
cin>>dispChar;
cout<<endl<<endl;
cout<<"The user entered: "<<favCity<<endl;
extract_char(favCity, dispChar-1);
/*
OR
cout<<"The character at position "<<dispChar<<" is: "<<favCity.at(dispChar-1);
*/
return 0;
}

Unicode chars shown as decimal numbers instead of symbols, how do I fix this?

I want to write a small program that is able to display unicode characters not included in ASCII or LATIN_1 using wchar_t.
I'm using C++14 and I've configured my text editor to store characters according to the UTF-8 standard. I've tried using both char16_t and char32_t but the result stays the same.
inside main()
wchar_t spade = L'\u2660';
wchar_t heart = L'\u2665';
wchar_t diamond = L'\u2666';
wchar_t clover = L'\u2663';
cout << spade << endl;
cout << heart << endl;
cout << diamond << endl;
cout << clover << endl;
The code above outputs the decimal values 9824 9829 9830 9827, instead of the unicode character symbols.
you need to use std::wcout to print Unicode characters
std::cout does not have any overloads of operator<< that accept wchar_t, char16_t or char32_t as input. So the compiler promotes those values to int, which is why you see numeric values outputted.
You need to use std::wcout instead of std::cout when outputting wchar_t data.
Alternatively, if your console supports UTF-8, you can use std::cout with UTF-8 strings, instead of wide (UTF-16/32) strings.
const char *spade = u8"♠";
const char *heart = u8"♥";
const char *diamond = u8"♦";
const char *clover = u8"♣";
cout << spade << endl;
cout << heart << endl;
cout << diamond << endl;
cout << clover << endl;

String and Cycle

Premise: I'm a newbie on C and C++, that's why I need your help.
Hello guys, I have a problem with strings and cycle on C++ (I'm pretty inexperienced):
I want to make a simple login screen, I made a struct with username, password etc and I decided that the username must be >3 characters and <15 characters, but I don't know how to do it, let me explain:
#include <iostream>
#include <cstdlib>
using namespace std;
struct login{
string userID;
string userPSW;
string userMAIL;
char userGENDER;
}login1;
cout << "Username: ";
cin >> login1.userID;
I made a an if/else, while and for, but when I try to compare a string with an int (like: login1.userID < 15), there is an error, I found some solution (like convert to int the string) but my code doesn't work.
So, can someone help me with this problem?
Another problem is while cycle, i tried with char userID , but the cycle keep spamming the cout << "error" that i made and it's not what i want, i want a cycle where there is an error if characters are <3 and >15 and cycle must return to "Username: ", with this method people can retry to insert an username.
I don't speak/write english very well, but I tried to get understood.
Thank you in advance guys.
STRING PROBLEM FIXED WITH YOUR HELP!
cycle problem still not solved :(
You cannot compare a std::string to a number. If you want to compare the length of the string, use .length():
cout << "Username: ";
cin >> login1.userID;
if(login1.userID.length() > 15)
{
cout << "please chose a shorter name." << endl;
You cannot compare a string to an int. If you want to compare the string's length then you can use the length() method. It will return the number of chars in the string that you can compare.
Like
string barString = "hello"
if(barString.length() > 15)
cout<< "string longer than 15 chars";