Hello I am a fairly new at programming and have little experience with programming beyond the tutorials on this website(which I think I am on the 15th) and I have been messing around with a few programs that they have had in the the tutorials and expanding them and I was just wondering if there is any bad habits, things I could do to make my programs more organized, or anything else that I should try to fix now before I get to use to programming like that. One of the programs I have expanded on was in the tutorial about typecasting(lesson 11) and was showing how you could turn numbers into ASCII characters. I expanded on the program by giving an option to search for a certain number's character or to show all of the ASCII numbers. Here is what the program looks like
Code:
#include <iostream>
using namespace std;
int allchar()//prints all ASCII characters
{
int x = 0;//sets x to 0; allows for multiple printings of the ASCII characters
while(x < 256) {
cout<< x <<". "<< (char)x <<" ";
x=x+1;
//Note the use of the int version of x to
// output a number and the use of (char) to
// typecast the x into a character
// which outputs the ASCII character that
// corresponds to the current number
}
return 0;
}
int picked_char()//User inputs the number to find which character it is
{
cout<<"Please enter in the characters number you want to find.\n";
int character;
cin>>character;
cin.ignore();
character=character;
cout<<character<<". "<< (char)character <<" ";
return 0;
}
int main()
{
int again=1;//runs program again
while(again == 1){
int choice;//user input for list of ascII or certain one
int letter;//uesr input for which character to search
int again_repeat = 1;//makes sure correct input in the while(again_repeat)
int repeat=1;//repeats while(again ==1) statement till correct input is put in
int program_repeat;//input to control if program repeats
while(repeat == 1){
cout<<"Do you want a list of ASCII characters(press 1)\n";
cout<<"or search for a certain one(press 2?\n";
cout<<"\n";
cin>>choice;
cin.ignore();
if(choice == 1){
allchar();
cout<<"\n";
cout<<"\n";
choice = 0;
repeat = 0;
}
else if (choice == 2){
picked_char();
cout<<"\n";
cout<<"\n";
choice = 0;
repeat = 0;
}
else{
cout<<"Invaild Entry. Please re-enter choice.\n";
cout<<"\n";
repeat = 1;
}
}
cout<<"If you want to search for another ASCII character press 1.\n";
cout<<"If you want to exit this program press 2.\n\n";
while(again_repeat == 1){
cin>>program_repeat;
cin.ignore();
cout<<"\n\n";
if(program_repeat==1){
again=1;
again_repeat = 0;
}
else if(program_repeat==2){
again_repeat = 0;
again=0;
}
else{
cout<<"Invaild Entry. Please re_enter choice.\n";
}
}
}
return 0;
}
If any of you have any suggestions on things that might improve my programming now so I will not develop bad programming habits. Oh also sorry if the variables have names that might not make sense could not think of other names for them in the moment.