Get the numbers of numbers help . Numbers is classified as a group of numbers! Help Please!

Looking for the best way to find the # of numbers when I already have the number of digits found ! Hmmm? Any help Please

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cctype>
#include<stdio.h>
#include<iterator>

using namespace std;

int main() {

  char ch[80]; 
  int i, num = 0,character = 0, letters = 0, spaces = 0, punct = 0, digits = 0, special = 0, upper = 0, lower = 0;

  cout << "\nEnter The String:\n " << endl;
  gets (ch);
  
  for (i =0; ch[i] !='\0';i++) 
  {
  	
  	if (ch[i] != ' ')
character++;

 else if(isdigit(ch[i])) 
       ++digits;
    ++ch[i];
}

cout << "Number of digits: " << digits << endl;
cout << "Number of numbers " << num;

}
the question is not completely clear, what do you mean with number of numbers?
Count the number of times that you switch from a non-digit to a digit.
I am trying to read in a string and for example if I have the number 23. The number of numbers should be 1! The number of numbers should be coded as a group of numbers.
Count the number of times that you switch from a non-digit to a digit.

Below, count every time you switch from false F (not a digit) to true T (is a digit). This gives you the number of groups, as the switch from F to T is the signature of a new group starting. (The initial _ below just means the status before you started the string.)

Example:
_123abc456def789
FTTTFFFTTTFFFTTT
 ^     ^     ^
 |     |     |
 1     2     3



Trivial example:
_12
FTT
 ^
 | 
 1 
Last edited on
Thank you ! Do you have a code that shows how this works. I like the method, but not sure how to code this!
Just have a bool variable called STATE, say. Initialise it to false. Loop through the string, setting STATE to true if a digit, false if not. Every time STATE changes from false to true, add one to your count of numbers.

You would learn more by attempting to code that yourself: it doesn't need any advanced programming features.
I never uses Boolean and I suck ! *FacePalm* I have this far! I Tried to do a nested for loop


 
Last edited on
@marqual123

Let's go back to your original code. I've extracted the relevant parts.


DONE - loop through the string
DONE - identified and counted digits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
  char ch[80]; 
  int i, digits = 0;

  cout << "\nEnter The String:\n " << endl;
  gets (ch);
  
  for (i =0; ch[i] !='\0';i++)             // You loop through the string
  {
  	if (isdigit(ch[i]))
        {
           ++digits;       // You increment the count of digits
        }
   }
}



NEXT STAGE - introduce a bool variable, for whether you are in a digit group or not.
bool STATE = false; // initialised to false


THEN...
Now can you change STATE between true and false according as the character you are looking at is a digit or not. Hint: you are already using isdigit().


FINALLY...
Can you introduce a new count of "number groups":
int numbers = 0;
and increment it by 1 (numbers++) only when STATE changes from false to true.


You need to think about what you are doing, not just throw code snippets together thinking that they will magically make working code.

@lastchance I'm trying so hard to learn but its just not coming together for me. I just don't know the placement of where the things go! I've read over this and looked up other Boolean code. I cannot grasp it just yet!
Okay I have gotten it to compile but it says I only have 1 number of numbers when I have 2 numbers!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool NUM;
int main()
{
  char ch[80]; 
  int i, digits = 0, numbers = ;

  cout << "\nEnter The String:\n " << endl;
  gets (ch);
  bool NUM = false ; //initialises to false
  for (i =0; ch[i] !='\0';i++)             // You loop through the string
  {
  	if (isdigit(ch[i]))
        {
           ++digits;       // You increment the count of digits
        }
       if (NUM == true)
	 numbers++;	
   }
}

Enter The String:

23 . 21
Number of characters: 5
Number of letters: 0
Number of lower cases : 0
Number of upper cases : 0
Number of digits: 4
Number of punctuation characters: 1
Number of special characters: 0
Number of spaces: 2
Number of words: 3
Number of numbers 1
@marqual,
There is no point in presenting output that manifestly didn't come from the code that you have just posted. Make sure that code and output are consistent.
Topic archived. No new replies allowed.