Can't figure this out

This is a discussion on Can't figure this out within the C Programming forums, part of the General Programming Boards category; Hello. This is my program and I can't figure out why the input is the way that it is. The ...

  1. #1
    Unregistered
    Guest

    Can't figure this out

    Hello. This is my program and I can't figure out why the input is the way that it is. The program is supposed to accept a string of up to 100 characters from the user and calculate how many vowels, words, sentences, and double letters(like the "tt" in "letters") there are in the message. The message and the results are then displayed to the user. This is what I have so far:#include <iostream.h>
    #include <iomanip.h>

    int main (void)
    {
    char message[100];
    int count,vowels,words,sentences,double_letters;
    cout<<"Enter your message now. Maximum of 100 characters."<<endl;
    cin>>message;
    //Initialization
    vowels=words=sentences=double_letters=0;
    for(count=0;count<100;count++)
    {
    switch(message[count])
    {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
    vowels++;
    break;
    case 32: //ASCII for a space
    words++;
    break;
    case '.':
    sentences++;
    break;
    }
    if((message[count]==message[count+1])&&(message[count]!=32))
    {
    double_letters++;
    }
    }
    cout<<"Message: "<<message<<endl;
    cout<<"Vowels: "<<vowels<<endl;
    cout<<"Words: "<<words<<endl;
    cout<<"Sentences: "<<sentences<<endl;
    cout<<"Double letters: "<<double_letters<<endl;
    return 0;
    }

  2. #2
    Unregistered
    Guest

    Btw

    By the way I am using Microsoft Visual C++ 6.0 and this is the output that I am currently getting:

    Enter your message now. Maximum of 100 characters.
    This message is too short. It does not have many letters.
    Message:This
    Vowels:1
    Words:0
    Sentences:0
    Double Letters:94
    Press any key to continue

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Replace the line:
    Code:
    for(count=0;count<100;count++)
    with:
    Code:
    for (count = 0; count < strlen(message); count ++)

  4. #4
    Unregistered
    Guest

    Thanks a million!!

    Thanks! Worked like a charm!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your code is C++, please post on that board in future!

    Also:
    >for (count = 0; count < strlen(message); count ++)
    this is inefficient, as strlen is called every time you go round the loop. It'd be better to store the length in a variable, and use that in the for statement.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 04:22 PM
  2. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  3. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 12:47 AM
  4. My library is failing and I cannot figure out why...
    By DerelictDream in forum C++ Programming
    Replies: 7
    Last Post: 08-15-2005, 03:47 PM
  5. Can't figure out strcmp error
    By Fyodorox in forum C++ Programming
    Replies: 10
    Last Post: 05-08-2002, 10:18 AM

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