C++ do-while loop of vowels won't work!

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 8650 Views - Last Post: 10 August 2011 - 06:19 AM Rate Topic: -----

#1 opawix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-March 11

C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 04:52 PM

guys can someone help me with this problem:

i can't let the vowels cout or print with this code (using do-while):
char letter;

int main()
{
    do{
        cout<<letter;
        letter++;
    }
    while((letter!='b')||(letter!='c')||(letter!='f')||(letter!='g')||(letter!='h')||(letter!='j')||
          (letter!='k')||(letter!='l')||(letter!='m')||(letter!='n')||(letter!='p')||(letter!='q')||
          (letter!='r')||(letter!='s')||(letter!='t')||(letter!='v')||(letter!='w')||(letter!='x')||
          (letter!='y')||(letter!='z'))
}


any help will do guys. thanks a lot. :D

This post has been edited by no2pencil: 07 August 2011 - 04:54 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: C++ do-while loop of vowels won't work!

#2 Absolute.  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 38
  • Joined: 06-August 11

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:08 PM

 while((letter!='b')||(letter!='c')||(letter!='f')||(letter!='g')||(letter!='h')||(letter!='j')||
          (letter!='k')||(letter!='l')||(letter!='m')||(letter!='n')||(letter!='p')||(letter!='q')||
          (letter!='r')||(letter!='s')||(letter!='t')||(letter!='v')||(letter!='w')||(letter!='x')||
          (letter!='y')||(letter!='z'))



you need a semi-colon after the while part in do-while loops. I would try to help you but I don't fully understand what you are trying to do

This post has been edited by Absolute.: 07 August 2011 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:08 PM

your doing this wrong. your whole idea is off infact. let's look at what your doing

1) you loop stops as soon as a vowel is reached so it only prints 'a'(assuming it starts at 'a')
2) you must be using a global variable
3) there is nothing that tells it to stop
4) rather than checking for a vowel you check to see if it is any other character in the alphabet. why check for 20 things when you can check for just 5?

now let's look at this for a bit. im going to assume that your attempt is to use loops to print out all the vowels. this is probably better done with an array but but we won't mess with that for now.

let's look at some pseudocode for this.

for letter from 'a' to 'z' do
    if letter is a vowel then
        print letter


this solves your issues doing the following.

1) it starts at 'a' and keeps going until it get's to 'z'
2) the variable is declared for this purposes in local scope(like for(int i; ; ))
3) it stops once it get's to 'z'
4) it only checks to see if the letter is a one of 5(sometimes 6 ;) letters rather than check to see if it's not one of 20 letters

now if you up for using strings(arrays) there is a better way to print out the vowels.
char* vowels = "aeiouy";
std::cout<<vowels; 


however this most likely isn't how you wanted to do this but it dose the same thing never the less :)

This post has been edited by ishkabible: 07 August 2011 - 05:10 PM

Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:11 PM

At what point do you even assign letter a value?
What data type is letter? It must be some type of numeric if you can increment it with ++, therefore I wonder how you expect to compare it to a character as you are doing.

When do you expect this loop to finish? There is no mechinism to stop it. (No check to see if letter > 10000 for example)

I think you need to show us the whole code.
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:13 PM

actually tlhIn`toq in C/C++ characters are are just really small integers.

the following works just fine:

char c = 'a';
++c;
std::cout<<c;



this will print 'b'
Was This Post Helpful? 0
  • +
  • -

#6 opawix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-March 11

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:14 PM

View PostAbsolute., on 07 August 2011 - 05:08 PM, said:

 while((letter!='b')||(letter!='c')||(letter!='f')||(letter!='g')||(letter!='h')||(letter!='j')||
          (letter!='k')||(letter!='l')||(letter!='m')||(letter!='n')||(letter!='p')||(letter!='q')||
          (letter!='r')||(letter!='s')||(letter!='t')||(letter!='v')||(letter!='w')||(letter!='x')||
          (letter!='y')||(letter!='z'))



you need a semi-colon after the while part in do-while loops. I would try to help you but I don't fully understand what you are trying to do


i miss to put the semi colon in my code, but still won't work with this code. i would like to make an output of vowels using a do-while loop.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:17 PM

Also, the loop won't continue as soon as you hit 'b'.
The while comparison will fail because letter will equal 'b' and execution will drop out of the loop and move to the next line after the loop.

View Postopawix, on 07 August 2011 - 06:14 PM, said:

View PostAbsolute., on 07 August 2011 - 05:08 PM, said:

 while((letter!='b')||(letter!='c')||(letter!='f')||(letter!='g')||(letter!='h')||(letter!='j')||
          (letter!='k')||(letter!='l')||(letter!='m')||(letter!='n')||(letter!='p')||(letter!='q')||
          (letter!='r')||(letter!='s')||(letter!='t')||(letter!='v')||(letter!='w')||(letter!='x')||
          (letter!='y')||(letter!='z'))



you need a semi-colon after the while part in do-while loops. I would try to help you but I don't fully understand what you are trying to do

i miss to put the semi colon in my code, but still won't work with this code. i would like to make an output of vowels using a do-while loop.


Please don't waste our time with code that won't compile and run. Showing us some version other than what you copy/paste directly from your IDE offers too many places for transcription errors just like this one.

Please copy/paste the entire file here as asked earlier. Obviously you have several things we need to see or I wouldn't have asked for them.

Did you even address any of the other problems I already mentioned?

This post has been edited by tlhIn`toq: 07 August 2011 - 05:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 opawix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-March 11

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:18 PM

