Hi!

I have to write program, that reads signs and it counts, how many is capital letters, how many small letters, and how many remaining signs. It stops, when reads thrue 20 signs or when sign q or Q is entered. Before the end it writes out typed and calculated numbers.

I have some problems with while loop, it doesn't stops by sign q or Q (that is why I put it in comments) and another, bigger problem is with printing all entered numbers on the screen. Do I have to write entered numbers in array or what?

Thanks for your answers!

dr.eu

#include <iostream.h>
#include <stdlib.h>

int main()
{
   char sign;
   int countersmall, countercapital, counterremain, number, counternumbers, sum;

   countersmall = countercapital = counternumbers = sum = 0;

   cout << endl << " Program, that reads signs and it counts, how many is capital letters," << endl;
   cout << " how many small letters, and how many remaining signs." << endl;
   cout << " It stops, when reads thrue 20 signs or when sign q or Q is entered." << endl;
   cout << " Before the end it writes out typed and calculated numbers." << endl << endl << endl << endl;
/*
   cout << " Enter sign (q or Q for end) and press ENTER: ";
   cin  >> sign;

   if(sign == 'q' || sign == 'Q')
   {
      cout << endl  << endl  << endl << " ***** END ***** " << endl;
      cout << endl << endl << endl;
      system("pause");
      return 0;
   }

   else if(sign != 'q' || sign != 'Q')

   do
   {
*/
      for(int counter = 1; counter <= 20; counter++)
      {
         cout << endl << " Enter " << counter << ". sign (q or Q for end) and press ENTER: ";
         cin  >> sign;


         if(sign >= 'A' && sign <='Z')
         {
            countercapital++;
         }

         if(sign >= '0' && sign <='9')
         {
            counternumbers++;

            if(sign=='0')
            {
               number=0;
            }

            else if(sign=='1')
            {
               number=1;
            }

            else if(sign=='2')
            {
               number=2;
            }

            else if(sign=='3')
            {
               number=3;
            }

            else if(sign=='4')
            {
               number=4;
            }

            else if(sign=='5')
            {
               number=5;
            }

            else if(sign=='6')
            {
               number=6;
            }

            else if(sign=='7')
            {
               number=7;
            }

            else if(sign=='8')
            {
               number=8;
            }

            else if(sign=='9')
            {
               number=9;
            }

            else
               number=number;
         }

         if(sign >= 'a' && sign <='z')
         {
            countersmall++;
         }

         sum = sum + number;

         counterremain = 20 - (counternumbers + countersmall + countercapital);
      }

      cout << endl << endl << endl << endl  << " Number of small letters is: " << countersmall;
      cout << endl << " Number of capital letters is: " << countercapital;
      cout << endl << " Number of remaining signs is: " << counterremain;
      cout << endl << " Number of numbers is: " << counternumbers;
      cout << endl << " Sum of all numbers is: " << sum;
      cout << endl << " Following numbers were entered:" << endl << " ";

      for(counternumbers = 1; counternumbers <= 19; counternumbers++)
      {
         cout << number << ", ";
      }
      cout << number;


   //}while(sign != 'q' || sign != 'Q');

   cout << endl  << endl << endl << " ***** END ***** " << endl;

   cout << endl << endl << endl << " ";
   system("pause");

 return 0;
}

Move lines 19-25 down within the loop to line 36 and it should work.

lines 47-99: You don't need to do that at all -- just save the sign in a char array so that they can be printed later at the end of the program. Since sign is a single character I guess you only enter single digit numbers.

OK, I used array char digit[19], but it dont work well, all entered numbers are printed but also some other signs, who fill the rest of array place. Also the sum is wrong too.

#include <iostream.h>
#include <stdlib.h>

int main()
{
   char sign;
   int countersmall, countercapital, counterremain, number, counternumbers, sum;

   char digit[19];

   countersmall = countercapital = counternumbers = sum = 0;

   cout << endl << " Program, that reads signs and it counts, how many is capital letters," << endl;
   cout << " how many small letters, and how many remaining signs." << endl;
   cout << " It stops, when reads thrue 20 signs or when sign q or Q is entered." << endl;
   cout << " Before the end it writes out typed and calculated numbers." << endl << endl << endl << endl;

   do
   {
      for(int counter = 1; counter <= 20; counter++)
      {
         cout << " Enter " << counter << ". sign (q or Q for end) and press ENTER: ";
         cin  >> sign;


      if(sign == 'q' || sign == 'Q')
      {
         cout << endl  << endl  << endl << " ***** END ***** " << endl;
         cout << endl << endl << endl;
         system("pause");
         return 0;
      }


         else if(sign >= 'A' && sign <='Z')
         {
            countercapital++;
         }

         else if(sign >= '0' && sign <='9')
         {
            counternumbers++;

            digit[counternumbers] = sign;

            sum = digit[counternumbers];
         }

         else if(sign >= 'a' && sign <='z')
         {
            countersmall++;
         }

         counterremain = 20 - (counternumbers + countersmall + countercapital);
      }

         sum = sum + digit[counternumbers];


      cout << endl << endl << endl << endl  << " Number of small letters is: " << countersmall;
      cout << endl << " Number of capital letters is: " << countercapital;
      cout << endl << " Number of remaining signs is: " << counterremain;
      cout << endl << " Number of numbers is: " << counternumbers;
      cout << endl << " Sum of all numbers is: " << sum;
      cout << endl << " Following numbers were entered:" << endl << " ";

      for(counternumbers = 1; counternumbers <= 19; counternumbers++)
      {
         cout << digit[counternumbers] << ", ";
      }
      cout << digit[counternumbers];

      cout << endl << endl;

   }while(sign != 'q' || sign != 'Q');

   cout << endl  << endl << endl << " ***** END ***** " << endl;

   cout << endl << endl << endl << " ";
   system("pause");

 return 0;
}

