Help, im new to C++ i need some advice
#include <ios开发者_JS百科tream>
#include <string>
using namespace std;
int main()
{
cout << "Input a Sentence: ";
cin >> x;
{
char* string = x;
int letter_count[26];
// Initialization
for(int i=0; i<26; letter_count[i++]=0);
// Counting the number of letters
for(int i = 0; string[i] != '\0'; i++) {
if(string[i] > 64 && string[i] < 91)
letter_count[string[i]-65]++;
else if (string[i] > 96 && string[i] < 123)
letter_count[string[i]-97]++;
else if (string[i] == '.')
break;
}
// Show the result
for(int i=0; i < 26; i++)
if (letter_count[i] != 0)
std::cout << letter_count[i] << " "<< char(i+97) << std::endl;
}
}
Why doesn't this program compile?
- Your code won't be able to compile. You used
x
incin << x
before declaring it. - use
std::getline
to read a line. - Don't use std type name (string) as the variable name. (Thanks to bdonlan)
Don't use magical number (63,etc).
By combine above together, we have this. This is far from perfect , but it is improved.
cout << "Input a Sentence: "; string line; std::getline(std::cin , line); int letter_count[26]; // Initialization for(int i= 0; i<26; i++) { letter_count[i] = 0; } // Counting the number of letters for(int i = 0; line[i] != '\0'; i++) { if(line[i] >= 'a' && line[i] <= 'z'){ letter_count[line[i]-'a']++; }else if (line[i] >= 'A' && line[i] <= 'Z'){ letter_count[line[i]-'A']++; }else if (line[i] == '.') break; } // Show the result
don't use the word string as your variable name, you are including the string.h header which defines a class with the same name,
and yes,it would be better if you write a question with a specific problem
You don't have declared the x
variable. It should be a std::string
:
string x;
After reading the input you declare a variable called string
(with char* string = x;
). If you leave out that line and just use x
everywhere where you now use string
your program compiles fine.
It also already very nearly does what I guess you intend it to do.
For starters you have to declare x
before you can use it.
Also you can change
int letter_count[26];
// Initialization
for(int i=0; i<26; letter_count[i++]=0);
to just
int letter_count[26] = {0};
The specific problem in your code is that you didn't declare the variable x
before use. (And your declaration was weird. To declare a variable, put the type followed by the name, like char* x
, optionally followed by an assignment to initialize it, char* x = "hello world"
. After the variable has been declared, the compiler will let you use it.
#include <iostream> // include the header containing cout and cin
#include <string> // include the header containing the string class
// using namespace std; // don't do this. Prefix standard library names with the std namespace instead
int main()
{
std::cout << "Input a Sentence: ";
std::string x; // declare a variable named x, so we can use it afterwards - and use the C++ string class, not char*
//std::cin >> x; // only reads a word into x. If we want a sentence, use getline instead:
std::getline(cin, x);
int letter_count[26] = {}; // automatically fills the array with zeros
// Counting the number of letters
for(std::string::iterator it = x.begin(); it != x.end(); ++it) { // might as well use iterators. That's what they're for. And then your code behaves better if the string actually contains a \0 character.
if(*it >= 'A' && *it <= 'Z'){ // never leave out the {}'s. It'll blow up on you later, when you modify the code
letter_count[*it - 'A']++;
}
else if (*it >= 'a' && *it <= 'z'){
letter_count[*it-'a']++;
}
else if (*it == '.'){
break;
}
}
// Show the result
for(int i=0; i < 26; i++)
if (letter_count[i] != 0)
std::cout << letter_count[i] << " "<< static_cast<char>(i+97) << std::endl; // prefer C++-style casts
}
}
精彩评论