Thread: Need Help with C++

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    10

    Need Help with C++

    Hello everyone. Let me start by sayying thanks for taking your time to read my post and help answer my questions. I've been trying to learn C++ by myself. I've been reading the tutorials over and over till I understand them as best as possiable. I've gotten to 'Strings' so far and I've have not really made any code of my own. I've done all of the tutorials code and understood how the worked. But when I try to piece all that I've learned together to make my own could I seem to get stopped in my tracks.. Anyone have any ideas on how to go about learning/making own easy little programs as you go? I'm trying to learn myself and I dont have anyone to really help me, but I am trying my hardest and will continue to keep trying. I'm trying to make a guessing game, but I seem to get stopped on how to make the game keep going after the frist guess. Here is the code..
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	char guess1 [13];
    
    	cout<<"Welcome to David's Guessing game. The game is simple all you have to do is guess the word I am thinking of.\n";
    
    		cin.getline( guess1, 13 );
    		if (strcmp (guess1, "sha boooiinng") == 0)
    			cout<<"Wow you won on you'r frist try. You must have cheated\n";
    		else
    			cout<<"You suck you have to try again. Altough you cant cause my programming skills lack the stuff to do it";
    
    	cin.get();
    }

    Can someone help me learn how to make the game like keep going so you can keep guessing? Maybe open my eyes? Also if anyone is feeling really nice and can help me on like AIM or something every once in awhile when I need alittle help as I learn that would be great.. Thanks..

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    You must use a loop.

    like:
    Code:
    int main() {
    
       bool done = false;
    
       while( !done ) {
          // guess code here
          // set done to true if user is right
       }
    
    }

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Use a loop of some sort. (Google it). The while() loop is used: while(expr) { code } and will continue executing "code" until "expr" proves false.
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	char guess1 [13];
    	int done = 0;
    
    	cout<<"Welcome to David's Guessing game. The game is simple all you have to do is guess the word I am thinking of.\n";
    
    	while(done == 0)
    	{
    		cin.getline( guess1, 13 );
    		if (strcmp (guess1, "sha boooiinng") == 0)
    		{
    			cout<<"Wow you won on you'r frist try. You must have cheated\n";
    			done = 1;
    		}
    		else
    			cout<<"You suck you have to try again.\n";
    	}
    
    	cin.get();
    }
    Hopefully that'll work. There are other, simpler ways to code the same result: using the keyword "break", for example.
    Keep reading those tutorials, but coding your own code is what will make the knowledge stick. It can be a tad frustrating at times, and I've started many projects that I never finished because the idea was too big for my experience. But even though a program isn't finished, something is learned each step of the way.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    10
    Thanks for both of your help... Now I just gotta find someone to help me once in awhile..

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The game obviously needs something more. You got to tell the guesser if they got some letters right or not. Otherwise it's practically impossible to guess it. And it would give you a chance to learn more about strings.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    10
    Aye that would be nice but I dont know how I would go about doing that at all.. I'll see what I can do though.. Thanks.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    guess1 needs enough space for the NULL terminator. It must be declared with 14 chars, at least, and getline must be passed 14 to read in 13 chars and add the terminator.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed