cout

How to format text using std:out setfill std::setw std:right with one padding space

萝らか妹 提交于 2019-12-12 15:15:13
问题 I just want to format a string and an integer value with right justify. There is no problem to do this without leading space before the integer value. bytes.....................123981 total bytes..............1030131 But it should look like this: bytes ................... 123981 total bytes ............ 1030131 Unfortunately the example below wont work, because setw (right justify) relates only to the next stream element. int iBytes = 123981; int iTotalBytes = 1030131; cout << setfill('.');

PAT乙级真题 1006 换个格式输出整数 C++实现

六月ゝ 毕业季﹏ 提交于 2019-12-12 13:09:49
题目 让我们用字母 B 来表示“百”、字母 S 表示“十”,用 12…n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 3 位的正整数。例如 234 应该被输出为 BBSSS1234,因为它有 2 个“百”、3 个“十”、以及个位的 4。 输入格式: 每个测试输入包含 1 个测试用例,给出正整数 n(<1000)。 输出格式: 每个测试用例的输出占一行,用规定的格式输出 n。 输入样例 1: 234 输出样例 1: BBSSS1234 输入样例 2: 23 输出样例 2: SS123 思路 ASCII码共128个字符,需记住几个常见字符的ASCII码: 回车:13 空格:32 “0”:48; “A”:65; “a”:97; 数字字符ch转换为数字可以由ch-48得出。 附常用符号的ASCII码: 算法 # include <iostream> using namespace std ; int main ( ) { string str ; cin >> str ; if ( str . size ( ) == 3 ) { int n = str [ 0 ] - 48 ; for ( int i = 0 ; i < n ; i ++ ) { cout << 'B' ; } } if ( str . size ( ) >= 2 ) { int n = str [

How to get rid of std::cout quickly

跟風遠走 提交于 2019-12-12 06:04:16
问题 I have inherited a large codebase where std::cout is very frequently used with the purpose of printing out debugging information. There is no real debug/log functionality in the code and since this will be a short lived task (bugfix) implementing debug/log functionality for now is out of the question. It really bothers me the amount of text this program outputs, and at least till I have to work on it I would like to disable all the printouts on a temporary basis. I see two solutions for now:

Change color of cout text C++

十年热恋 提交于 2019-12-12 04:47:27
问题 Im trying to change the color of some of my cout outputs but after that cout to be again the same color. Ive tried the following code: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4); cout << "Help-Please" << endl; but it continues even after the cout has been completed. Also to add that some of these color changes will be inside if statements. What code would you recommend me to add to to get this effect? 回答1: The text color stays in effect, you must call SetConsoleTextAttribute

Xcode std::cout output acting odd

非 Y 不嫁゛ 提交于 2019-12-12 04:34:33
问题 I've been using Xcode for 2 years and have written quite a few C++ command line applications. For the past few months, I've abandoned command line applications and have focused on GUI applications, but after coming back to some older command line applications, I noticed that std::cout isn't working the way I last remember. If I have a cout statement such as "enter age: " before a cin statement, when running the app in Xcode, it no longer shows the cout statement first, but rather after the

Reverse order when using std::cout [duplicate]

和自甴很熟 提交于 2019-12-11 19:25:01
问题 This question already has answers here : cout << order of call to functions it prints? (3 answers) Parameter evaluation order before a function calling in C [duplicate] (7 answers) Closed 6 years ago . The program below outputs 10. I expected it to first print 0 (the else branch of function f) and then to print 1. How come that the order is reversed? #include <iostream> using namespace std; int f(bool& b){ if (b==true){ return 1; } else { b=true; return 0; } } int main () { bool b=false; cout

Taking input over standard I/O in multithreaded application

ε祈祈猫儿з 提交于 2019-12-11 11:57:30
问题 I have a question about input/output, or basically interaction with the user, in multithreaded applications. Let's say I have a program that starts three threads and waits for them to end, then starts them again int main() { while(true) { start_thread(1); start_thread(2); start_thread(3); //.... join_thread(1); join_thread(2); join_thread(3); } } Every thread also outputs data over cout . I'm looking for a way to take input by the user ( cin ) without stopping / hindering the main loop in it

Weird Error in C++ Program: Removing Printout Breaks Program

不羁岁月 提交于 2019-12-11 08:34:24
问题 This is a very strange problem... removing the cout in the function below causes it to stop printing the correct/expected results and printing garbage values. (i.e. it still RUNS the data it outputs, though, is wrong). Any ideas? bool extract_tension(std::vector<double> &interfacial_tension_trap, std::vector<double> &interfacial_tension_simp, const std::string data, const unsigned int num_slabs, const double z_min, const double z_max) { //start @ first number unsigned int start = 17; unsigned

XCode 4 only displaying cout statements with endl

谁说我不能喝 提交于 2019-12-11 06:17:59
问题 I have a really weird issue with my cout statements. I've only tried this out with XCode 4. For instance, if I write, cout << "this works" << endl; cout << "this doesnt"; cout << memorySizeLimit << " blocks of memory available." << endl; I see all three output statements in my debugger console. However, if I change the order to, cout << memorySizeLimit << " blocks of memory available." << endl; cout << "this works" << endl; cout << "this doesn't"; I only see the first two couts. Even stranger

Showing WideString variable into console

。_饼干妹妹 提交于 2019-12-11 05:25:18
问题 I'm having problems showing a WideString into the console. I'm totally new on Builder C++ and C++ in general. Not sure if I need some headers or maybe the values shown when debugging could help. It seems that when doing wcout << s; it is showing the address instead of the "wchar array". Here it is my code: //--------------------------------------------------------------------------- #include <iostream.h> #include <vcl.h> #include <string> #include <wstring.h> #pragma hdrstop //---------------