hello,

i want write a simple programe wich count the number of the chrechtar

this is my code

#include <iostream>
using namespace std;
int main()
{
int count = 0;
char str[30];
cout << "enter a word: " << endl;
cin >> str;
for (int x = 0; x <30; x++)
if(str[x]){
count++;
}
cout << count;
return 0;
}

second questions: the user should input two numbers and then i have two find all the numbers between them, then find their product

Sample run: Enter first number: 2
Enter second number: 6
2 3 4 5 6
Product = 720

this is my code but not comblete

#include <iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Please enter two numbers: " << endl;
cin >> num1 >> num2;
	for(int i; num1 > num2; i++)
    cout << i;
return 0;
}

i coulnd't do it :(

First problem: what is your program going to do if I enter 50 characters? You should try that.

Second problem: The for loop on line 8 is incorrect. for(int i = num1; i <= num2; i++)

thank you for your replying

first problem i tried this many times but still can't solve it

thank you for the second problem, i understand how it works

#include <iostream>

int main ()
  {
    int count=0;
    char* str;

    std::cout << "Enter word: " << std::endl;
    std::cin >> str;

    for (;*str!='\0';count++,*str++) {}
    std::cout << count;
  }
#include <iostream>

int main ()
  {
    int count=0;
    char* str;

    std::cout << "Enter word: " << std::endl;
    std::cin >> str;

    for (;*str!='\0';count++,*str++) {}
    std::cout << count;
  }

Hmm, have you tested that? Does it work as expected?

it's working but the code is very complex to me

culd you please help me with my code?

thank you :)

>it's working but the code is very complex to me
Do not know about the complex part, but the code is wrong, do not follow it.

caut_baia, you were wrong in two ways: first, you tried to give the OP a direct answer which can spoil his fun of `getting' towards the answer. It's not the answer which is important but it's the path to the answer which is.

Secondly, you failed to allocate any memory allocation to str, it is pointing to some random memory location and because you are cin-ing to that location, you are invoking undefined behaviour. Please allocate enough memory to str before using it.

I think the no one understood the point that Ancient_Dragon wanted to make about the 50 character limit. He wanted the OP to consider the std::strings. They will make the program flexible to accept any length long input string.

commented: Excellent comments +28

thank you for your addition

but could you help me

When a program doesn't work as expected, *think* how it is working currently.
You have declared an array of 30 chars.
You are running a loop for each of those char. If it is not zero (which it would if it was the post-last character of the string, the null-character), you are incrementing the count. Pretty well till here, but what should happen if the current character, is actually a null-character? The loop should terminate. But in your case it is not doing so.
Now you see the problem?

Below is a safer version in terms of getting the input without std::string i.e it can easily be done with char arrays too.

Otherwise, the program is functionally practically the same i.e. the for-loop and the if-statement (that does not break the loop), are as in the original, however this version gets the count right ;)

Now empror9, can you figure out why does this happen?

#include <iostream>
#include <iomanip>  // <- because of setw()
using namespace std;

int main()
{
  // The size of the array, note: 'const'
  const int array_size = 30;

  int count = 0;

  // !! Attention !!
  // Initialize the whole array to '\0'
  char str[array_size] = "";

  cout << "enter a word: " << endl;

  // Safely get the input, no fear of buffer overflow
  cin >> setw(array_size) >> str;

  for (int x = 0; x < array_size; x++)
    if(str[x])
    {
      count++;
    }

  cout << count;

  return 0;
}

The above post (#10) contains a fairly `nice' code. Though it, too is looping unnecessarily throughout the whole array. Stepping out of the loop once we know that the current character is null '\0' will be better: (modifying the above code)

#include <iostream>
#include <iomanip>  // <- because of setw()
using namespace std;

int main()
{
  // The size of the array, note: 'const'
  const int array_size = 30;

  int count = 0;

  // !! Attention !!
  // Initialize the whole array to '\0'
  char str[array_size] = "";

  cout << "enter a word: " << endl;

  // Safely get the input, no fear of buffer overflow
  cin >> setw(array_size) >> str;

  for (int x = 0; x < array_size; x++)
    if(str[x])
    {
      count++;
    }
    else break;


  cout << count;

  return 0;
}

This code can however be shortened considerably which would eliminate the need of many redundant variables. Here is a possibility:

#include <iostream>
#include <iomanip>  // <- because of setw()
using namespace std;
int main()
{
  // The size of the array, note: 'const'
  const int array_size = 30;
  char str[array_size];

  cout << "enter a word: " << endl;
  // Safely get the input, no fear of buffer overflow
  cin >> setw(array_size) >> str;

  int count;
  for (count = 0; str[count]; count++) ;

  cout << count;
}
commented: Helpful! +9

thank you for your addition

there is a small problem, when input a spacae so after space will not count

for example if i enter "my name" so the result will be 2 and that's wrong

can you see my new code

#include <iostream>  
using namespace std;
int main()
{
int size = 0;
char name[20];
cout<<"Please enter your name: ";
cin.getline(name,20);
size = strlen(name);
cout << size;
  return 0;
}

the problem with my code is counting even the spaces

so any help to just coun the charcter

waiting for you

Technically, space is also a character. Perhaps what you are talking about is the alphanumeric characters.
Now this is simple, just loop through your string and check if each character is alpha-numeric (hint, use isalnum() in <cctype>) , if yes, increase the counter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.