ok need some help plz

This is a discussion on ok need some help plz within the C++ Programming forums, part of the General Programming Boards category; ok hello again I got my program done just having trouble with something. I need to find all instances of ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Exclamation ok need some help plz

    ok hello again

    I got my program done just having trouble with something. I need to find all instances of a letter in a string, ex: Alpha: has two 'a'. My program only reads the first 'a'. This is what my prog looks like.


    Code:
    #include<iostream>       //Includes the I/O library
    
    using namespace std;
    char loopcount;
    int tries;               //All the different type of global variables used
    string word;
    char letter;
    string plar;
    int i;
    char n;
    int main()
    {
    cout<< "Please enter you name: ";    // Enters the plaers name
    cin>>plar;
     
    cout << "Please enter a word: ";     // Enters a word
    cin>> word;
    
    //Displays alot of end lines to hide the word that is to be guessed
    cout<< endl<<endl<<endl<<endl<<endl<<endl;
    cout<< endl<<endl<<endl<<endl<<endl<<endl;
    cout<< endl<<endl<<endl<<endl<<endl<<endl;
    cout<< endl<<endl<<endl<<endl<<endl<<endl;
    cout<< endl<<endl<<endl<<endl<<endl<<endl;
    cout<< endl<<endl<<endl<<endl<<endl<<endl;
    cout<< endl<<endl<<endl<<endl<<endl<<endl;
    cout<< endl<<endl<<endl<<endl<<endl<<endl;
    cout << "Please enter the amount of tries: "; //Enters the amount of tries
     
    cin>> tries;
    
    string guess(word.size(), '*');        // use the word and converts to '*'
    cout << guess <<endl;
    
    for (i = tries; i>=0; i--)        // Used a forloop to decrement the tries
    {
    cout<<"Please enter a letter to be guessed: ";
    cin>> letter;
    
    int pos = word.find(letter); // Find if the letter is in the word and
                                 // displays it
     
    guess[pos] =  letter;
    if (pos != string::npos)
    {
    cout<<"Correct. You now have: "<<i<< " left"<<endl;
    cout <<guess<<endl;
    }
    else
    cout<<"Wrong! You now have: "<<i<< " left"<<endl;
    if (tries == 0)
    {
    cout<<"Sorry you guessed wrong, the word was: "<<word<<endl;
    return 1;
    }
     
    }
    //If word was guessed correctly then a happy face will be displayed
    if (guess == word)
    {
    cout<<"Great Job! "<<plar<< " YOU WIN!"<<endl;
    return 1;
    }
     
    }
    cout <<plar<< " YOU LOSE! :(  The word was: "<< word<<endl;
    return 0;
    }
    }
    what u guys think.. man this gets me anoyed lol
    thanks again

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    A few things to note:

    1 Your program layout needs a rethink.

    2 Indent your code - it makes reading and understanding it much better

    3 Please, do not use global variables like this! Place all your variables in main, making them
    local. Only use global if really have to, or use a static variable instead.

    4 All your endls could be a for loop

    Code:
    for ( int i = 0; i <= 10; i++ )
              {
                 cout << "\n\n";
              }

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Only use endl if you are done using cout<< for now.

    use "\n" instead which is the newline character.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Exclamation thanks for ur advice

    thanks for ur advice. but what u think i should do abt the problem Im having ?

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You need to learn about loops, check out the tutorial on this site or the section in your book.

    PS. No offence but this is a technical forum, not a chat room. Please take some time to read what you are typing and also don't use chatslang shorthand like "ur" and you will earn more respect on the board.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Exclamation also

    its easier to do it this way anyways

    Code:
    cout<<setfill(' ')<<setw(1000);
    but good idea

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    But you need loops for checking the word. Generally you just iterate through the word and you can check each letter and count them.

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Why would you show him an infinate loop program and tell him to try it out?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Talking sounds good

    so btw(by the way) does anyone that knows programming have any ideas?

    thanks

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I already answered you.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Talking oh ok thanks man

    but I know I need a loop. But I cant seem to get it to work.

    So does anyone that knows or understands C++ have any GOOD Ideas.

    thanks

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Have you read my points I gave you? Look in a good book and read up on program layout. Getting the layout looking right is half the program done.

    When you said any ideas.... what did you mean exactly?

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    BTW I think some people may take offence to saying that above...

    Code:
    GOOD ideas

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Smile alright guys

    I dont mean to be rude, but last time I was on here I had some good help.
    I guess you dont get help twice in a row.

    anyways ill try asking my prof later. She'll Probably have a better Idea on how to go about this.

    Thanks

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Everybody on this thread has attempted to be of help to you. Reading your above reply makes me feel like you have not taken it in or do not care what we have said. If this is the case, then perhaps asking your prof is the answer like you said.

    I am not being rude to you, but every single person on this board will try to help you, and all we ask in return is a little gratitude.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 10:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 11:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 01:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 09:44 AM

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