0
I want to make a program counting characters in a sentence .... and it just doesn't workđ....someone can explain to me???
#include <iostream> using namespace std; int main() { char x; int counter; while (cin>>x) { counter += 1; } cout<< "there is " << counter << " characters" <<endl; return 0; }
3 Answers
+ 6
I expect you are getting a compiler error because you didn't put a number stating how big the array x is. Your counter isn't initialized so you will get a random result. Reading into an array however many characters it contains, yet you only increment counter by 1. cin will overwrite memory, if you type more characters than x supports.
0
IBO I can address you to use array of characters instead
0
I think simple way to count characters in a paragraph is use strlen(i dont know the equivalent of this in c++) just trim the whitespaces and use strlen add 1 since the strlen starts counting at zero like array :)