Like Tree2Likes

Having trouble with variable types

This is a discussion on Having trouble with variable types within the C Programming forums, part of the General Programming Boards category; As some of you are aware I am trying to make a program to make a code that will read ...

  1. #1
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67

    Having trouble with variable types

    As some of you are aware I am trying to make a program to make a code that will read 10 characters and from there display numerous outputs depending on what characters are present.

    I have successful with the guidance of some of you here wrote the correct source code and it will read the characters. However I would like ir to distingguish between the first and the second character so I can display different ouputs depending on the first and second characters independantly.

    my code is as follow
    Code:
     #include<stdio.h>  
    void main(void)
    {
      
    
    
       int search, c, n, count = 0, number, count1[127]={0}, count2[127]={0}, i, a;
       char array[127], array1[127];
      
      
       n = 10; 
      
      
      
       for ( c = 0 ; c < n ; c++ )
          scanf(" %c  %c ",&array[c], &array1[c]);
      
          for(i=0;i<n;i++)  
       { 
          count1[array[i]]++;                                                              //if array[i] is N, then count1[N]++//
       }
     
       
       for(i=0;i<127;i++)
        { 
          
          
          if(count1[i] == 5)
          printf("Flush.\n");
          if(count1[i] == 4)
          printf("Four of a kind.\n");
          if(count1[i] == 3)
          printf("Three of a kind.\n");
          if(count1[i] == 2)
          printf("Pair.\n");
          if(count1[i] == count1[i] && count1[i] == 2)
          printf("Two Pair.\n");
          
          for(a=0;a<n;a++)  
       { 
          count1[array1[a]]++;                                                              //if array[a] is N, then count1[N]++//
       }
     
       
       for(a=0;a<127;a++)
        { 
          
          
          if(count1[a] == 5)
          printf("Flush.\n");
          if(count1[a] == 4)
          printf("Four of a kind.\n");
          if(count1[a] == 3)
          printf("Three of a kind.\n");
          if(count1[a] == 2)
          printf("Pair.\n");
          if(count1[a] == count1[a] && count1[a] == 2)
          printf("Two Pair.\n");
          
         
          
              
                          
                          
           };      
      
     
        return 0;
     
        }
      }
    I am not sure if I have done the correct thing with making two arrays.
    Just to help out anyone is trying to help me, my input would be something along the lines of
    " c4 c7 sj sa h7" all representing cards from a deck of cards.

    Thank you so much for your time.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497
    "void main" should be "int main"

    Also, notice that your return statement is in the wrong spot - this is most likely because you missed a closing bracket on the second "for" statement and added the last curly bracket to get rid of the error.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497
    I would also like to add that spotting missing curly braces is easy if you indent your code properly - Which, to your credit, you were close. It might be worth seeing if your IDE has an auto indentation option - What do you use to write your programs in?
    stahta01 likes this.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    THanks for your help, I have to use an in browser IDE - one the university told me to use. I am still a bit stuck with the curly braces and where it should, have been looking at it and playing around with them but with no luck. Any help would be appreciated.

  5. #5
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I am so very close to it working now, I think I have sorted out the curly brace issue, correct me if I am wrong though ( will post code in a minute ). However I still am unable to get it to distingush properly between the first and second character of input and give me an output depending on both of these.
    The second part always seems to show three of a kind no matter what changes I make to the source code.
    Code:
      #include<stdio.h>  void main(void)
    {
       
     
     
       int search, c, n, count = 0, number, count1[127]={0}, count2[127]={0}, i, a;
       char array[127], array1[127];
       
       
       n = 10; 
       
       
       
        
       for ( c = 0 ; c < n ; c++ )
       scanf(" %c %c ",&array[c], &array1[c]);
       
       {   
        for(i=0;i<n;i++)  
        
        count1[array[i]]++;                                                              //if array[i] is N, then count1[N]++//
       }
      
        
               for(i=0;i<127;i++)
                 { 
           
           
                 if(count1[i] == 5)
                    printf("Flush.\n");
                 
                 }   
                 
                 
                 
           
       {   
        for(a=0;a<n;a++)  
       
        count2[array1[a]]++;                                                              //if array[a] is N, then count1[N]++//
       }
      
        
            for(a=0;a<127;a++)
                 { 
           
           
                 if(count1[a] == 5)
                    printf("Flush.\n");
                 if(count1[a] == 4)
                    printf("Four of a kind.\n");
                 if(count1[a] == 3)
                    printf("Three of a kind.\n");
                 if(count1[a] == 2)
                    printf("Pair.\n");
                 if(count1[a] == count1[a] && count1[a] == 2)
                    printf("Two Pair.\n");
                 
                  return 0;   
            
                 };      
       
      
       
      
    }
    my input for example is
    s2 sq sq sq sq

    with my output being
    Flush. Three of a kind.

    And i know it is not possible to have queen of spades four times, this is just for testing.
    Thanks again

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,196
    I regard styling issues normally as a matter of taste, but in this case I think it is preventing you from seeing problems. To help see the problems, please rewrite your code according to these rules:
    1. No blank lines, and only one variable declaration per line (that way we can clearly see which line has a problem)
    2. Use braces after if, for, etc. And do not use them elsewhere unless needed.
    3. Turn on warnings and post them here first

    Notice that following these simple rules does not change the meaning or logic of your code.

    Code:
    #include<stdio.h> 
    int main(void)
    {
        int search;
        int c;
        int n;
        int count = 0;
        int number;
        int count1[127] = {0};
        int count2[127] = {0};
        int i;
        int a;
        char array[127];
        char array1[127];
        n = 10;
        for (c = 0; c < n; c++) {
            scanf(" %c %c ", &array[c], &array1[c]);
        }
        for (i = 0; i < n; i++) {
            count1[array[i]]++;    //if array[i] is N, then count1[N]++//
        }
        for (i = 0; i < 127; i++) {
            if (count1[i] == 5) {
                printf("Flush.\n");
            }
        }
        for (a = 0; a < n; a++) {
            count2[array1[a]]++;    //if array[a] is N, then count1[N]++//
        }
        for (a = 0; a < 127; a++) {
            if (count1[a] == 5) {
                printf("Flush.\n");
            }else if (count1[a] == 4) {
                printf("Four of a kind.\n");
            }else if (count1[a] == 3) {
                printf("Three of a kind.\n");
            }else if (count1[a] == 2) {
                printf("Pair.\n");
            }else if (count1[a] == count1[a] && count1[a] == 2) {
                printf("Two Pair.\n");
            }
            return 0;
        }
    }
    Here is the output from gcc
    Code:
    cards.c: In function 'main':
    cards.c:20:3: warning: array subscript has type 'char' [-Wchar-subscripts]
    cards.c:28:3: warning: array subscript has type 'char' [-Wchar-subscripts]
    cards.c:8:6: warning: unused variable 'number' [-Wunused-variable]
    cards.c:7:6: warning: unused variable 'count' [-Wunused-variable]
    cards.c:4:6: warning: unused variable 'search' [-Wunused-variable]
    cards.c:44:1: warning: control reaches end of non-void function [-Wreturn-type]
    You should address these problems first. For example, your last "for" loop only runs the first time and then you immediately return. Maybe you got the braces mixed up.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,196
    Also try to change your input format to like this

    Code:
    if (scanf(" %c%c", &array[c], &array1[c]) != 2) {
    	printf("invalid input");
    	exit(EXIT_FAILURE);
    }
    Notice you should do this 5 times, not 10, if you want to read in a line like "s2 sq sq sq sq"

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by SDH View Post
    I have to use an in browser IDE - one the university told me to use.
    As I understand it, you have to submit your work via the browser IDE.

    But why don't you write your program offline at your computer with the IDE/editor of your choice and test and debug it there? If you are finished, just copy'n'paste your code into the browser IDE and submit it.

    Is that really impossible?

    Bye, Andreas

  9. #9
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    AndiPersti - that is possible thanks, any that you would recommend?
    c99tutorial - thank you very much for helping me lay it out in the correct format. And for also pointing out the problem with the 10. i thought i needed 10 because there is 10 characters present but clearly that doesn't matter. Will have a play around and try and get it working.

    Thank you very much for your help.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by SDH View Post
    AndiPersti - that is possible thanks, any that you would recommend?
    I don't use an IDE so I can't recommend one.

    This site recommends Code::Blocks for Windows and XCode for OSX.

    Bye, Andreas

  11. #11
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I have run my code on the compiler you gave me to into the link. But any ideas of why my code still wont work? I'm stuck for ideas now

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,196
    Post the code which works in Codeblocks but doesn't work in the online compiler. And post the message the on-line compiler gives you.

  13. #13
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    This is the version that will not work on the online compiler. What ever the input it alwaysgives an output of 'Four of a kind'. Have I got the way it displays the hand set up correctly.

    I would like it to work, that if there is 4 characters the same ( of the second character ) then display four of a kind, then 3 for kind of a kind etc etc

    Code:
     #include<stdio.h>int main(void)
    {
    
    
        int c;
        int n;
    
    
    
    
        int count1[127] = {0};
        int count2[127] = {0};
        int i;
        int a;
        char array[127];
        char array1[127];
        n = 10;
        for (c = 0; c < n; c++) {
            scanf(" %c %c ", &array[c], &array1[c]);
        }
        for (i = 0; i < n; i++) {
            count1[array[i]]++;    //if array[i] is N, then count1[N]++//
        }
        for (i = 0; i < 127; i++) {
            if (count1[i] == 5) {
                printf("Flush.\n");
            }
        }
        for (a = 0; a < n; a++) {
            count2[array1[a]]++;    //if array[a] is N, then count1[N]++//
        }
        for (a = 0; a < 127; a++) {
            if (count2[a] == 5) {
                printf("Flush.\n");
            }else if (count2[a] == 4) {
                printf("Four of a kind.\n");
            }else if (count2[a] == 3) {
                printf("Three of a kind.\n");
            }else if (count2[a] == 2) {
                printf("Pair.\n");
            }else if (count2[a] == count2[a] && count2[a] == 2) {
                printf("Two Pair.\n");
            }
            return 0;
        }
    }
    thanks for you assistance

  14. #14
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Just to help you I think has something to do with this part of the code,
    Code:
     for (c = 0; c < n; c++) {        scanf(" %c %c ", &array[c], &array1[c]);
    because if i alter the %c the output will change.

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    1,196
    So just to clarify: this code does not give you the correct result, and it is not an issue of whether you use Codeblocks with gcc or whether you use the online compiler. So the suggestion was, to try to get it work as you want on your own computer (because this is an easier tool) and then afterwards to submit the final product to your online compiler (because that's what your prof uses)

    1. You didn't address the scanf format yet. Read post #7 above.
    2. Why do you have "return 0" inside your for loop?
    3. Provide us with the exact input line(s) you are giving it and what the answer should be.
    4. Step 3 is important for testing - its a "test case" and you should generally repeat this step for any program after you make changes to make sure it didn't get "broken".

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable types
    By C_ntua in forum C Programming
    Replies: 4
    Last Post: 06-18-2008, 04:13 AM
  2. File types trouble...
    By Raigne in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2006, 07:35 PM
  3. variable types
    By pktcperlc++java in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2004, 09:30 PM
  4. variable types
    By chavezbri in forum C Programming
    Replies: 3
    Last Post: 01-16-2003, 01:39 PM
  5. Variable types
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2001, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21