A for Loop

Started by
7 comments, last by bioagentX 21 years ago
Here is the code for my for loop. For some reason, it doesn''t display the correct amount of numberse


/*Chapter 4 Excercise 24 Program
By Brendan Harnett (C) Brendan Harnett All Rights Reserved*/


#include

int main()
{





for(int x=1; x<=400; x++)//displays numbers from 1000 to 0
{


 cout<

Shouldn''t it display the numbers from 1 to 400? It doesn''t seem to be working with MSVC++ intro edition. If someone thinks they could identify the problem, which is probably a non-code related problem, I''d be very grateful.

-BioagentX    
There are three types of people in this world, those who can count, and those who can't
Advertisement
I did include the header file, but for some reason it didn''t show up in the message above.
There are three types of people in this world, those who can count, and those who can't
Well ... you might want to make it output something



i.e.
cout <

to
cout << x; in your code which you copied - unless you have an extreem case of typos ( //displays numbers from 1000 to 0 ) <--



~ Take care.
works for me, using .net 2k3

for(int x = 0; x <= 400; x++)   cout << " " << x; 


as long as you''ve #include <iostream> you shouldn''t be having problems. what version of msvc are you using?
crud, that should have been
for(x = 1; x <= 400...
quote:Original post by Anonymous Poster
crud, that should have been
for(x = 1; x <= 400...




if it should''ve been "for(x=1; x <= 400;..." be sure you have
defined x before the for loop otherwise you''ll get an error about x being undefined.
Ok, here''s the actual real code:

/*Chapter 4 Excercise 24 ProgramBy Brendan Harnett (C) Brendan Harnett All Rights Reserved*/#includeint main(){for(int x=0; x<=400; x++)//displays numbers from 0 to 400{ cout<}int stayopen;cin>>stayopen;return 0;} 


For some reason, it only displays the numbers from 102 to 400. I don''t know why, it could have something to do with XP''s windows size. Because on my older computer the program works, however, on my new one with windows XP, it''s not generating the necessary numbers.
There are three types of people in this world, those who can count, and those who can't
Its probably just the your window buffer isn''t large enough and the numbers 0-101 are not being helf. Pipe the output to a text file and open that by doing programname.exe | output.txt from the command prompt.
... You mean to say that the only problem is that the first numbers are not present in the console window''s buffer? Sheesh. Just try exchanging "cout << x << endl;" for "cout << x << " " << flush;" or something similar (I know it''s slow and it''s not how I''d do it, but it''s the simplest way) and see if it works. By the way, in the console window''s settings you should be able to increase the default buffer size ...

This topic is closed to new replies.

Advertisement