473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Strange output in loop

#include <iostream>
using namespace std;

int main(void)
{
int i=0;
while(char c = cin.get() != EOF) {
i++;
cout << i << c << endl;
}
}

I am a C programmer and I am learning C++ from Thinking in C++.
The problems with the above code are:
1) i is incremented twice and the output is printed twice.
2) c is not printed.

Nov 6 '07 #1
3 1097
riva <ra*********@gmail.comwrote in news:1194284825.342443.93040
@e34g2000pro.googlegroups.com:
#include <iostream>
using namespace std;

int main(void)
{
int i=0;
while(char c = cin.get() != EOF) {
i++;
cout << i << c << endl;
}
}
Well, the problem is that you are assigning c the value of the comparison
between cin.get() and EOF. I am sure this isn't what you want. Try:

#include <iostream>
using namespace std;

int main()
{
int i=0;
char c;

while ((c = cin.get()) != EOF) {
i++;
cout << i << c << endl;
}
}

joe
Nov 6 '07 #2
On Nov 6, 3:23 pm, riva <ra.ravi....@gmail.comwrote:
#include <iostream>
using namespace std;

int main(void)
{
int i=0;
while(char c = cin.get() != EOF) {
i++;
cout << i << c << endl;
}

}

I am a C programmer and I am learning C++ from Thinking in C++.
The problems with the above code are:
1) i is incremented twice and the output is printed twice.
2) c is not printed.
And your question is?

You probably mean something like
while(
(c = std::cin.get()) != EOF
) { ... }

Without the parentheses, you compare std::cin.get() with EOF, and
assign the result of the comparison to c. Then, c will contain some
sort of boolean representation, which is usually not printable (\0,
\1, or whatever).

Nov 6 '07 #3
On Nov 6, 7:36 am, Joe Greer <jgr...@doubletake.comwrote:
Well, the problem is that you are assigning c the value of the comparison
between cin.get() and EOF. I am sure this isn't what you want. Try:

. . .
char c;

while ((c = cin.get()) != EOF) {
. . .
If 'char' is unsigned on a given implementation, this comparison could
lead to some odd behavior, as EOF is guaranteed to be negative (which
is why many I/O functions that handle single characters take and
return values of type int).

Nov 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Morgan Wolfe | last post by:
Hello. I'm working on some simple code to generate a list of random integers and store them in a 50 element array. I have two conditions that I'm checking, first, that i < length(array) and...
4
by: hall | last post by:
Hi. I've come across someting strange. I was trying to make a for-loop execute repetadly until the function called inside it does not return true during the entire loop (see program below). ...
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
2
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
10
by: greenflame | last post by:
I have the following function. It is supposed to multiply all the elements of a matrix by a specified factor. It tells me that on "line 58" that undefined is null or not an object. Here is the...
11
by: Marlene Stebbins | last post by:
Something very strange is going on here. I don't know if it's a C problem or an implementation problem. The program reads data from a file and loads it into two arrays. When xy, x, y, *xlist and...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
4
by: stat_holyday | last post by:
Greetings. I'm confused. I'm attemting to create dynamic buttons based on the count of questions in a database. The button has 3 states, blank, selected, and inactive. The script below works great,...
1
by: jonavanmona | last post by:
Hi, i found this strange thing in firefox. if i alert the output of a simple 'for in' loop, it returns first the right value and then: function forEach(){ why is it doing this? it gives...
5
by: Gancy | last post by:
PROGRAM StringBuilder sbTest1 = new StringBuilder(); StringBuilder sbTest2 = new StringBuilder(); for (int i = 1; i <= 54; i++) sbTest1.Append("a");...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.