Our instructor gave us this code (from char c to endl;). It should print out the ASCII characters from 1 thru 127 in 16 rows (he briefly showed us that it works) and told us we are supposed to format it appropriately. I finally got it to compile with no errors, however it only tells me "press any key to continue...".
I have tried adding and removing brackets, adding and removing semicolons, and moving cout around, but all that did was compound the errors. This code is error free but without any output and I just cannot "see" any reason for it not to work (if I copied everything completely and correctly). Any hints or pointers? Many Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
char c;
int i;
for (i =0; i<=127; i++)
c=i;
if (i % 16 ==0 && i <= 0)
{
cout << c << endl;
}
return 0;
}
I'm dense I guess. I changed the number to 128 and got the same output...nothing. It seems that, for reasons I don't understand or know, it's not converting the c=i part properly even tho it is initialized. Did I possibly miss a part of the code??
Here's your code with comments to help you explain (sorry if it's patronizing):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
usingnamespace std;
int main()
{
char c;
int i;
for (i =0; i<=127; i++) //i starts at 0 and each loop increments by 1
c=i; //each time the loop goes through c becomes a new character
if (i % 16 == 0 && i <= 0) //if i%16 is equal to 0 AND i is less or equal to 0 - which it's not
cout << c << endl; //if so, write c to the screen
return 0;
}
I was under the impression that the modulus would keep the rows at 16 and reset to zero (places) then continue this as long as it wasn't less than or equal to zero (which it couldn't be after the first iteration) obviously something I'm missing (visually or in code) is preventing an iteration from occuring. I have replaced the entire statement with other signs (>=, ||, +, =, etc) and used numbers other than zero and the most I got was a single little triangle type character. Between this and my two other problems my brain is fried.
Thanks for your responses and I'm gonna give it heck in the morning when my eyes aren't so droopy maybe I'll "see" clearer then. Maybe.
I have tried braces around the
For
{
c=i
)
I have put them around the entire statement from the "for" enclosing the "if". and I have them as they are now and unforunately the output is the same. I am just not getting this lesson and it's very frustrating. Between the incremention and these loops I'm going loopy! (Sorry for the sick pun it shows my lack of intelligent output at this point). Thanks for the input sorry for the (lame) joke.
/*
This program will convert integers into ASCII characters and show them in the display
*/
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
char c; //declare variables
int i;
for (i =0; i<=127; i++) //the integer is zero as a starting point and will be no greater than 127.
{
c=i; //this should convert ASCII characters into integers.
}
if (i % 16 == 0 && i <= 0) //this should align the characters into 16 rows.
{
cout << c << endl; //this should display the ASCII charcters on the screen
}
return 0;
}