Thread: Homework blah

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Homework blah

    Write a program that uses a loop to display the characters for each ASCII code 32 through 127. Display 16 characters on each line with one space between characters.

    We are using the Starting out with C++ Early Objects 6th edition. I really don't know how to start this program, I'm not sure if it wants me to display ever character, or if I have to do get the user to input numbers and give me the characters that it should be or the other way around. If you can please help me that would be great.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Sounds very self-explanatory to me. Use a loop from 32 to 127 to display the characters with those values, no user input needed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    You can display your output with the printf function. Use the "%c" formatting string to display your loop counter as a character. #include <stdio.h> to be able to use printf.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Zlatko
    You can display your output with the printf function. Use the "%c" formatting string to display your loop counter as a character. #include <stdio.h> to be able to use printf.
    Although you could do as Zlatko suggests, I suggest that you practice with more C++ specific I/O instead. You could choose to use a loop counter variable of type char, or of say, type int but cast to char for printing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Although you could do as Zlatko suggests, I suggest that you practice with more C++ specific I/O instead.
    Oh, sorry, I thought I was in the other forum.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Yeah, what Zlatko lost me, I haven't seen that before...

    Anyways this is what I have right now, but it is annoying because it wants 16 chars on each line, and it displays the first number twice...

    So I guess I might have to do a few loops, maybe one loop per line...

    Source code


    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	//declaration of variables
    	int dec = 32;
    
        //loop
    	while(dec <= 127)
    	{
    		cout<< dec
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++
    			<< " " << dec++ << endl;
    
    	}
    
    	system("pause");
    		return 0;
    }
    This is what it looks like

    47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
    62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47
    77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62
    92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77
    107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92
    122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107
    137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122
    Press any key to continue . . .

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Look up the difference between postfix and prefix increment on the forum.

    If you search the forum, you'll find plenty of posts about it, and can fix your code.

    edit: Oh wait, try printing char(dec++).
    Last edited by whiteflags; 06-29-2009 at 02:41 PM.

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Hint:

    Loops can be nested within each other.
    Woop?

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by karusy View Post
    Write a program that uses a loop to display the characters for each ASCII code 32 through 127. Display 16 characters on each line with one space between characters.
    That's just printing the numbers 32 to 127.
    You need to print the ASCII chars for those numbers (i.e. use a char instead of an int).

    To split lines every 16 chars you can either have 2 loops, or if you've learned about the % operator you can use that instead.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    I still don't get the postfix and the prefix, if someone could give me a hand that would be great. So help with the chars displaying twice, I don't know how to fix that.
    My teacher didn't really explain them...
    anyways this is the code

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	//declaration of variables
    	char dec = 32;
    
        //loop
    	while(dec <= 110)
    	{
    		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;
    		}
    	}
    
    	system("pause");
    		return 0;
    }
    This is the Screen Output

    / . - , + * ) ( ' & % $ # " !
    > = < ; : 9 8 7 6 5 4 3 2 1 0 /
    M L K J I H G F E D C B A @ ? >
    \ [ Z Y X W V U T S R Q P O N M
    k j i h g f e d c b a ` _ ^ ] \
    z y x w v u t s r q p o n m l k
    ⌂ ~ } | { z
    Press any key to continue . . .

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So here's the thing. Prefix or postfix, you're not allowed(*) to do ++ more than once in a statement. So you need to split this up into code that is much easier, by only having one printed char per "cout" statement.

    (*)Well, you're allowed, but the compiler is also allowed to do anything it wants -- there's no way to guarantee you'll get the order you desire.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning Multiple Values from a function
    By Eddie K in forum C Programming
    Replies: 6
    Last Post: 03-12-2006, 01:18 PM
  2. Backdooring Instantaneous Radius of Curvature & Functions
    By just2peachy in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2004, 12:25 PM
  3. homework time
    By dP munky in forum C Programming
    Replies: 3
    Last Post: 11-23-2002, 04:49 AM
  4. OS Reality
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 02-15-2002, 01:03 AM
  5. NOTs, ORs and ANDs? HELP!!
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 09:28 PM