| Author |
C++ Wheel of Fortune Extra credit |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
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;
}

Edited by vegeta_man111 on 13-03-11 05:35 |
|
| Author |
RE: C++ Wheel of Fortune Extra credit |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
Not to be a bitch, but... if you can't solve it yourself, do you really feel that you deserve extra credit?
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth! |
|
| Author |
RE: C++ Wheel of Fortune Extra credit |
GTADarkDude
Member

Posts: 142
Location: The Netherlands
Joined: 23.02.08 Rank: God |
|
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 |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
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
Member

Posts: 320
Location: inside you.
Joined: 22.09.10 Rank: God |
|
|
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.
Hare Lambda!
Edited by Arabian on 14-03-11 10:14 |
|
| Author |
RE: C++ Wheel of Fortune Extra credit |
JDavidC
Member
Posts: 8
Location:
Joined: 30.01.11 Rank: Elite |
|
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 |
|