| | |
string and char functions
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2007
Posts: 46
Reputation:
Solved Threads: 0
I would like to make this first function work as the one were i opened it from a file. In this first one i am using arrays the second one uses strings. ignore the masking of the password in the first one i want it to do that and then check if the password typed in is correct.
c Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include <ctype.h> using namespace std; int main() { int ch; char pword[100]; int i = 0; char guess[100]; int life = 5; char key; puts ("Enter your password"); fflush(stdout); while ((ch = getch()) != EOF && ch != '\n' && ch != '\r' && i < sizeof(pword) - 1) { if (ch == '\b' && i > 0) { printf("\b \b"); fflush(stdout); i--; pword[i] = '\0'; } else if (isalnum(ch)) { putchar('*'); pword[i++] = (char)ch; } } pword[i] = '\0'; printf ("\nYou entered >%s<", pword); cout << "Player 2. Guess the word" << endl; cin >> guess; for (int i = 0 ; i < 5 ; i++) { pword[i]; } for (int j = 0 ; j < 4 ; j++) { cout << guess[j]; //prints the *'s } while (life !=0) //if lives are greater than 0 carry on else don't { cout << "\nplease enter a guess , if you don't guess it right you loose a life" <<endl; cout << "guess is : " ; cin >>key; // grab key from user for (int k = 0; k < 5; k++) //takes guess and checks it over { if (key == pword[k]) { guess[k] = pword[k]; } } for (int i = 0; i < pword[5]; ++i) { if (key!= pword[i]) { life --; cout << "You've just lost a life, you now have "<< life << "remaining\n"<<endl;; } } for (int h = 0; h < 4; h++) { cout << guess[h] ; if( key == guess[h]) { cout << "WHAT"; return 0; } } } if (life == 0) { cout << "Your out of lives, do you wish to play again?" <<endl; } return 0; } SECOND FUNCTION //headers string make_display_word(string s) // REPLACES THE STRING WITH * { string t = ""; for (int i = 0; i < s.size(); i++) t += '*'; return t; } bool ch_in_string(char ch, string s) // CHECKS IF THE CHARACTER ENTERED AND STRING ARE THE SAME { for (int i=0; i < s.size(); i++) if (ch == s[i]) return true; return false; } void enter_char(char ch, string secret, string &display) // CHECKS IF THE CHARACTER ENTERED AND STRING ARE THE SAME { for (int i = 0; i < secret.size(); i++) { if (secret[i] == ch) display[i] = ch; } } // DRAWS THE HANGMAN void play(int figure) { int chance; cout << "How many Chances do you want" << endl; cin >> chance; string word ; string q; ifstream fin; fin.open(infile); if(fin.fail()) { cerr << "What" << endl; } while(!fin.eof()) { int i; for(i = 1; i<=10;i++) { i = rand()%100; // Assuming 10,000 words in the file do { fin>> word; }while ( --i >= 0 ); // NUMBER OF WRONG GUESSES int wrong_count = 0; int figure = 1; string display_word = make_display_word(word); string already_guessed = ""; do { cout << word; cout << "Your guess so far: " << display_word << " " << "Wrong guesses: " << wrong_count << " Letters guessed: " << already_guessed << endl; if (display_word == word && wrong_count ==0) { cout << "Whoa you are great"; getch(); exit(0); } if (display_word == word) { cout << "You got it!\n"; getch(); exit(0); } if (wrong_count >= chance) { cout << "You are dead!\n" << "The correct word was" << " "<<word; getch(); exit(0); } cout << "\nGuess a letter: "; char ch; cin >> ch; ch = tolower(ch); if (!isalpha(ch)) { cout << "Only letters!\n\n"; } else { if (ch_in_string(ch, already_guessed)) { cout << "You already guessed '" << ch << "'\n"; } else { already_guessed += ch; if (ch_in_string(ch, word)) { enter_char(ch, word, display_word); } else { draw(figure); cout << "Sorry, wrong guess!\n"; wrong_count++; figure++; } } } }while(!false); } } }
Last edited by WaltP; Feb 27th, 2007 at 3:22 am. Reason: Twice I've added CODE tags. It's your turn!
I don't have a clue what question you are asking.
function ch_in_string() -- there's a lot easier way to do that. std::string has a find method
function ch_in_string() -- there's a lot easier way to do that. std::string has a find method
C Syntax (Toggle Plain Text)
bool ch_in_string(char ch, string s) { return s.find(ch) == string::npos) ? true : false; }
Last edited by Ancient Dragon; Feb 27th, 2007 at 8:12 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
I don't have a clue what question you are asking.
function ch_in_string() -- there's a lot easier way to do that. std::string has a find method
C Syntax (Toggle Plain Text)
bool ch_in_string(char ch, string s) { return s.find(ch) == string::npos) ? true : false; }
C Syntax (Toggle Plain Text)
bool char_in_strin (string s, char ch) { // return true if char is found in string return ( s.find (ch) != string::npos ? true : false ) ; }
or in a simple way:
C Syntax (Toggle Plain Text)
if ( my_string.find (my_char) != string::npos ) { std::cout << "Character was found in the string \n" ; } else { std::cout << "Character NOT found in the string \n" ; }
Last edited by ~s.o.s~; Feb 27th, 2007 at 1:11 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Standard C/C++ does not allow you to type and not echo the characters. Input is buffered which means you type and the system stores the characters until the ENTER is pressed. Then your program reads the characters.
Some -- but not all -- compilers have a way around this, but the functions are
1) not standard so not recommended
2) not necessarily the same for the compilers that do allow this
Some -- but not all -- compilers have a way around this, but the functions are
1) not standard so not recommended
2) not necessarily the same for the compilers that do allow this
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Comparing two letters in one string with another char string? (C)
- How to convert string to const char* in C (C++)
- friend functions (C++)
- VB's Left, Right, Mid Functions in C++? (C++)
- Debugging string function (C++)
- List <char *> Problem (C)
Other Threads in the C Forum
- Previous Thread: Factorial of a number
- Next Thread: Problems linking libs, I think...
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






