Where did printf("%s", 6); come from?
That prints garbage (or generates a segfault) because the value '6' is taken as the address of a null-terminated character string (char[]) to display. On an x86, that address is protected. (And it doesn't contain textual data.)
#include <cctype>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
bool done = false; // (use boolean variables for boolean values)
while (!done)
{
string input; // (use std::strings instead of char[])
// Get the user's input string
cout << "Please enter something> " << flush;
getline( cin, input ); // (use std::getline() to obtain user input)
// Display the user's input string containing nothing but alphabetic characters
cout << "original: ";
for (unsigned n = 0; n < input.length(); n++)
{
if (isalpha(input[n])) // (use std::isalpha() to make sure
cout << input[n]; // only alphabetic characters are printed)
}
cout << endl;
// Display the alphabetic index of each alphabetic character
// to the user using Hammurabi's technique
cout << "alphabetic indices: ";
for (unsigned n = 0; n < input.length(); n++)
{
if (isalpha(input[n]))
cout << (int)(toupper(input[n])-'A'+1) << " ";
}
cout << endl;
// Does the user want to play again?
cout << "Would you like to do it again (y/[n])? " << flush;
getline( cin, input );
// (we're done if the user just pressed ENTER
// or typed anything that doesn't start with a Y)
done = !input.empty() && (toupper(input[0])!='Y');
}
return 0;
}
LOL, yes, I knew that. I was only explaining to the OP why it was failure. I was just wondering where the idea that he would print garbage came from, since everything was a char*... that's all.
Silly words. They never sound the way they're meant....