#include <iostream>
using namespace std; 
 
int main() 
{ 
  char letter='a';
  do
  {
       cout<<letter;
        ++letter;

  }while(letter<='z');
 system ("pause");
  return 0; 
}



heres another code outputting the alphabets but i would like to output vowels in do-while loop.

This post has been edited by ishkabible: 07 August 2011 - 05:20 PM
Reason for edit:: please post code in code tags

Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:20 PM

all you have to do now is check to see if letter is a vowel on each loop.
Was This Post Helpful? 0
  • +
  • -

#10 opawix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-March 11

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:21 PM

View PosttlhIn`toq, on 07 August 2011 - 05:17 PM, said:

Also, the loop won't continue as soon as you hit 'b'.
The while comparison will fail because letter will equal 'b' and execution will drop out of the loop and move to the next line after the loop.

View Postopawix, on 07 August 2011 - 06:14 PM, said:

View PostAbsolute., on 07 August 2011 - 05:08 PM, said:

 while((letter!='b')||(letter!='c')||(letter!='f')||(letter!='g')||(letter!='h')||(letter!='j')||
          (letter!='k')||(letter!='l')||(letter!='m')||(letter!='n')||(letter!='p')||(letter!='q')||
          (letter!='r')||(letter!='s')||(letter!='t')||(letter!='v')||(letter!='w')||(letter!='x')||
          (letter!='y')||(letter!='z'))



you need a semi-colon after the while part in do-while loops. I would try to help you but I don't fully understand what you are trying to do

i miss to put the semi colon in my code, but still won't work with this code. i would like to make an output of vowels using a do-while loop.


Please don't waste our time with code that won't compile and run. Showing us some version other than what you copy/paste directly from your IDE offers too many places for transcription errors just like this one.

Please copy/paste the entire file here as asked earlier. Obviously you have several things we need to see or I wouldn't have asked for them.

Did you even address any of the other problems I already mentioned?




sorry for that sir, there's a full code down.
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5316
  • View blog
  • Posts: 27,220
  • Joined: 10-May 07

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:22 PM

I would use some functionality from the string header, namely strcmp, & look for vowels packed in an array, instead of 7 conditional if statements.
Was This Post Helpful? 0
  • +
  • -

#12 cmh0114  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 143
  • Joined: 03-January 10

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:23 PM

View Postopawix, on 07 August 2011 - 06:18 PM, said:

#include <iostream>
using namespace std; 
 
int main() 
{ 
  char letter='a';
  do
  {
       cout<<letter;
        ++letter;

  }while(letter<='z');
 system ("pause");
  return 0; 
}



heres another code outputting the alphabets but i would like to output vowels in do-while loop.


If you only want to print vowels, then that would work, just add an if statement to check if the letter is a vowel before you print it. Make a String of the vowels, then add:
if(vowelString.contains(letter)) cout<<letter;


Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:26 PM

View Postishkabible, on 07 August 2011 - 06:13 PM, said:

actually tlhIn`toq in C/C++ characters are are just really small integers.

the following works just fine:

char c = 'a';
++c;
std::cout<<c;



this will print 'b'


Thanks. I was trying to get the OP to explain his code. I don't think he actually understands it. He still hasn't shown the full version of his original code. But has shown "another code". I think he is just copy/pasting code from his text book or the web and hoping someone here will fix it up and he can turn it in to his teacher Monday morning.
Was This Post Helpful? 0
  • +
  • -

#14 opawix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-March 11

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:28 PM

while statement says not equal to any letters so i assume that cout<<letter would output the letters not mentioned in while.
Was This Post Helpful? 0
  • +
  • -

#15 opawix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-March 11

Re: C++ do-while loop of vowels won't work!

Posted 07 August 2011 - 05:33 PM

View PosttlhIn`toq, on 07 August 2011 - 05:26 PM, said:

View Postishkabible, on 07 August 2011 - 06:13 PM, said:

actually tlhIn`toq in C/C++ characters are are just really small integers.

the following works just fine:

char c = 'a';
++c;
std::cout<<c;



this will print 'b'


Thanks. I was trying to get the OP to explain his code. I don't think he actually understands it. He still hasn't shown the full version of his original code. But has shown "another code". I think he is just copy/pasting code from his text book or the web and hoping someone here will fix it up and he can turn it in to his teacher Monday morning.



i'am not copying any codes in the web sir. i make it by my self. is that just the code have locking keys to a right output. that's why i'am asking if you or someone knows about it. heres my orignal code.


<iostream>
using namespace std;
int main()
{
int letter;
do
{
cout<<letter;
letter++;
}while((letter!='b')||(letter!='c')||(letter!='f')||(letter!='g')||(letter!='h')||(letter!='j')||

         (letter!='k')||(letter!='l')||(letter!='m')||(letter!='n')||(letter!='p')||(letter!='q')||

         (letter!='r')||(letter!='s')||(letter!='t')||(letter!='v')||(letter!='w')||(letter!='x')||

         (letter!='y')||(letter!='z'));
system("pause");
return 0;
}


This post has been edited by no2pencil: 07 August 2011 - 05:46 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2