cout

Why does scanf appear to skip input?

匆匆过客 提交于 2019-11-29 10:52:59
I am confused about scanf's behaviour in the following program. scanf appears to input once, and then not input again, until a stream of characters is printed. Below in a C program #include<stdio.h> int main() { int i, j=0; do { ++j; scanf("%d", &i); printf("\n\n%d %d\n\n", i, j); } while((i!=8) && (j<10)); printf("\nJ = %d\n", j); return 0; } here, Till i am inputting any integer program works perfectly fine, but when a character is inputted it goes on printing the last inputed value of i and never stops(untill j is 10 when loop exits) for scanf to take next input. output:: 1 <-----1st input

How to print '\\n' instead of a newline?

孤者浪人 提交于 2019-11-29 10:42:23
I am writing a program that uses prints a hex dump of its input. However, I'm running into problems when newlines, tabs, etc are passed in and destroying my output formatting. How can I use printf (or cout I guess) to print '\n' instead of printing an actual newline? Do I just need to do some manual parsing for this? EDIT: I'm receiving my data dynamically, it's not just the \n that I'm corned about, but rather all symbols. For example, this is my printf statement: printf("%c", theChar); How can I make this print \n when a newline is passed in as theChar but still make it print normal text

c++, cout and UTF-8

為{幸葍}努か 提交于 2019-11-29 10:10:18
Hopefully a simple question: cout seems to die when handling strings that end with a multibyte UTF-8 char, am I doing something wrong? This is with GCC (Mingw) on Win7 x64. **Edit Sorry if I wasn't clear enough, I'm not concerned about the missing glyphs or how the bytes are interpreted, merely that they are not showing at all right after the call to cout << s4 (missing BAR). Any further cout s after the first display no text whatsoever! #include <cstdio> #include <iostream> #include <string> int main() { std::string s1("abc"); std::string s2("…"); // … = 0xE2 80 A6 std::string s3("…abc"); std

Difference between “endl” and “\n” [duplicate]

天涯浪子 提交于 2019-11-29 09:34:26
问题 Possible Duplicate: C++: “std::endl” vs “\n” I'm wondering if there is any significant difference between these two ways to print newline : cout << endl; //approach1 cout << "\n"; //approach2 Is there any practical difference? 回答1: Yes, they're different. "\n" is just a string of length 1 that gets appended to stdout. std::endl , instead, is an object that will cause to append the newline character ( "\n" ) AND to flush stdout buffer. For this reason it will take more processing. 来源: https:/

Using the console in a GUI app in windows, only if its run from a console

▼魔方 西西 提交于 2019-11-29 07:53:05
My application is a GUI app that has helpful (though optional) information through the terminal (via cout). In Windows I either have a console appear (by compiling as a console app, or allocating it dynamically) or I don't. My intention is to make use of the console IF it is being run from the console, but ignore the console completely if it was not. (Essentially what happens in Linux and OS X). I do not wish to redirect to a file (and in the case of using cin, this is not a viable solution anyway). Is there a way to attach a GUI app in Windows to the console it is run from, if and only if it

How to disable cout output in the runtime?

好久不见. 提交于 2019-11-29 06:27:06
问题 I often use cout for debugging purpose in many different places in my code, and then I get frustrated and comment all of them manually. Is there a way to suppress cout output in the runtime? And more importantly, let's say I want to suppress all cout outputs, but I still want to see 1 specific output (let's say the final output of the program) in the terminal. Is it possible to use an ""other way"" of printing to the terminal for showing the program output, and then when suppressing cout

关于std::ios::sync_with_stdio(false)

时光总嘲笑我的痴心妄想 提交于 2019-11-29 06:20:00
很多C++的初学者可能会被这个问题困扰,经常出现程序无故超时,最终发现问题处在cin和cout上,(甚至有些老oier也会被这个问题困扰,每次只能打scanf和printf,然后一堆的占位符巨麻烦),这是因为C++中,cin和cout要与stdio同步,中间会有一个缓冲,所以导致cin,cout语句输入输出缓慢,这时就可以用这个语句,取消cin,cout与stdio的同步,说白了就是提速,效率基本与scanf和printf一致。然后就可放心的使用cin,cout了。(不过实际上使用了using namespace std;之后就可以直接打ios::sync_with_stdio(false);了) 今天遇到有人问问题说关闭流同步以后会炸空间,我也很诧异。然后看了下代码,问题出在scanf()。取消流同步以后,stdio中带有的scanf()和printf()输入输出的内部同步也会被取消(大概是这样的,如果有误请联系博主更正),这时候再用scanf()和printf()就可能会出玄学错误,所以用的时候也要注意。   另外,如果使用文件输入输出的话,一定记住要把这条语句放在freopen()后面,反正也会出西西,但是具体问题博主也不太清楚。。。 转至:www.cnblogs.com/cytus 来源: https://www.cnblogs.com/cunard/p/11458759

4、string类

旧巷老猫 提交于 2019-11-29 06:18:23
1、使用方法 必须包含#include<string>及using namespace std; 1 char charr1[20]; 2 char charr2[20] = "gao"; 3 string str1; 4 string str2 = "yixue"; 5 cout << "enter charr1:" << endl; 6 cin >> charr1; 7 cout << "enter str1:" << endl; 8 cin >> str1; 9 cout << charr1 << " ,"<<charr2 << "," << str1 << "," << str2 << endl; 2、初始化 string str3{ "I am a girl." }; string str4 = { "I am a student" }; 【1】可以使用C风格字符串来初始化string对象 【2】可以使用cin将键盘输入存储到string对象 【3】可以使用cout显示string对象 【4】可以使用数组索引来访问string的字符 【5】如上述代码所示,str1的声明创建一个长度为0的string对象,但将输入读取到str1时,将自动调节str1的长度。 3、赋值、拼接、附加 【1】不能将一个字符数组赋值给另一个字符数组,但可以将一个字符串赋值给另一个字符串。 【2

Output aligned columns

匆匆过客 提交于 2019-11-29 04:52:29
I am learning C++. I have a problem formatting the output of my program. I would like to print there columns perfectly aligned but so far I cannot do it, here is my code: int main() { employee employees[5]; employees[0].setEmployee("Stone", 35.75, 053); employees[1].setEmployee("Rubble", 12, 163); employees[2].setEmployee("Flintstone", 15.75, 97); employees[3].setEmployee("Pebble", 10.25, 104); employees[4].setEmployee("Rockwall", 22.75, 15); printEmployees(employees, 5); return 0; } // print the employees in my array void printEmployees(employee employees[], int number) { int i; for (i=0; i

Why is the address of this volatile variable always at 1?

可紊 提交于 2019-11-28 23:09:32
I wanted to inspect the address of my variable volatile int clock; cout << &clock; But it always says that x is at address 1. Am i doing something wrong?? iostreams will cast most pointers to void * for display - but no conversion exists for volatile pointers. As such C++ falls back to the implicit cast to bool . Cast to void* explicitly if you want to print the address: std::cout << (void*)&clock; There's an operator<< for const void* , but there's no operator<< for volatile void* , and the implicit conversion will not remove volatile (it won't remove const either). As GMan says, the cv