-
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
-
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";
}
-
Only use endl if you are done using cout<< for now.
use "\n" instead which is the newline character.
-
thanks for ur advice
thanks for ur advice. but what u think i should do abt the problem Im having ?
-
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. :D
-
also
its easier to do it this way anyways
Code:
cout<<setfill(' ')<<setw(1000);
but good idea
-
But you need loops for checking the word. Generally you just iterate through the word and you can check each letter and count them.
-
Why would you show him an infinate loop program and tell him to try it out?
-
sounds good
so btw(by the way) does anyone that knows programming have any ideas?
thanks :)
-
-
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
-
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?
-
BTW I think some people may take offence to saying that above...
-
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
-
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.