Sep 21, 2016 at 12:13am Sep 21, 2016 at 12:13am UTC
Hello guys, I'm new to the forums! My name is Leonardo, if you wish to call me by that.
I have this problem for homework but I can't figure out for the life of me how to do this bit.
This is the problem:
Write a program that uses a loop to display the characters for the ASCII codes 0
through 127. Display 16 characters on each line.
I got it to print all of the ascii codes... except they're not in lines of 16 characters each.
They just print out in 127 lines of one character each.
(BTW I'm new to c++, we've only covered up to loops in my class.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
using namespace std;
int main()
{
const int MAX = 127;
const int MIN = 0;
char code;
while (code >= MIN && code <= MAX)
{
cout << code << endl;
code++;
cout << code << endl;
}
return 0;
}
Last edited on Sep 21, 2016 at 12:17am Sep 21, 2016 at 12:17am UTC
Sep 21, 2016 at 12:19am Sep 21, 2016 at 12:19am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
using namespace std;
int main()
{
int i;
int &code = i;
const int MIN = 32;
const int MAX = 127;
const int characterPerLine = 16;
for (i = MIN; i < MAX; i++)
{
cout << (char )code << ' ' ;
if ((i % characterPerLine) == 0) cout << endl;
}
cout << endl;
return 0;
}
! " # $ % & ' ( ) * + , - . / 0
1 2 3 4 5 6 7 8 9 : ; < = > ? @
A B C D E F G H I J K L M N O P
Q R S T U V W X Y Z [ \ ] ^ _ `
a b c d e f g h i j k l m n o p
q r s t u v w x y z { | } ~
Hello, Leonardo.
Welcome to CPlusPlus forum.
Last edited on Sep 21, 2016 at 12:20am Sep 21, 2016 at 12:20am UTC
Sep 21, 2016 at 12:31am Sep 21, 2016 at 12:31am UTC
Thank for the fast reply, SakurasouBusters.
Can I integrate this solution without changing all of my code?