Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ Wheel of Fortune Extra credit


ghost's Avatar
0 0

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?

// 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;
}

ghost's Avatar
0 0

Not to be a bitch, but… if you can't solve it yourself, do you really feel that you deserve extra credit?


GTADarkDude's Avatar
Member
0 0

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.


ghost's Avatar
0 0

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.


Arabian's Avatar
Member
0 0

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.


JDavidC's Avatar
Member
0 0

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.