Thread: For Loop Acting Goofy

  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 06: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,366
    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,366
    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, 02:43 PM