c++11 - C++ problem with understanding of counting char variables


Question: 

I wrote a program that supposed to print the number of characters that i entered till it hits the '#' character. what i don't understand is, when i input in the console more than one character (say "hello world") the program count all the characters in one iteration. why does it count all the character in one iteration instead of 1?

char ch;
int count = 0;
cout << "Enter characters; enter # to quit:\n";
cin.get(ch);
while (ch != '#')
{
    cout << ch;
    ++count;
    cin.get(ch); // use it again
}
cout << endl << count << " characters read\n";



1 Answer: 

why does it count all the character in one iteration instead of 1?

It does not. You can verify that by changing the output a bit in the loop.

while (ch != '#')
{
   ++count;
   cout << "ch: " << ch << ", count: " << count << endl;
   cin.get(ch); // use it again
}
 

More Articles


java - gRPC: Generate certificateChainFile and privateKeyFile to make TLS/SSL work

I want to make tls/ssl connection between client and server(Not mutual, one way). This is my setup:Server: Server server = ServerBuilder.forPort(8443) // Enable TLS .useTransportSecurity(certChainFile, privateKeyFile) .addService(new GreetingServiceImpl()) .build();Cli

r - split string at every character

I want to split every character in a string and output it as comma or tab separated characters:I need to use file_in and file_out since I have very many lines.inputTTTGGCTTTGTGCAATGG........outputT,T,T,G,G,CT,T,T,GT,G,C,A,A,T,G,GI have used this, but it prints every character vertically: /usr/bin/p

libgit2sharp: how to set multiple values with same key

how do I set multiple values for a config-key? Some sections supports multiple values:[remote "origin"] url = git@github.com:schacon/simplegit-progit.git fetch = +refs/heads/master:refs/remotes/origin/master fetch = +refs/heads/qa/*:refs/remotes/origin/qa/*Something like this is not working in


Know of any setup tutorials for SQL Server Express and C#?

I'm a C# and MySQL developer, but I'm looking into Microsoft's SQL Server for a new project. I'm familiar with MySQL syntax and the .NET connector. Does anyone know of any decent tutorials that just cover the code to interact with SQL Server? No C# basics or SQL basics, just how to get the two tec

parsing - Parse JQ output through external bash function?

I want to parse out data out of a log file which consist of JSON sting and I wonder if there's a way for me to use a bash function to perform any custom parsing instead of overloading jq command.Command:tail errors.log --follow | jq --raw-output '. | [.server_name, .server_port, .request_file] | @ts

powershell - Table - Count Column

I have a PowerShell script that coverts JSON from an API and creates a table output.How would I TOTAL (add or count) the column 'TotalEndpoints' and display the total number of Enpoints?Here is the creation of the table(Invoke-RestMethod @Params).Sites | Format-Table SiteName,SiteId,TotalEndpoints


c++ - GCC Cross compile to a i586 architecture (Vortex86DX)

I have Ubuntu 12.01 with gcc 4.8.2 and would like to cross compile for the Vortex86DX CPU running an old 2.6.23 kernel.I´m trying the following testing code:#include <iostream>int main(){ std::cout << "Hello world" << std::endl;}That is compiled using the following command line:g

Using gstreamer plugins bad in android

I am trying to show h264 encodes streams inside an android application using the GStreamer SDK and Android NDK. My Android.mk looks like:LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := AppLOCAL_SRC_FILES := App.cppLOCAL_SHARED_LIBRARIES := gstreamer_androidLOCAL_LDLIBS := -llog -

c++ - How To Return Elapsed Time From steady_clock as a Primitive Data Type (double)

First and foremost, let me say that I just starting using this library yesterday, so my understanding of it is still fairly basic. I'm trying to capture the FPS of a vision processing program I'm creating and output it to a screen using the chrono library. In my case, I need to cast the elapsed time

hashtable - Powershell 2 and .NET: Optimize for extremely large hash tables?

I am dabbling in Powershell and completely new to .NET.I am running a PS script that starts with an empty hash table. The hash table will grow to at least 15,000 to 20,000 entries. Keys of the hash table will be email addresses in string form, and values will be booleans. (I simply need to track whe