C++ problem solving, HELP....PLEASE

This is a discussion on C++ problem solving, HELP....PLEASE within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm working on my first project and feel some confusing, please advice me, very appreciate. here is the ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    1

    Question C++ problem solving, HELP....PLEASE

    Hi all, I'm working on my first project and feel some confusing, please advice me, very appreciate.

    here is the problem:

    Write a program to do the following:
    1. Input a series of string into an array. Let the user specify how many strings are to be input.
    2. Write a function to return the number of vowels in a string.
    3. Output each word of a string on a separate line. Assume that the words are separated by a single blank with a period at the end of the string.

    And here is my work. It look like I make the #2 and #3 but blind in #1.

    #include <iostream>
    #include <fstream> // For file I/O
    #include <string> // For string class
    #define MAXLEN 50


    using namespace std;

    int nstr; // the number of string
    int PrintString(char*);
    int CountVowel(char*);

    int main ()
    {


    char string[MAXLEN];

    cout << "Enter the sentence:"<< endl;

    cin.getline(string,MAXLEN);
    cout << "The sentence '" << string << "' has " << CountVowel(string) <<" vowels." << endl;
    cout << "The words are: "<<endl;
    cout << PrintString(string) <<endl;

    return 0;
    }

    //countwovel function count the number of vowels in each string

    int CountVowel(char* str)
    {
    int nCount = 0;

    for(int i=0; str[i] != '\0'; ++i)
    {
    if( str[i]=='A'|| str[i]=='a'
    || str[i]=='E'|| str[i]=='e'
    || str[i]=='I'|| str[i]=='i'
    || str[i]=='O'|| str[i]=='o'
    || str[i]=='U'|| str[i]=='u')
    ++nCount;
    }
    return nCount;
    }

    //This function print out the individual words of the string on each line


    int PrintString(char* str)
    {
    int nPrint = 0;

    for( int j=0; str[j] !='\0';++j)
    if (str[j]!=' ')
    cout<<str[j];
    else if (str[j]==' ')
    cout<<endl;

    return nPrint;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Cool Hi, there !

    my English is very bad, therefore I may misunderstand what you mentioned. But let me try...

    // #1

    int N;
    char* arrOfString[20]; // 20 for example

    cout<<"enter number of your string :"<<endl;
    // your code: accept N

    for (int counter = 0; counter < N; counter++)
    {
    cout<<"enter string No."<<counter<<" :"<<endl;
    // your code: use cin.getline() to accept arrOfString[counter]
    }

    /* Sorry for uncompleted code.
    You can use defined "string" class instead of C-string.
    And, you may use dynamic string allocation for unlimited array
    (indeed, limited by system memory).

    Hope I didn't make you so disappointed :.).
    */

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Cool Hi, there !

    my English is very bad, therefore I may misunderstand what you mentioned. But let me try...

    // #1

    int N;
    char* arrOfString[20]; // 20 for example

    cout<<"enter number of your string :"<<endl;
    // your code: accept N

    for (int counter = 0; counter < N; counter++)
    {
    cout<<"enter string No."<<counter<<" :"<<endl;
    // your code: use cin.getline() to accept arrOfString[counter]
    }

    /* Sorry for uncompleted code.
    You can use defined "string" class instead of C-string.
    And, you may use dynamic string allocation for unlimited array
    (indeed, limited by system memory).

    Hope I didn't make you so disappointed :.).
    */

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Whenever you don't know the size of an array at compile time you need to use dynamic memory based on the operator new (or new []), and then the operator delete (or delete[]) when you are done with the array.

    Therefore, in your code do something like this:


    int number;
    cout << "How many strings do you wish to enter" << endl;
    cin >> number;

    char ** input;
    input = new char *[number];
    for(int i = 0; i < number; i++
    {
    input[i] = new char[MAXLEN];
    }

    etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. JOB: C/C++ Developer with problem solving skills
    By VoltRecruiter in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 01-26-2006, 12:25 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Sign-up Thread: Problem Solving #1
    By ygfperson in forum Contests Board
    Replies: 15
    Last Post: 01-26-2003, 02:55 AM

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