why isn't this working

This is a discussion on why isn't this working within the C++ Programming forums, part of the General Programming Boards category; why isn't my program working?? char ch; int countspace=0; cout << "Please enter a line of text, "; cout << ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    why isn't this working

    why isn't my program working??

    char ch;
    int countspace=0;
    cout << "Please enter a line of text, ";
    cout << "followed by a return: ";
    cout << endl;
    cin.get(ch);
    while (ch == '\n')
    {
    if (ch == ' ')
    countspace++;
    if(countspace > 0 && ch != ' ')

    ch= toupper(ch);
    cout <<ch;
    countspace = 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: why isn't this working

    Originally posted by archie
    why isn't my program working??

    char ch;
    int countspace=0;
    cout << "Please enter a line of text, ";
    cout << "followed by a return: ";
    cout << endl;
    cin.get(ch);
    while (ch == '\n')
    {
    if (ch == ' ')
    countspace++;
    if(countspace > 0 && ch != ' ')

    ch= toupper(ch);
    cout <<ch;
    countspace = 0;
    }
    You are only calling get() once...therefore it will work for 1 char....not a string......

    Also, you would do better to identify the exact problem you have......."This doesnt work" is not very helpful

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> char ch;

    Hint, this statement declares a single character, not a string. You want an array of char.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Ooops, bit of concurrence!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    7
    We are not supposed to use arrays or strings only char.



    Originally posted by adrianxw
    >>> char ch;

    Hint, this statement declares a single character, not a string. You want an array of char.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    why isn't my program working??

    char ch;
    int countspace=0;
    cout << "Please enter a line of text, ";
    cout << "followed by a return: ";
    cout << endl;
    cin.get(ch);
    while (ch == '\n')
    {
    if (ch == ' ')
    countspace++;
    if(countspace > 0 && ch != ' ')

    ch= toupper(ch);
    cout <<ch;
    countspace = 0;
    }
    First, counting spaces isn't safe unless you know what the format of the input will be like, what if I entered a line with each word separated by two spaces? To be safe and simple here is what you do, ask the user to enter a series of words to start, they really should know what to do or the program won't be useful. Use cin.get to read the first character of the input and test if it's a vowel, if it is then change the case to upper.

    Go into a loop to check for a newline and test for a space, if it's a space then print one space and loop until you don't have a space, test that for vowel and if it is change the case. If it isn't just print the character. If it's not a space at all, print the character and get a new one, at the end print a newline and you're done.
    Code:
    #include <iostream>
    using namespace std;
    
    static int isVowel ( char ch ) 
    { 
      ch = toupper ( ch ); 
      return ( ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U' ) ? 1 : 0; 
    }
    
    int main ( void )
    {
      char ch;
      cout<<"Enter a series of words"<<endl;
      cin.get( ch );
      if ( isVowel ( ch ) ) ch = toupper ( ch );
      while ( ch != '\n' ) {
        if ( isspace ( ch ) ) {
          cout<<" ";
          while ( isspace ( ch ) ) cin.get( ch );
          if ( isVowel ( ch ) ) {
            ch = toupper ( ch );
            cout<<ch;
          }
          else
            cout<<ch;
        }
        else 
          cout<<ch;
        cin.get( ch );
      }
      cout<<endl;
      return EXIT_SUCCESS;
    }
    Just in case this is a homework question, you don't want to turn in my code. Teachers know if you didn't write it, so beware

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    7
    Originally posted by Prelude

    First, counting spaces isn't safe unless you know what the format of the input will be like, what if I entered a line with each word separated by two spaces? To be safe and simple here is what you do, ask the user to enter a series of words to start, they really should know what to do or the program won't be useful. Use cin.get to read the first character of the input and test if it's a vowel, if it is then change the case to upper.

    Go into a loop to check for a newline and test for a space, if it's a space then print one space and loop until you don't have a space, test that for vowel and if it is change the case. If it isn't just print the character. If it's not a space at all, print the character and get a new one, at the end print a newline and you're done.
    Code:
    #include <iostream>
    using namespace std;
    
    static int isVowel ( char ch ) 
    { 
      ch = toupper ( ch ); 
      return ( ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U' ) ? 1 : 0; 
    }
    
    int main ( void )
    {
      char ch;
      cout<<"Enter a series of words"<<endl;
      cin.get( ch );
      if ( isVowel ( ch ) ) ch = toupper ( ch );
      while ( ch != '\n' ) {
        if ( isspace ( ch ) ) {
          cout<<" ";
          while ( isspace ( ch ) ) cin.get( ch );
          if ( isVowel ( ch ) ) {
            ch = toupper ( ch );
            cout<<ch;
          }
          else
            cout<<ch;
        }
        else 
          cout<<ch;
        cin.get( ch );
      }
      cout<<endl;
      return EXIT_SUCCESS;
    }
    Just in case this is a homework question, you don't want to turn in my code. Teachers know if you didn't write it, so beware

    -Prelude

    Hey Thanka aton,
    Just one more question.Instead of using the function isvowel how can i find out if the first character enetered is a vowel??

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    if((toupper(ch) == 'A') || (toupper(ch) == 'E') || (toupper(ch) == 'I') || (toupper(ch) == 'O') || (toupper(ch) == 'U'))

    which is bascially what prelude's function does anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 04:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 05:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21