problems with nesting in looping

This is a discussion on problems with nesting in looping within the C++ Programming forums, part of the General Programming Boards category; ok so i am told to do a problem where i am using a loop to displaying ASCII characters for ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    problems with nesting in looping

    ok so i am told to do a problem where i am using a loop to displaying ASCII characters for codes 0-127 displaying 16 on each line. but when I put it in my program and try and run it, my comp starts beeping at me and the output is repetative can someone help me please... thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    One of the ASCII characters is "beep". If you're getting things repeated, then you should check how you structure your loop (i.e., make sure you don't start each loop at 1, since that means you'll never get past 16).

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    here is what i have inputted

    Im not sure what you extactly mean with starting the loop at 1 did i do that here? because it is still repeating
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	char dec = 0;
    
     
    	while(dec <= 127)
    	{
    		cout<< dec
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++ 
    			<< " " << dec++
    			<< " " << dec++ << endl;
    		while ( dec == 122 || dec >= 127)
    		{
    			cout<< dec
    				<< " " << dec++
    				<< " " << dec++
    				<< " " << dec++
    				<< " " << dec++
    				<< " " << dec++ <<endl;
    		break;
    		}
    
    	}
    		system ("pause");
    		return 0;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So when your compiler says:
    Code:
    ascii.cpp:9: warning: comparison is always true due to limited range of data type
    that might give you a hint. (Also hint: you should see the thread about signed vs. unsigned char in the C forum.)

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    sorry

    im not trying to ask for you to just give me the answer but I dont know what you mean? my compiler doesn't say that. but should I try and meybe enter a bool or and if statement to make it stop repeating?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by wobbles View Post
    im not trying to ask for you to just give me the answer but I dont know what you mean? my compiler doesn't say that. but should I try and meybe enter a bool or and if statement to make it stop repeating?
    Then you should either (1) get a better compiler or (2) stop muffling your compiler's warnings. Which one is appropriate depends on what compiler you have and what you've done to it. But it means just what it says: for any variable of type char, that variable will always be less than or equal to 127 (on my system and presumably your system as well). It is therefore not possible for your loop to stop, since any variable of type char will be less than 127. Try the following and see what you get:
    Code:
    #include <iostream>
    
    int main() {
        char bob = 127;
        bob = bob + 1;
        std::cout << (int) bob << std::endl; //cast to int, so the value is printed instead of a character
        return 0;
    }

  7. #7
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    You could, simply, make an int to check how far you are on the line, and the usual one to check how far along the 127 characters you need to display

    Then once the counter reachs sixteen you insert a line break, and one you reach the 127<sup>th</sup> character you "return" to stop the program.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    thank you

    thank you to those who tried to help me.. I appreciate it greatly but im still not quite getting it, I was also reading somewhere about doing it ++dec to get to not keep going to the negative side of it and continuing to loop, am I on the right track?

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    still trouble with looping

    here is what I have so far

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char dec = 0;
    	char cha = 0;
    
    
    	cout << "Here are the ASCII codes from 0 to 127\n";
    	cout << "---------------------------------------\n";
     
     
    	for(dec = 0; dec <= 127; ++dec)
    	{
    		for (cha = 0; cha = 16; ++cha)
    		cout << dec << "\t";
    			
    		cout << endl;
    		
    
    	}
    		return 0;
    }
    and still everytime I ouput it I get a repatative answer and its even the wrong symbols. how can i fix this can some one please help thank you.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    I think that your code is doing what you want to do. The problem is, as mentioned earlier, that some of these characters are not printable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Quote Originally Posted by wobbles
    and still everytime I ouput it I get a repatative answer and its even the wrong symbols.
    Repetative answer? You expect something else?
    Code:
    		for (cha = 0; cha = 16; ++cha)
    		cout << dec << "\t";
    cha == 16 was intended?

    [EDIT]
    And of course you should always trace your code to find out how it works. Ask yourself what chars will be outputed in each loop?
    Last edited by siavoshkc; 03-28-2010 at 03:45 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    ok updated

    ok so now I have it so it stops and doesnt repeat but now it doenst show any characters is there something im missing?


    Code:
                                           for (int i = 0; i <= 127; ++i)
    		{
    			for (int j = 0; j == 16; ++j)
    				cout << (j * i) + j<< "\t";
    				cout << endl;
    					
    		}
    		
    return 0;
    }

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    It is not all the problem, but it's the hard part. I leave simple part to you. Try the code below
    Code:
    int i = 53;
    cout << i;
    Compare the results with this one
    Code:
    char c = 53;
    cout << c;
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  3. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 05:38 PM
  4. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 01:47 AM
  5. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 07:07 PM

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