13 Replies - 372 Views - Last Post: 27 September 2012 - 10:33 PM Rate Topic: -----

#1 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Vowels not being counted?

Posted 27 September 2012 - 08:24 PM

Hello,
So like all the other beginning programming classes, I have to write a program that asks the user to input the text, then have the program spit out the number of each vowel in the text. I think I have the loop right and it complies, but when testing it, nothing is outputted. Can anyone help me out?
#include <iostream>
using namespace std;

int main()
{
  // Declare variables
  int va(0);   //number of A's
  int ve(0);   //number of E's
  int vi(0);   //number of I's
  int vo(0);   //number of O's
  int vu(0);   //number of U's
  char c=' ';

  cout << "Enter text: ";
  cin >> c;

  while (c != '.' || c != '?')
    {
      if(c=='a' || c=='A')
        {
          cout << "Number of a's: " << va++ << endl;
        }
      if(c=='e' || c=='E')
        {
          cout << "Number of e's: " << ve++ << endl;
        }
      if(c=='i' || c=='I')
        {
          cout << "Number of o's: " << vo++ << endl;
        }
      if(c=='o' || c=='O')
        {
          cout << "Number of u's: " << vu++ << endl;
        }
    }
  return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Vowels not being counted?

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Vowels not being counted?

Posted 27 September 2012 - 08:30 PM

First of all, when you are declaring variables in c or c++, never use brackets, or it may be considered as a function.

so ideally, a variable should be declared with the following stntax.

variable_type variable_name;
eg. int Number_of_a;

change the brackets in the variables and you code will certainly work.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Vowels not being counted?

Posted 27 September 2012 - 08:31 PM

If you enter a vowel it should output something. It's not correct but it should output something.

Your problem is you are only asking for one character. If you want to ask for more than one character then you must move the input into the loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Re: Vowels not being counted?

Posted 27 September 2012 - 08:37 PM

View Postjimblumberg, on 27 September 2012 - 08:31 PM, said:

If you enter a vowel it should output something. It's not correct but it should output something.

Your problem is you are only asking for one character. If you want to ask for more than one character then you must move the input into the loop.

Jim

Well that's what's really confusing me. After compiling, this is what I get:
Posted Image

I'm not sure I have it correct, but I thought that the loop would continue until the
c != '.'
was satisfied?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Vowels not being counted?

Posted 27 September 2012 - 08:44 PM

Quote

First of all, when you are declaring variables in c or c++, never use brackets, or it may be considered as a function.

Using parentheses when defining a variable is perfectly acceptable in a C++ program.
int a(100); // Create the variable a and initialize it to 100.

It is called constructor initialization and it is acceptable.



Quote

Well that's what's really confusing me. After compiling, this is what I get:


A char can only accept one character, so when you try to type in "hello" it only accepts the 'h', now since your cin statement is prior to the loop you will not be able to enter any other character and since this character is not a vowel nothing is printed, but the program will never end because you never satisfy your loop exit clause.

Jim

This post has been edited by jimblumberg: 27 September 2012 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Vowels not being counted?

Posted 27 September 2012 - 08:44 PM

View Postjimblumberg, on 28 September 2012 - 09:11 AM, said:

Using parentheses when defining a variable is perfectly acceptable in a C++ program.
int a(100); // Create the variable a and initialize it to 100.

It is called constructor initialization and it is acceptable.


Jim


Thank you for that information Sir :), but is it a good programming practice to use a variable as a constructor?

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#7 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Re: Vowels not being counted?

Posted 27 September 2012 - 08:51 PM

View Postjimblumberg, on 27 September 2012 - 08:44 PM, said:

A char can only accept one character, so when you try to type in "hello" it only accepts the 'h', now since your cin statement is prior to the loop you will not be able to enter any other character and since this character is not a vowel nothing is printed, but the program will never end because you never satisfy your loop exit clause.

Jim

Oh, ok. That makes sense. But it now outputs the number as 0 instead of one. Does this mean that I need to add va++ +1 ?
I'm sorry for the additional questions...
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Vowels not being counted?

Posted 27 September 2012 - 08:58 PM

View Postraghav.naganathan, on 27 September 2012 - 10:44 PM, said:

Thank you for that information Sir :), but is it a good programming practice to use a variable as a constructor?

It is acceptable practice. The key is consistency, if you consistently use this method then I consider it a good practice. But if in one place you use this notation and another you use the assignment operator then I would consider your program marginal. Also I would consider your program marginal if you didn't initialize your variables when you declared them.

View Postmoot, on 27 September 2012 - 10:51 PM, said:

Oh, ok. That makes sense. But it now outputs the number as 0 instead of one. Does this mean that I need to add va++ +1 ?
I'm sorry for the additional questions...


The problem is with your print out. You print the character before you increment the value so it shows the previous value. To fix this problem you can use the pre-increment ++va instead of the post-increment va++.

Jim

This post has been edited by jimblumberg: 27 September 2012 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#9 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Re: Vowels not being counted?

Posted 27 September 2012 - 08:59 PM

Hm, I realized my program was wrong in that there are O's where U's are supposed to be and the I has been forgotten, etc.
Was This Post Helpful? 0
  • +
  • -

#10 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Re: Vowels not being counted?

Posted 27 September 2012 - 09:38 PM

Quote

but the program will never end because you never satisfy your loop exit clause.

Going back to this, the loop exit clause is correct as the "stuff" between the parentheses after "while," correct?

View Postjimblumberg, on 27 September 2012 - 08:58 PM, said:

The problem is with your print out. You print the character before you increment the value so it shows the previous value. To fix this problem you can use the pre-increment ++va instead of the post-increment va++.

Jim

Ok, then to make things clean, I should really be having the counter equal to a variable. And not use cout or else "Number of __" will display each time the loop runs. Then once it is out of the loop, I can write the sum of each vowel. maybe?
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Vowels not being counted?

Posted 27 September 2012 - 09:49 PM

Just use the pre-increment operator (++a) and it will correctly add and print out your numbers. Or if you don't like the pre-increment operator put the increment before the printout:
   va++;
 cout << "Number of a's: " << va << endl;


Quote

Going back to this, the loop exit clause is correct as the "stuff" between the parentheses after "while," correct?

No your exit clause is not correct. Your loop will never exit. You are using the wrong logical operator, you should be using the and operator&&.


If you have further questions please post your modified code.

Jim
Was This Post Helpful? 1
  • +
  • -

#12 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Re: Vowels not being counted?

Posted 27 September 2012 - 10:06 PM

I believe I have to correct code now, thank you so very much and I apologize for not posting my code each time it was modified.

May I ask why it was necessary to use the and operator? I had thought that the or operator was to be used due to the fact that either '.' or '?' was to stop the loop.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Vowels not being counted?

Posted 27 September 2012 - 10:21 PM

The problem with the OR operator in that instance is because you were using the negation operator!. So with the following equation:
c != '.' or c != '?' 

if c is not equal to either the while will execute. But if c is equal to '.' or '?' the while will still execute because if c == '.' it is not equal to '?', remember you're using the OR operator so the not equal to '?' will cause the while to execute.

Jim

This post has been edited by jimblumberg: 27 September 2012 - 10:21 PM

Was This Post Helpful? 1
  • +
  • -

#14 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Re: Vowels not being counted?

Posted 27 September 2012 - 10:33 PM

Oh. That actually makes a lot of sense. Thank you so much! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1