entering a long character

This is a discussion on entering a long character within the C Programming forums, part of the General Programming Boards category; I cant get this code to work, what am i doing wrong. I want to enter a model number and ...

  1. #1
    Unregistered
    Guest

    entering a long character

    I cant get this code to work, what am i doing wrong.

    I want to enter a model number and print an output depending on the answer.

    Please help and advise. Thanks.

    Code :-
    #include<stdio.h>
    #include<conio.h>

    int main (void)

    {
    clrscr();

    char a[6];

    printf("Please enter the Model Number? ");
    scanf("%s", &a);

    if (a == 123abc ) printf("Shelf 5");
    if (a == 456def ) printf("Shelf 4");
    if (a == 12ab54 ) printf("Shelf 3");
    if (a == 76fred ) printf("Shelf 2");
    if (a == 21jufs ) printf("Shelf 1");

    Return 0;

    }

  2. #2
    Peaches
    Guest
    1) I think it should be char a[7] to make room for the '\0' appended to strings
    2) You don't need the '&' in front of the 'a' in the scanf() as arrays are addresses anyway (and '&' means address of)
    3) I'm not sure you can compare stings like that - you'l probably have to use 'strcmp(a, "123abc")' returns 0 if 1 if false

    Hope that helps (if indeed its right =) )

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Peaches is spot on, Try this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void) 
    
    { 
    
    char a[7];  // needs one extra for the \0
    
    clrscr();
    
    printf("Please enter the Model Number? "); 
    scanf("%s", a); // dodgy scanf!!!
    
    if (!strcmp(a, "123abc" )) 
    	printf("Shelf 5"); 
    else if (!strcmp(a, "456def" )) 
    	printf("Shelf 4"); 
    else if (!strcmp(a, "12ab54" )) 
    	printf("Shelf 3"); 
    else if (!strcmp(a, "76fred" )) 
    	printf("Shelf 2"); 
    else if (!strcmp(a, "21jufs" )) 
    	printf("Shelf 1");
    else
    	printf("Unknown"); 
    
    return 0; 
    
    }
    The if else is better as you only check enough to find the right one, also I added an option for incorrect entries.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. hton long long
    By carrotcake1029 in forum C Programming
    Replies: 1
    Last Post: 06-01-2008, 08:26 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM
  5. Entering character in integer field
    By ToxicLove in forum C Programming
    Replies: 4
    Last Post: 05-23-2004, 04:51 PM

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