Results 1 to 2 of 2

Thread: how to fix my loop

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    18

    how to fix my loop

    With the following code i keep getting an infinite loop, im not even sure if the following code is right, but what i am trying to do is have the user type in a sentence, then a letter, and the program finds how many times the letter is entered, how would i accomplish this? thank you

    #include <iostream.h>
    #include <apstring.h>
    void main()
    {
    apstring sentence;
    char letter;
    int x;
    cout<<"enter a sentence ";
    getline(cin,sentence);
    cout<<"enter a letter ";
    cin>>letter;
    x=0;
    do
    {cout<<sentence.find(letter);
    x=x+1;}
    while (letter>=0);
    cout<<"The letter "<<letter<<" appears "<<x<<" times in the sentence";
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    do
    {cout<<sentence.find(letter);
    x=x+1;}
    while (letter>=0);

    I don't know exactly how apstring works, but I'm sure it won't alter letter, therefore letter will never be < 0.

    Anyway you should use <string> and <iostream> and
    using namespace std;
    instead of <iostream.h> and <apstring.h>.

    iostream.h is deprecated and apstring.h simply is crap.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width