Follow us on Twitter!
Become the change you seek in the world. - Gandhi
Wednesday, April 16, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 17
Guests Online: 15
Members Online: 2

Registered Members: 82808
Newest Member: Krish
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ Wheel of Fortune Extra credit


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 13-03-11 01:23
I think I have the basic code done right, but seems like after I do my first guess I get a runtime error. Any suggestions?

Code

// Chapter X Assignment X
// Corey Hartshorn

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string word;
    string stars;
    char letter;
    char guess;
   
   
   
    cout <<"Input a word or phrase with a max of 20 characters:" << endl;
    getline(cin, word);
    stars = word;
 


for(int x = 0; x < word.length(); x++)
{
        letter = stars.at(x); // gets each letter by itself
        if (isalpha(letter))// checks to make sure is a letter
           {
           stars.at(x) = '*'; // places a '*' wherever a letter is
           }
           else if (letter == ' ')
           {
           stars.at(x) = '-'; // places a '-' wherever a space is
           }
           else
           {
           cout << letter << " is not a letter" << endl;
           break; // declares not a correct variable and ends the 'for' loop
           }
}
   
cout << endl << "Press the ENTER key to continue..." << endl;
cin.sync();
cin.get(); // pause
   
for (int i = 0; i <= 25; i++)// moves the screen down so you dont see 'word'
{
    cout << "##################################################" << endl;
}
   

cout << "The phrase is " << stars << endl; //outputs phrase in stars
cout << endl;
 
while(stars.find('*') != string::npos)
{
    cout << "What's your guess? "<<endl;
    cin >> guess;
 

    if(word.find(guess) != string::npos)
    {
                                 
        for (int q = 0; q <= word.length(); q++)
        {
            if (word.at(q) == guess)
            {
                           stars.at(q) = word.at(q);
            }
        }
    }
}

   
   
     
    cout << endl << "Press the ENTER key to continue..." << endl;
    cin.sync();
    cin.get(); // pauses at the end
    return 0;
}








Edited by on 13-03-11 05:35
Author

RE: C++ Wheel of Fortune Extra credit


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 13-03-11 10:26
Not to be a bitch, but... if you can't solve it yourself, do you really feel that you deserve extra credit?


Author

RE: C++ Wheel of Fortune Extra credit

GTADarkDude
Member



Posts: 142
Location: The Netherlands
Joined: 23.02.08
Rank:
Newbie
Posted on 13-03-11 11:28
Dude, you did exactly the same thing wrong in one of your previous topics. You know, you're kinda meant to learn from those things. So you can do your homework yourself, in the future...
For the last time: when a string has length 5, indexes 0 up to and including 4 are usable. Not 5. Because 0,1,2,3,4,5 makes length 6. Right, got it? Now fix your code yourself.


...
- - -
Author

RE: C++ Wheel of Fortune Extra credit


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 14-03-11 05:59
well, considering I am allowed to ask my fellow classmates for input, but none of them have above a D+, I figured I would ask you guys. Sorry that I am trying to learn, but this is how I do it. I do what I can until i hit a dead end and then get advice and continue.


Author

RE: C++ Wheel of Fortune Extra credit

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 14-03-11 10:14
vegeta_man111 wrote:
well, considering I am allowed to ask my fellow classmates for input, but none of them have above a D+, I figured I would ask you guys. Sorry that I am trying to learn, but this is how I do it. I do what I can until i hit a dead end and then get advice and continue.


It sounds like either you or your professor have a hard time communicating simple concepts. In either case, while it was good of you to come here, the quality of the error involved shows that you seriously need to do some extra reading to supplement your course studies.


G'bye y'all! I was an asshole, So korg banned me.

Edited by Arabian on 14-03-11 10:14
Author

RE: C++ Wheel of Fortune Extra credit

JDavidC
Member

Your avatar

Posts: 8
Location:
Joined: 30.01.11
Rank:
Guest
Posted on 18-03-11 00:22
Google terms like: C++ array bounds off-by-one errors

Bear in mind that arrays in C++ are 0-based, they start from 0, NOT 1. That means you go from 0 to n - 1 when you have an array of n elements. There is an off-by-one error in one of your loops because you go from 0 to n (although n is something else in your program, but I don't want to simply tell you explicitly where the problem lies, you have to be able to learn this so it's second nature, this post and the stuff I've given you to Google should help).

Be sure to test your program once you've fixed the error to see if it works properly.

Edited by JDavidC on 18-03-11 00:22