Thread: Newbee

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    3

    Talking Newbee

    I am new to programing and just want to share my first very small guess a number program and get some suggestion on it. There is one other thing I am using Bloodsheed Dev C++ and it come up with these errors at the bottom of the screen when compling but the .exe still works fine


    Code:
    ////////////////////////////////////////////////////////////////////////////////
    //
    //                              langue C++
    //              Pogram Computer generates number and user trys to guess it
    //
    ////////////////////////////////////////////////////////////////////////////////
    
    #include <iostream.h>   //Main include basic funcitions
    #include <string.h>     //For use of stings
    #include <stdio.h>      //??????
    int main()
    {
    int number;             //"number" variable use to save the users number
    int answer;             //"answer" variable use to save if the player wants to try agin 1=Yes 2=No
    int round;              //"round" variable use to save how many times the player has tryed to gues there is only 5 chances
    char name[20];          //"name" is a string that saves the players name and makes the game more customized
    int r;                  //"r" variable is the number that the user is tring to guess
    r=rand()%11;            //Changes the value of 'r" at random
    
    ////////////////////////////////////////////////////////////////////////////////
    //
    //                              Cheat Codes
    //
    ////////////////////////////////////////////////////////////////////////////////
    
    {
         round=0;           //Sets the veriable "round" to 0
         cout<<"What is your name: ";     //Ask the user to input there name
         cin.getline(name, 20, '\n');     //Saves the user input in the variable "name"
         if(!strcmpi("Beta", name))       //Tells the computer that if the user input is "Beta" then do the code below it
         {
              r=5;          //Sets the veriable "r" to 5
         }
         if(!strcmpi("Loser", name))      //Tells the computer that if the user input is "Loser" then do the code below it
         {
              cout<<"Sorry, "<<name<<" ,no cheating for you.  By the way did you chose this name. ";     //Prints the texts in "
              cin.get ();  //Tells the computer to wait until the user to press enter to run the next line of code
              return 0;    //Tells the computer to end the program
         }
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    //
    //                          Guess the Number part
    //
    ////////////////////////////////////////////////////////////////////////////////
    
    do                     //Tells the computer to do what is blow this if while = what you have put in
      {
            round++;       //Adds 1 to the veriable "round"
            cout<<"I am think of a number between 1-10 what is it: ";  
            cin>>number;
            if(number==r)
            {
                    cout<<"Congradulations, "<<name<<" ,you got it right!!!";
                    cin.get ();
                    answer=2;
            }
            else if(number>r)
            {
                    cout<<"1=Yes 2=No"<<endl<<endl<<"Sorry, "<<name<<" ,it was a little to high.  Would you like to try agin: ";
                    cin>>answer;
            }
            else if(number<r)
            {
                    cout<<"1=Yes 2=No"<<endl<<endl<<"Sorry, "<<name<<" ,it was a little to low.  Would you like to try agin: ";
                    cin>>answer;
            }
            if(round==5)
            {
                    answer=2;
                    cout<<"Sorry, "<<name<<" ,that was your last try the answer was "<<r;
                    cin.get ();
            }
      }
    while (answer==1);
    cin.get ();
    return 0;
    }
    The .exe is attached to the post. Feed back would be apceated.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    Sorry it did not post the .exe so i made it a zip and it has a picture of the errors I get when I try to compile

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    A few suggestions:

    1. If you get compile errors, but your exe still works, that is probably because you are using an old exe from an earlier time when your build succeeded. Try deleting (or moving or renaming) your exe and then compile to see if the build really is successful.
    2. Use the new/correct headers. <iostream.h> is old and non-standard, and <stdio.h> and <string.h> are deprecated. Change them to <iostream>, <cstring> and <cstdio>. That will cause you to have to deal with the std namespace, but if you don't want to worry about it you can just put using namespace std; under the includes on small single file projects like this.
      Code:
      #include <iostream>   //Main include basic funcitions
      #include <cstring>     //For use of stings
      #include <cstdio>      //??????
      using namespace std;
    3. You should #include <cstdlib> (or the deprecated <stdlib.h>), since that is where the rand() function comes from. It might work without it, but you should always include the proper header for the library functions you use.
    4. You might want to seed your random number, or the number will always be the same (8). Use srand(time(0)); and #include <ctime> (or the deprecated <time.h>) to get the time functions. This will make the number truly random.
    5. By doing rand()%11, you are getting a number from 0 to 10, not 1-10 like your menu says.
    6. You need to account for names longer than 19 characters. You have to ignore whatever is in the input after the first 19 characters are read into the name, or that will be used as an answer to your next few questions. Try inputting a name that is 30 characters long and see what happens.
    Hope the suggestions help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbee: Define a struct with scoop outside file?
    By Marcux in forum C Programming
    Replies: 2
    Last Post: 07-18-2007, 12:46 PM
  2. Newbee To C With Lccwin32 (Need Wedit HELP?)
    By theorematics in forum C Programming
    Replies: 3
    Last Post: 02-17-2005, 08:25 PM
  3. Linux newbee
    By geek@02 in forum Linux Programming
    Replies: 8
    Last Post: 11-09-2004, 12:02 AM
  4. Help with variables (newbee)
    By stugatza in forum C Programming
    Replies: 7
    Last Post: 08-18-2004, 10:40 AM
  5. Newbee question
    By d00-asu in forum Windows Programming
    Replies: 4
    Last Post: 08-31-2001, 03:32 AM