Display chinese characters in console application - visual-studio-2012

I have the following console app in Visual Studio
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
wcout << "displayStringsTest_data" << endl;
wcout << L"列举" << endl;
wcout << "Done" << endl;
return 0;
}
But the Chinese characters are output as question marks
Does anyone know how I can get them output in the correct form
Thanks

Even though you are sending correct Chinese symbols to console.
Windows setting should be changed so that it supports those language specific characters.
You need to change PC language to Chinese from "Region and Language" section from control panel, which is by default to US English.

Related

How to extract numbers correctly from an input file in command line argument

I am attempting use ifstream to extract two numbers from a file in argv[1], named "inputFile", and the extracting operator seems to be extracting the bits of code rather than the numbers needed.
inputFile.txt was put into the command line operator by right clicking the project, going to properties -> debugging -> command arguments -> typing inputFile.txt into command arguments in visual studio 2017.
The file inputFile.txt is as below:
1 2
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
//Test opening file
cout << "Input file: " << argv[1] << endl;
ifstream in(argv[1]);
if (!in)
{
cerr << "Unable to open " << argv[1] << " for input";
return 1;
}
//extract numbers
int num1;
int num2;
in >> num1 >> num2;
cout << num1 << endl << num2 << endl;
in.close();
return 0;
}
I expect the int num1 to hold 1, and the int num2 to hold 2, but instead each variable holds the number -858993460.
Are you sure the file you are reading has got the data you expect? This code works fine for me, compiling with Visual Studio 2005. But, If I change the contents of the file, writing not numbers (for example if I write: a b), num1 and num2 ends with -858993460.

Visual Studio 2017 C++ issue with string [duplicate]

This question already has answers here:
Why isn't cin >> string working with Visual C++ 2010? [closed]
(2 answers)
Closed 5 years ago.
This is my code (created in Visual Studio 2017):
#include<iostream>
using namespace std;
int main() {
string ime;
int visina;
double tezina;
cout << "Unesite ime:" << endl;
cin >> ime;
cout << "Unesite visinu (cm):" << endl;
cin >> visina;
cout << "Unesite tezinu (kg):" << endl;
cin >> tezina;
return 0;
}
Afther the compiling process I got a this message:
no operator ">>" matches these operands
This message corresponding with this line of code: cin >> ime;
What is wrong in this code?
You should
#include <string>

Getline doesn't work when push Enter

I have a very strange problem. Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\n');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
This code doesn't end when I push Enter at all; therefore there is no way to complete the input. The output will stop like this and wait:
what is your name?
However if I change '\n' to 'p' or whatever char, it will finish the input when the specific char is input. For instance:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\p');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
Screen:
what is your name?
hello, Frank, how are you today?
RUN SUCCESSFUL (total time: 2s)
PS:
I am using NetBeans IDE 8.0 and Windows 8 Pro x64
New Line character depends on the operating system.
Reminder - \r\n or \n\r?
I would leave out the third parameter of the getline call all together. This will use getline's default behavior and terminate the read on the enter key.
EDIT: After reading you are still having problems, I decided to run some tests on your code.
First, to confirm that the default behavior works without specifying the third parameter to the getline method.
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
while(true){
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name);
cout << "hello, " << user_name << ", how are you today?" << endl;
}
}
The output:
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
Second, to test manually specifying the escape sequence '\n' i did this:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
while(true){
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\n');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
}
And the output:
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteBoardDev
hello, WhiteBoardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
compiled on g++ w/osx so i didnt bother testing the '\r\n' case. This brings up a good point about cross platform compatibility. If you want your code to work in multiple environments try to not specify anything platform specific in your code (aka the EOL sequence) and leave out that third parameter entirely.
Now I have solved my question with the help of WhiteboardDev. In my coding environment, I should use '\r' and during the input I need push Enter and Space, two keys in order to complete the it.

c++ programming

I am writing a program that calculates and prints parking charges i am using get.line for the user to enter, how do i use "find" to separate the line?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main ()
{
string enter_date;
string enter_time;
string exit_date;
string exit_time;
int calculatecharge;
int num;
cout << "Please enter the date and time the car is entering "<< endl
<< "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin,enter_date);
getline (cin,enter_time);
cout<< "Please enter the date and time the car is exiting "<< endl
<< "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin,exit_date);
getline (cin,exit_time);
find(' ')
cout<<"Parking fee due: "<< num << endl;
return 0;
}
If you are using getline() twice, try the below code.
getline (cin, enter_date, ' ');
getline (cin, enter_time);

Can not print out the argv[] values using std::cout in VC++

This is my first question on the site even though i have been coming here for reference for quite some time now. I understand that argv[0] stores the name of the program and the rest of the commandline arguements are stored in teh remaining argv[k] slots. I also understand that std::cout treats a character pointer like a null terminated string and prints the string out. Below is my program.
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << argv[0] << " ";
cout << argv[1] ;
return 0;
}
According to all the other programs I have seen over my internet search in the issue, this program should printout two strings viz. name of the program and the commandline arguement. The console window shows
0010418c 001048d6
I believe these are the pointers to argv[0] and argv[1] resp.
The only commandline arguement I have is "nanddumpgood.bin" which goes in argv[1] and shows the strings correctly if I mouseover the argv[] arrays while debugging.
Whis is this happening? What am I doing wrong? I understand, arrays decay to pointers in special cases? Is this a case where it doesnt?
I also understand that std::cout treats a character pointer like a null terminated string and prints the string out.
That's mostly correct. It works for char*, but not other types of characters. Which is exactly the problem. You have a _TCHAR*, which IS char* on an ANSI build but not on a Unicode build, so instead of getting the special string behavior, you get the default pointer behavior.
I understand, arrays decay to pointers in special cases? Is this a case where it doesnt?
argv is an array, but neither argv[0] nor argv[1] are arrays, they are both pointers. Decay is not a factor here.
The simplest fix is to use int main(int argc, char* argv[]) so that you get non-Unicode strings for the command-line arguments. I'm recommending this, rather than switching to wcout, because it's much more compatible with other code you find on the internet.
Use wcout for Unicode strings.
I guess you are compiling your application with the unicode compiler switch which treats all TCHAR as wchar_t. Therefore cout treats argv as an int.
Write instead
wcout << argv[0] << L" ";
wcout << argv[1] ;
or change to Use Multi-byte character set in the Project settings/General.

Resources