dr.eu

for(counternumbers = 1; counternumbers <= 19; counternumbers++){
    cout << digit[counternumbers] << ", ";
}
cout << digit[counternumbers];

First things first, Arrays are 0 index'ed. This means the first element within the array is 0 not 1. That is your first mistake. Number 2, you have an array of size 19 thus that is 0..18. using the <= operator you print number 1..19 and then you go on and print another character outside of the loop. Since the memory at point digit[19] and upwards isn't controlled by you, you are accessing random data, thus printing out what is junk. You should always be careful with thinks like this.

Change it to

for(counternumbers = 0; counternumbers < 19; counternumbers++){
    cout << digit[counternumbers] << ", ";
}

You may have other problems i have meerly address that one!

Chris

Well, I increase array size to 21, but the output is the same, not only entered numbers. Also the sum of those numbers is not corect.

dr.eu

Try initialising the array to 0?

No, with 0 size of array is not better.

>> sum = digit[counternumbers];
Two problems:
1) use += operator, not =

2)That is adding the ascii value of the digits. You need to convert to integer by subtracting '0', like this: sum += digit[counternumbers] - '0';

Super, Ancient Dragon!

You solved two of my problems, while loop and sum of numbers!!!
It's only one left, the output of entered numbers:
for example, if user entered numbers 1 2 3 4 5 6 7 8 9 and 0, the output must be: 1,2,3,4,5,6,7,8,9,0.

what you have should work -- but move it down outside that loop. You are getting strange charcters because it is trying to display uninitialized array elements which have not been filled yet.

Well, because it didn't work, I move away the do while loop and left only for, but with no result.

#include <iostream.h>
#include <stdlib.h>

int main()
{
   char sign;
   int counter, countersmall, countercapital, counterremain, number, counternumbers, sum;

   char digit[21];

   countersmall = countercapital = counternumbers = sum = 0;

   cout << endl << " Program, that reads signs and it counts, how many is capital letters," << endl;
   cout << " how many small letters, and how many remaining signs." << endl;
   cout << " It stops, when reads thrue 20 signs or when sign q or Q is entered." << endl;
   cout << " Before the end it writes out typed and calculated numbers." << endl << endl << endl << endl;


      for(counter = 1; counter <= 20; counter++)
      {

         cout << " Enter " << counter << ". sign (q or Q for end) and press ENTER: ";
         cin  >> sign;


         if(sign == 'q' || sign == 'Q')
         {
            cout << endl  << endl  << endl << " ***** END ***** " << endl;
            cout << endl << endl << endl;
            system("pause");
            return 0;
         }

         else if(sign >= 'A' && sign <='Z')
         {
            countercapital++;
         }

         else if(sign >= '0' && sign <='9')
         {
            counternumbers++;
            digit[counternumbers] = sign;
            sum += digit[counternumbers]-'0';
         }

         else if(sign >= 'a' && sign <='z')
         {
            countersmall++;
         }

         counterremain = 20 - (counternumbers + countersmall + countercapital);

       }

      cout << endl << endl << endl << endl  << " Number of small letters is: " << countersmall;
      cout << endl << " Number of capital letters is: " << countercapital;
      cout << endl << " Number of remaining signs is: " << counterremain;
      cout << endl << " Number of numbers is: " << counternumbers;
      cout << endl << " Sum of all numbers is: " << sum;
      cout << endl << " Following numbers were entered:" << endl << " ";

      for(counternumbers = 1; counternumbers <= 21; counternumbers++)
      {
         cout << digit[counternumbers] << ", ";
      }


   cout << endl  << endl << endl << " ***** END ***** " << endl;

   cout << endl << endl << endl << " ";
   system("pause");

 return 0;
}

I think, if this code is that:

else if(sign >= '0' && sign <='9')
         {
            counternumbers++;
            digit[counternumbers] = sign;
            sum += digit[counternumbers]-'0';
         }

than array is filled with numbers, else will be not. Am I wrong?
And the result is once again with other signs.

dr.eu

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.