Thread: True or false statements

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    20

    True or false statements

    I was wondering if someone could point me in the right direction here with my code.The objective of the program is to ask the users name and display their name before asking them a question. The program ask the user if "Europe is bigger than Asia?"

    "The program must include 2 string answers(one if user is correct, and one if user is wrong)in a 2-D array, and should print out the proper answer from the array after getting the user‟s answer"
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
    	int i,j;
    	char  choice[BUFSIZ], name[BUFSIZ];
        char T[23] = {"Your answer is wrong."};
    	char F[24] = {"Your answer is correct!"};
    
           
            printf("What is your name ?");
            gets(name);
            puts("Hello");
            puts(name); 
            printf("Is Europe larger than Asia? (T) or (F):");
            //scanf("%s", choice);
    		gets(choice);
    		gets(T);
    		gets(F);
    		for(i=0; i<24; i++)
    		{
    		if( strcmp( choice , F ) == 0 )
            puts(T[i]); 
    		
    	    // printf("%s", F);
    		else if (strcmp(choice , F) < 0)
                putchar("\n");
    			puts(F[i]);
    		}
    	// printf("%s", T);
    
    getchar();
    return 0;
        
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, what is the problem that you are facing?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    Ok if I use scanf for choice instead of gets and I use printf instead of puts I get no errors, but nothing outputs. If U use gets and puts it says T[i] and F[i] can't be used I looked at the examples from my lecture but it works just fine! I'm not sure why either method doesn't work.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why are you trying to input into T or F?

    You also don't have an array of strings. You have a string called T, and a string called F. Your requirements that you posted indicated you need an array of two strings, so you should probably make something like
    Code:
    char responses[2][24];

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Correct the usage of puts.

    Signature:
    int puts(const char *s);

    You are trying to use T[i] so pass the address of this.
    Also it seems that you are going wrong logically when comparing inputs.

    Read the signature and usage of functions before you use in code.
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    Here is what I have now. It outputs my name and lets me input T or F but it doesn't display the result.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    int main(void)
    {
    int i,j;
    char choice[BUFSIZ], name[BUFSIZ];
    char output [2][24] = {"Your answer is wrong.",
    "Your answer is correct!"};
    
    
    printf("What is your name ?");
    gets(name);
    puts("Hello");
    puts(name);
    printf("Is Europe larger than Asia? (T) or (F):");
    scanf("%s", &choice);
    //gets(choice);
    
    putchar('\n');
    for(i=0; i<24; i++)
    
    if (toupper (choice) =="T")
    puts (output[i]);
    //if( strcmp( choice , F ) == 0 )
    
    
    // printf("%s", F);
    // else
    //else if (strcmp(choice , F) < 0)
    // putchar("\n");
    //puts(F[i]);
    
    // printf("%s", T);
    
    
    return 0;
    
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    scanf("%s", &choice);
    %s is for a string, you only want one character, so why are you reading more?
    &choice in this case would be fine as just choice.
    Code:
    if (toupper (choice) =="T")
    You can't compare string literals (or arrays) with ==. "" indicates multiple characters, ' ' indicates a single character.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char choice, name[BUFSIZ];
       char output [2][24] = {"Your answer is wrong.",
                               "Your answer is correct!"};
    
        printf("What is your name ?");
        fgets(name,BUFSIZ,stdin);
        printf("Hello %s\n\n", name);
    
        printf("Is Europe larger than Asia? (T) or (F):");
        scanf("%c", &choice);
    
        if (toupper( choice ) == 'T')
          printf("%s \n",output[0]);
        else
          printf("%s \n",output[1]);
    
    
        return 0; 
    }
    Last edited by CommonTater; 07-01-2011 at 07:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. true or false
    By johngav in forum C Programming
    Replies: 4
    Last Post: 03-19-2009, 02:25 PM
  2. True or False
    By noob programmer in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 10:15 AM
  3. true/false
    By goran00 in forum C Programming
    Replies: 18
    Last Post: 03-12-2008, 10:40 AM
  4. optimizations in if statements using true and false
    By Bigbio2002 in forum C Programming
    Replies: 14
    Last Post: 11-11-2004, 07:26 PM
  5. True or false
    By Eibro in forum Tech Board
    Replies: 9
    Last Post: 09-15-2002, 08:07 AM