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; }

20th Sep 2017, 11:39 PM
Ibo Mohammad
Ibo Mohammad - avatar
3 Antworten
+ 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.
20th Sep 2017, 11:51 PM
John Wells
John Wells - avatar
0
IBO I can address you to use array of characters instead
20th Sep 2017, 11:46 PM
Nura Programmer
Nura Programmer - avatar
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 :)
20th Sep 2017, 11:49 PM
Joefrey Racal Ramos
Joefrey Racal Ramos - avatar