For Loop Acting Goofy

This is a discussion on For Loop Acting Goofy within the C++ Programming forums, part of the General Programming Boards category; Trying to make a simple app to output a range of characters to a text file. This is the code: ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    [SOLVED] For Loop Acting Goofy

    Trying to make a simple app to output a range of characters to a text file. This is the code:

    Code:
    #include <iostream>
    #include <fstream>
    int test[221];
    
    int main ()
    {
    	std::ofstream ascii_file ( "ascii.txt" );	
    	if ( !ascii_file.is_open() )
    	{
    		std::cout<<"File could not be opened.\n";
    		std::cout<<"Press enter key to exit...";
    		std::cin.get();
    		return 0;
    	}
    
    	ascii_file<<"ASCII Characters\n";
    	ascii_file<<"----------------\n";
    
    	for ( int i = 179; i <= 220; i = i + 6 )
    	{
    		test[i] = i;
    		/*
    		std::cout<<test[i]<<": "<<(char)test[i]<<" ";
    		std::cout<<test[i]++<<": "<<(char)test[i]<<" ";
    		std::cout<<test[i]++<<": "<<(char)test[i]<<" ";
    		std::cout<<test[i]++<<": "<<(char)test[i]<<" ";
    		std::cout<<test[i]++<<": "<<(char)test[i]<<" ";
    		std::cout<<test[i]++<<": "<<(char)test[i]<<"\n";
    		*/
    		ascii_file<<test[i]<<": "<<(char)test[i]<<" ";
    		ascii_file<<test[i]++<<": "<<(char)test[i]<<" ";
    		ascii_file<<test[i]++<<": "<<(char)test[i]<<" ";
    		ascii_file<<test[i]++<<": "<<(char)test[i]<<" ";
    		ascii_file<<test[i]++<<": "<<(char)test[i]<<" ";
    		ascii_file<<test[i]++<<": "<<(char)test[i]<<"\n";
    	}
    	ascii_file.close();
    	std::cout<<"File written successfully.\n";
    	std::cout<<"Press enter key to exit...";
    	std::cin.get();
    	return 0;
    }
    And this is the unexpected output:

    Code:
    ASCII Borders
    -------------
    179:  179:  180:  181:  182:  183: 
    185:  185:  186:  187:  188:  189: 
    191:  191:  192:  193:  194:  195: 
    197:  197:  198:  199:  200:  201: 
    203:  203:  204:  205:  206:  207: 
    209:  209:  210:  211:  212:  213: 
    215:  215:  216:  217:  218:  219: 
    I can't seem to get the numbers to orient themselves correctly. Any help would be appreciated.
    Last edited by Austaph; 12-02-2005 at 05:17 PM. Reason: Fixed

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    What exactly are you expecting? Everything looks lined up to me.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Look closer.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Preincrement
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Just do one output per loop and then check i to see if it's a multiple of 6, and if it is then output the newline. Your incrementing doesn't seem right, it is incrementing the value pointed to by the array, not the index i.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Quote Originally Posted by MrWizard
    Preincrement
    Thanks.

    Code:
    		ascii_file<<test[i]<<": "<<(char)test[i]<<" ";
    		ascii_file<<++test[i]<<": "<<(char)test[i]<<" ";
    		ascii_file<<++test[i]<<": "<<(char)test[i]<<" ";
    		ascii_file<<++test[i]<<": "<<(char)test[i]<<" ";
    		ascii_file<<++test[i]<<": "<<(char)test[i]<<" ";
    		ascii_file<<++test[i]<<": "<<(char)test[i]<<"\n";
    Outputs:
    Code:
    ASCII Characters
    ----------------
    179:  180:  181:  182:  183:  184: 
    185:  186:  187:  188:  189:  190: 
    191:  192:  193:  194:  195:  196: 
    197:  198:  199:  200:  201:  202: 
    203:  204:  205:  206:  207:  208: 
    209:  210:  211:  212:  213:  214: 
    215:  216:  217:  218:  219:  220: 

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You can replace your entire array with an int variable called test. You are using the array as a single variable. There is no advantage to using the array there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Goofy .......... first lady has no etiquette
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 04-10-2009, 11:29 AM
  2. Is my compiler acting up?
    By Alastor in forum C Programming
    Replies: 10
    Last Post: 10-11-2005, 07:19 PM
  3. char[] acting weird
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2003, 06:45 PM
  4. vectors acting up?
    By hsv in forum C++ Programming
    Replies: 1
    Last Post: 06-04-2003, 04:45 AM
  5. Goofy getline
    By Cela in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2003, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21