-4

My task is to get a string with no spaces from the user and make the computer count the number of characters, letters, numbers, and special characters (i.e. !@#$%^&*) However the program seems to be skipping the first character no matter what category this character falls under. note that it does count it in the number of characters just not in its category example: cin >> aZ12!@

output: 6 characters, 1 letter, 2 numbers, 2 special characters. it always skips the first character.

#include <iostream>
#include <string> 
using namespace std;

int main()
{
    char str[100]; // available character string max is 99 characters
    int i;
    int lett;
    int num;
    int spec;

    cout << "Please enter a continuous string of characters with no spaces" << endl ;
    cout << "(example: ASO@23iow$)" << endl << endl ;   //shows an example and then adds a blank line
    cout << "Enter your string: " ;
    cin >> str ;
    cout << endl ;

   while(str[i] != 0)
   {
      switch(str[i])
       {
            case '0' ... '9':
                i++ && num++;
                break ;
            case 'a' ... 'z':
                i++ && lett++;
                break ;
            case 'A' ... 'Z':
                i++ && lett++;
                break ;
            default :
                i++ && spec++;
       }
   }

   cout << "your string has " << i << " characters" << endl ;
   //prints the number of numbers in the string
   cout << "Your string has " << num  << " numbers in it." << endl ;       
   cout << "Your string has " << lett << " letters in it." << endl ;
   cout << "Your string has " << spec << " special characters." << endl   ;
   return 0 ;
6
  • Your variables are not initialized, and thus may contain anything. Set i, lett, num, and spec to 0 when initializing. Sep 9, 2016 at 21:06
  • 3
    a) The '0' ... '9' is not standard C++ and b) you need to enable more compiler warnings.
    – Baum mit Augen
    Sep 9, 2016 at 21:06
  • I don't see you initialize the value for i anywhere? shouldn't you set it zero before using it?
    – Sam Orozco
    Sep 9, 2016 at 21:06
  • 2
    @Mojo Its not about your formatting, its that you haven't shown enough effort in research and debugging attempts. Sep 9, 2016 at 21:19
  • 1
    @Mojo Its not about being good or bad at programming, its about how to ask a good question. This question boils down to, its skipping the first letter and here is the code. If i came to you as a specialist in something, say mechanics and said this part is leaking, then you turned it over and saw there were holes in the bottom, you would be annoyed as well. Put more effort into your problem solving and you will become a great programmer. Sep 9, 2016 at 21:26

1 Answer 1

0

In your code, int i is not initialized. Using it is Undefined Behaviour.

int i = 0;

The same goes for the rest of your variables. Also this doesnt do what you think it does:

i++ && lett++;

This is not do both operations, its a Boolean operator. It employs something called short circuiting, which means if the first part of the && evalutes to false (ie 0), then the expression must be 0 so there is no point in evaluating the rest of it (ie the lett++ part). So for your first loop (i == 0) your lett++ will be short circuited.

Change these to:

i++;
lett++;

If you fix this up it will work:

Live example

7
  • Didn't fix the problem. It still skips the first character.
    – Mojo
    Sep 9, 2016 at 21:09
  • again, did not fix the problem.
    – Mojo
    Sep 9, 2016 at 21:13
  • @Mojo I did downvote, the question doesnt conform to the standards of this site. Next time, try using a debugger to figure it out, if you still cant, give a MCVE and detail exactly what you have tried to fix the problem. You clearly did not look up what the && does, and if you had of debugged and stepped over the increment of i and lett you would have found this immediately. Put in a little more effort! Sep 9, 2016 at 21:22
  • 1
    @Mojo, sure, but did you look it up? When you printed out the value of i and lett at every line, weren't you confused about why i increments but lett doesn't? Wouldn't that have made you double check it? You didnt do any of the above because your first tought was to just ask and someone will tell you. Thats a bad start, thats why i shouldnt have answered here :( Sep 9, 2016 at 21:28
  • 2
    @Mojo "i don't even know how to use xcodes debugger" - now is a perfect time to learn.. Sep 9, 2016 at 21:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.