DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: c++ qns......

  1. #1
    Join Date
    Jun 2006
    Posts
    14

    Exclamation c++ qns......

    hi there...
    just wanna ask how to make the user can only type 4 number because i wanna do a mastermind game.....

    something like this:

    Enter your #1 guess: 0123
    Black: 0 White: 1

    and the white can locate whether u gt the right number but wrong place n black to locate whether u got the right place n the color.

    ty......

  2. #2
    Join Date
    Jun 2006
    Location
    Zagreb
    Posts
    49
    Check this one:

    Code:
    #include <conio.h>
    
    // this is your validation function; adapt it for valid range of "colors"
    bool isValidAnswerChar( char ch )
    {
       return ch >= '0' && ch <= '5';
    }
    
    void getAnswer( string& ch )
    {
       unsigned int char_rem = 4; // total number of characters in answer
       ch.clear();
    
       char curr = '\0';
       while ( char_rem )
       {
          curr = getch(); // get character without echoing
    
          // check if the character is valid and if it's already entered
          if ( isValidAnswerChar( curr ) && ch.find( curr ) == string::npos )
          {
             // add character to the answer and show it
             ch.append( 1, curr );
             putch( curr );
    
             --char_rem ;
          }
       }
    }
    getch() reads a character from the keyboard without echoing it, and putch() performs output to the console. I must warn you this works for Windows only because of <conio.h> file which is Win specific. If you want to port this code, read this as a reference material:


  3. #3
    Join Date
    Jun 2006
    Posts
    14

    thanks

    yo dcwexter... thanks for ur help...

    but i'm still a beginner.... n ur code is tooo difficult for me....

    but anyway thanks....

  4. #4
    Join Date
    Jun 2006
    Posts
    14
    hi any1....
    just some qn to ask.... how come my code...

    char printAscendingTriangle(int top_total)
    {

    int number=0;
    if(top_total >0){
    while(top_total>number){

    number++;
    cout<< setfill ('*') << setw (number)<<""<< endl;
    }
    }
    else if(top_total<0){

    while(abs(top_total)>number){

    number++;
    cout<< setfill ('*') << setw (number)<<""<< endl;
    }
    }
    return 0;
    }

    char printDescendingTriangle(int bottom_total)
    {
    if(bottom_total > 0){
    while(bottom_total>0)
    {
    cout << setfill ('*') << setw (bottom_total)<<""<< endl;
    bottom_total--;
    }
    }
    else if(bottom_total<0){
    while(abs(bottom_total)>0)
    {
    cout << setfill ('*') << setw (abs(bottom_total))<<""<< endl;
    bottom_total++;
    }
    }
    return 0;
    }

    will show this
    *
    **
    ***
    ***
    **
    *

    how come there is a space there.?? and the rest of my code also have a space...

    is it because of the set fill??? or setw...??? cause all my code use setfill and setw...

  5. #5
    Join Date
    Jun 2006
    Location
    Zagreb
    Posts
    49
    Can you be more specific about those spaces? If you're refering to the visual impression of spaces between lines of output, try with setfill('B') and see for yourself where the problem is.
    aliens are friends

  6. #6
    Join Date
    Jun 2006
    Posts
    14

    Exclamation

    Quote Originally Posted by dcwexter
    Can you be more specific about those spaces? If you're refering to the visual impression of spaces between lines of output, try with setfill('B') and see for yourself where the problem is.
    Top_total and the bottom_total is the input given by user… example for this case, 3 is given.

    char printAscendingTriangle(int top_total)
    {

    int number=0;
    if(top_total >0){
    while(top_total>number){

    number++;
    cout<< setfill ('*') << setw (number)<<""<< endl;
    }
    }
    return 0;
    }

    char printDescendingTriangle(int bottom_total)
    {
    if(bottom_total > 0){
    while(bottom_total>0)
    {
    cout << setfill ('*') << setw (bottom_total)<<""<< endl;
    bottom_total--;
    }
    }
    return 0;
    }

    will show this
    *
    **
    ***
    _***
    **
    *

    how come there is a space there(the underscore because the forum cannot show space…)…
    is it because of the set fill??? or setw...??? cause all my code use setfill and setw... :confused:

  7. #7
    Join Date
    Jun 2006
    Location
    Zagreb
    Posts
    49
    What is your calling code? The thing is that your code works fine on my platform (VS 2003 .NET, WinXP). Please, post your main (or whatever is it called) function that calls printAsc and printDesc.
    aliens are friends

  8. #8
    Join Date
    Jun 2006
    Posts
    14
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;

    char printAscendingTriangle(int top_total);
    char printPattern(int mid_total);
    char printDescendingTriangle(int bottom_total);


    void main(void)
    {
    int odd, total;
    char quit;
    cout<<"Input an integer (Press 'Q' to quit): ";
    cin>>total;
    odd=total%2;

    if(!cin) //Detecting input failure state if character is entered
    {
    cin.clear(); //reset input failure
    cin >> quit; //get input as character
    if(quit=='q' || quit=='Q'){
    cout<<"Press any key to continue . . .";
    }
    else
    {cout<<"Invalid entry! Please try again.\n";}
    }
    else
    {
    while(total!='q' || total!='Q'){

    if((odd==1 || odd==-1) && !(total == 1 || total == -1)){
    cout<< printAscendingTriangle(total);
    cout<< printPattern(total);
    cout<< printDescendingTriangle(total);

    }

    else{
    cout<<"Invalid entry! Please try again.\n";
    continue;
    }

    cout<<"Input an integer (Press 'Q' to quit): ";
    cin>>total;
    odd=total%2;
    if(!isdigit(total)) //Detecting input failure state if character is entered
    {
    cin.clear(); //reset input failure
    cin >> quit; //get input as character
    if(quit=='q' || quit=='Q'){
    cout<<"Press any key to continue . . .";
    }
    else
    {cout<<"Invalid entry! Please try again.\n";}
    }

    }
    }





    system("Pause");
    }



    char printAscendingTriangle(int top_total)
    {

    int number=0;
    if(top_total >0){
    while(top_total>number){

    number++;
    cout<< setfill ('*') << setw (number)<<""<< endl;
    }
    }
    else if(top_total<0){

    while(abs(top_total)>number){

    number++;
    cout<< setfill ('*') << setw (number)<<""<< endl;
    }
    }
    return 0;
    }

    char printPattern(int mid_total)
    {
    bool odd=false;
    int postive_space, negative_space, half,check=0;
    half=mid_total/2;
    postive_space=mid_total+1;
    negative_space=abs(mid_total)-1;
    if(mid_total<0){
    for(mid_total; mid_total!=0;mid_total++)
    {
    if (!odd){
    cout<<setw(abs(negative_space))<<setfill(' ')<<right<<"#"<<endl;

    if(abs(half)>check){
    negative_space--;
    check++;}
    else{
    negative_space++;
    check++;
    }
    odd=true;
    }

    else if(odd){
    cout<<setw(abs(negative_space))<<setfill(' ')<<"*"<<right<<endl;

    if(abs(half)>check){
    negative_space--;
    check++;}
    else{
    negative_space++;
    check++;
    }
    odd=false;
    }

    }
    }
    else if(mid_total>0){
    for(mid_total;mid_total!=0;mid_total--)
    {
    if (!odd){
    cout<<setw(postive_space)<<setfill(' ')<<"#"<<right<<endl;
    if(half>check){
    postive_space++;
    check++;}
    else{
    postive_space--;
    check++;
    }
    odd=true;
    }

    else if(odd){
    cout<<setw(postive_space)<<setfill(' ')<<"*"<<right<<endl;

    if(half>check){
    postive_space++;
    check++;}
    else{
    postive_space--;
    check++;
    }
    odd=false;
    }


    }
    }
    return 0;
    }

    char printDescendingTriangle(int bottom_total)
    {
    if(bottom_total > 0){
    while(bottom_total>0)
    {
    cout << setfill ('*') << setw (bottom_total)<<""<< endl;
    bottom_total--;
    }
    }
    else if(bottom_total<0){
    while(abs(bottom_total)>0)
    {
    cout << setfill ('*') << setw (abs(bottom_total))<<""<< endl;
    bottom_total++;
    }
    }
    return 0;
    }


    here is all my code..... and my do while loop seems to have some problem.... can u plz help mi toubleshoot... i have been trying for days.... ty...

  9. #9
    Join Date
    Jun 2006
    Location
    Zagreb
    Posts
    49
    Are you serious?
    aliens are friends

  10. #10
    Join Date
    Jun 2006
    Location
    Zagreb
    Posts
    49
    A question for you: what do you get after

    Code:
    cout << '\0';
    I think what you'll see is a space. So here is modified code:

    Code:
    void printAscendingTriangle( int top_total );
    void printDescendingTriangle( int bottom_total );
    
    enum AnswerState  { QuitState = 0, ValueState, UndefinedState };
    
    AnswerState
    getAnswer( int& outAnswer  )
    {
       int answer;
       cout << "Input an integer (Press 'Q' to quit): ";
       cin >> answer;
    
       if ( !cin )
       {
          char quit;
          cin.clear();
          cin >> quit;
          return ( quit == 'q' || quit == 'Q' ? QuitState : UndefinedState );
       }
    
       if ( answer < 2 )
          return UndefinedState;
    
       outAnswer = answer;
       return ValueState;
    }
    
    int
    main( void )
    {
       int answerValue;
       AnswerState answer = getAnswer( answerValue );
       while ( answer != QuitState )
       {
          if ( answer == ValueState )
          {
             // there's no cout << print??? anymore
             printAscendingTriangle( answerValue );
             // printPattern( answerValue );
             printDescendingTriangle( answerValue );
          }
          else
             cout << "Invalid entry! Please try again.\n";
          answer = getAnswer( answerValue );
       }
    
       return 0;
    }
    First of all, you don't need your print functions to return anything. What they do is print some shapes. And that's it, no more no less. So don't polute them with return 0;

    Next thing to do is to define your answer funciton. All you've done in code was more or less OK, but it was too disperged also. So, I've created function that tries to get some meaningful input from the user. If user input is a number, return value is ValueState from AnswerState enumeration. If the user enters Q or q, the function returns QuitState, and your main loop exits. If the user enters anything else, the function returns UndefinedStated, which means that user needs to input once again. Note that getAnswer function has outAnswer by reference.

    If the number is less than 2, it obviously means that user entered invalid value: if number 0 or 1 is entered, you don't get representative output, if the value is less than 0, it means that you're trying to print out with negative width (which could be than called antiwidth). So what we do when value is negative is to proclaim it invalid and wait for another user input. If you still want to support negative values, allow user to enter negative values, but store to the outAnswer absolute value of negative value entered. That way you can omit all the code for the negative values, which simplifies code significantly (in other words, you check for negative values only once).

    One more thing: main function is never void. It must have int return type. "return 0" at the end is optional, but return value type of main function is always int.

    Oh, I almost forgot, you need to fix printPattern function. I mean, really?!
    aliens are friends

  11. #11
    Join Date
    Jun 2006
    Posts
    14
    Quote Originally Posted by dcwexter
    A question for you: what do you get after

    Code:
    cout << '\0';
    I think what you'll see is a space. So here is modified code:

    Code:
    void printAscendingTriangle( int top_total );
    void printDescendingTriangle( int bottom_total );
    
    enum AnswerState  { QuitState = 0, ValueState, UndefinedState };
    
    AnswerState
    getAnswer( int& outAnswer  )
    {
       int answer;
       cout << "Input an integer (Press 'Q' to quit): ";
       cin >> answer;
    
       if ( !cin )
       {
          char quit;
          cin.clear();
          cin >> quit;
          return ( quit == 'q' || quit == 'Q' ? QuitState : UndefinedState );
       }
    
       if ( answer < 2 )
          return UndefinedState;
    
       outAnswer = answer;
       return ValueState;
    }
    
    int
    main( void )
    {
       int answerValue;
       AnswerState answer = getAnswer( answerValue );
       while ( answer != QuitState )
       {
          if ( answer == ValueState )
          {
             // there's no cout << print??? anymore
             printAscendingTriangle( answerValue );
             // printPattern( answerValue );
             printDescendingTriangle( answerValue );
          }
          else
             cout << "Invalid entry! Please try again.\n";
          answer = getAnswer( answerValue );
       }
    
       return 0;
    }
    First of all, you don't need your print functions to return anything. What they do is print some shapes. And that's it, no more no less. So don't polute them with return 0;

    Next thing to do is to define your answer funciton. All you've done in code was more or less OK, but it was too disperged also. So, I've created function that tries to get some meaningful input from the user. If user input is a number, return value is ValueState from AnswerState enumeration. If the user enters Q or q, the function returns QuitState, and your main loop exits. If the user enters anything else, the function returns UndefinedStated, which means that user needs to input once again. Note that getAnswer function has outAnswer by reference.

    If the number is less than 2, it obviously means that user entered invalid value: if number 0 or 1 is entered, you don't get representative output, if the value is less than 0, it means that you're trying to print out with negative width (which could be than called antiwidth). So what we do when value is negative is to proclaim it invalid and wait for another user input. If you still want to support negative values, allow user to enter negative values, but store to the outAnswer absolute value of negative value entered. That way you can omit all the code for the negative values, which simplifies code significantly (in other words, you check for negative values only once).

    One more thing: main function is never void. It must have int return type. "return 0" at the end is optional, but return value type of main function is always int.

    Oh, I almost forgot, you need to fix printPattern function. I mean, really?!
    ya printpattern function is a must....
    and wat is enum??
    btw ty for helping mi.... but i will nt be online for few days... so i will nt read the post... ty...

  12. #12
    Join Date
    Jun 2006
    Location
    Zagreb
    Posts
    49
    I didn't say that you remove printPattern function, just to fix it, simplify it, or whatever. It doesn't even work properly (for even numbers).

    Enumeration is something like set of integral constants. In the code above you can read

    Code:
    enum AnswerState  { QuitState = 0, ValueState, UndefinedState };
    as something similar to this:

    Code:
    const int QuitState = 0;
    const int ValueState = 1;
    const int UndefinedState = 2;
    The only difference is that AnswerState defines new type that is used very much like an integer type. So, without using enumeration, the code would look like this:

    Code:
    int
    getAnswer( int& outAnswer  )
    {
       int answer;
       cout << "Input an integer (Press 'Q' to quit): ";
       cin >> answer;
    
       if ( !cin )
       {
          char quit;
          cin.clear();
          cin >> quit;
          return ( quit == 'q' || quit == 'Q' ? 0 : 2 );
       }
    
       if ( answer < 2 )
          return 2;
    
       outAnswer = answer;
       return 1;
    }
    
    int
    main( void )
    {
       int answerValue;
       int answer = getAnswer( answerValue );
       while ( answer )
       {
          if ( answer == 1 )
          {
             // there's no cout << print??? anymore
             printAscendingTriangle( answerValue );
             // printPattern( answerValue );
             printDescendingTriangle( answerValue );
          }
          else
             cout << "Invalid entry! Please try again.\n";
          answer = getAnswer( answerValue );
       }
    
       return 0;
    }
    Which do you find more easily digestible? try to find some examples with enumerations (you can find it in almost every introductory book on c++).
    aliens are friends

  13. #13
    Join Date
    Jun 2006
    Posts
    14
    yo dude... thanks... i gt my code running properly.... nw i know wat's the problem... it's only the cout thing when using functions.... ty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center