It doesn't really matter that much what you use to write the code, as the language is always the same, so use whatever is most comfortable. Some offer extensions though, but it's not good to use them if you're learning the language, as they're deviations from the standard.
Some pointers though.
Code:
int i, choice;
char c;
while (choice!=4)
This while is a big error, you're using variable "choice" without initializing it - giving it some value first, so it contains some gibberish (leftovers in the memory). It will usually work, but it can also cause problems with your program, such instability or crashes, especially if the compiler attempts to optimize your code.
Never use variable without initializing it (giving it some value)
You're using it to control the loop, which is the
while loop. While loop is a loop that is used when it doesn't have to run even once (can be completely skipped), if you need the loop to be executed at least once, use the
do-while loop. This loop has condition at the end, so it's okay to use it there, as the variable choice gets initialized at the end. So you can write it as following:
Code:
void ascii()
{
int i, choice;
char c;
do
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout << "Choose an operation: \n\n";
cout << "1. Number to char\n";
cout << "2. Char to number\n";
cout << "3. ASCII List\n";
cout << "4. Exit\n\n";
cout << "Selection: ";
cin >> choice;
cin.get();
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
switch (choice)
{
case 1:
cout << "Number: ";
cin >> i;
cin.get();
cout << "\n\nChar: " << (char)i;
cout << "\n\n\nPress enter to go back...";
cin.get();
break;
case 2:
cout << "Character: ";
cin >> c;
cin.get();
i=c;
cout << "\n\nChar: " << i;
cout << "\n\n\nPress enter to go back...";
cin.get();
break;
case 3:
for (int x = 1; x < 255; x++)
{
cout << x << ": " << (char) x << "\n";
}
cout << "\n\n\nPress enter to go back...";
cin.get();
break;
case 4:
break;
}
} while (choice!=4)
}
Another thing that I don't like much is this line:
Code:
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
If you want to do something repeatedly like this, it may be better (cleaner) to use a loop, instead of writing so many "\n". For example:
Code:
for(int i = 0; i < 40; ++i)
cout << "\n";
Technically, it's a bit slower, but in this case, it doesn't matter at all and it's much more cleaner. If you're going to use it repeatedly, you can wrap it in a function and use like this:
Code:
void WriteLine(unsigned int number)
{
for(int i = 0; i < number; ++i)
cout << "\n"; // alternatively also cout << endl;
}
...
WriteLine(40);
Another thing that I find weird is that you have tons of "cout <<" for something. You can chain output for cout like this:
Code:
cout << "Choose an operation: \n\n
<< "1. Number to char\n"
<< "2. Char to number\n"
<< "3. ASCII List\n"
<< "4. Exit\n\n"
<< "Selection: ";
Also in the switch you have
Which doesn't really do anything, so you can just leave it out.
Now to the main() function, there are a few problems too. Again, using the variable choice without initializing it, like I explained already, so I ignore that now. Then. You don't need to choice and choice2 variables, one is good enough, because characters (char) are basically numbers too (just 8 bit) so if you want to save space, you can save the input to character too, like this:
Code:
choice = 0; // make it zero first, to erase upper bytes that won't be used
cin.get((char &)choice); // load character into the choice variable
cin.get(); // get rid of it newline in the input stream
Also the portion:
Code:
if (choice2=='Y' || choice2=='y')
Can be done like this, with function to convert it to lower case, so you don't have to check both
Code:
if( tolower(choice) == 'y' )
Also if you have only one command, you don't need the curly braces, so
Code:
if (choice2=='Y' || choice2=='y')
{
choice=4;
}
else
{
choice=3;
}
Can be written like
Code:
if (choice2=='Y' || choice2=='y')
choice=4;
else
choice=3;
Also, last thing: you declared the function main() as "int main()", meaning, it returns a value of type "integer". However, you do not return anything from inside the main, so you have to add at the end:
"return 0;" otherwise problems may arise. Or, declare main() as "void main()" if your compiler supports it, though main() should always return something, zero usually means that the program exited correctly.
If you have any questions, feel free to ask, I probably omitted some stuff, though I pointed out the most significant mistakes.