Thread: checking what the user has inputed

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    checking what the user has inputed

    Hi, I am using Dev C++ on a windows computer.

    In my code, the user enters the name of a city, and then according to what city it is, the program displays the coordinates of that city

    I can't find a way of figuring out and checking which city the user has entered so the code can displays its appropriate latitude and longitude


    Any ideas ???

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I am going to answer your question. However I think this is not your problem.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char city[15];
        
        printf("Input please\n");
        
        // Read up to the length of city (%15s)
        scanf("%15s", city);
        
        if(strcmp(city, "Athens") == 0)
        {
            printf("Γεία σου!\n");
        }
        else if(strcmp(city, "Munich") == 0)
        {
            printf("Hallo\n");
        }
        else if(strcmp(city, "Milan") == 0)
        {
            printf("Ciao\n");
        }
        else if(strcmp(city, "Sydney") == 0)
        {
            printf("Hello\n");
        }
        else
        {
            printf("Unknown city\n");
        }
    
        return 0;
    }
    For ref for strcmp click here.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    I did this and it worked perfectly, but is there another and shorter version for this....

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    String comparison would be part of the solution. It is a fair bet that a user will not enter "London" when they are thinking of Chicago.

    However, you would need to handle the fact that some names of cities have been reused in different countries, and even sometimes in multiple states within a country. For example, several states in the US have towns or cities named London. So your code would need to detect if there are multiple matches, and ask the user for clarification (for example, a country name or maybe a state).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    Yes, that approach breaks down as your city list gets bigger.

    You need to declare a structure with members name, longitude, latitude. Then populate an array of them using malloc and read in from an external file, or automatically generated as an internal data file.
    Then the simplest solution is to step through it from 0 to N-1, looking froma match with strcmp().
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    ummm, i am a beginner at c so i didnt understand to declare a structure with members name, longitude, latitude. Then populate an array of them using malloc and read in from an external file, or automatically generated as an internal data file.

    but for the multiple cities, there is a prompt that asks which city the user is asking for, it states the city, state and country for each similar possibility

    but i still dont get how to
    populate an array of them using malloc ??????

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    i just need a short, sweet version instead of the longish if statements

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    For structs see a simple example here.
    You could use a switch statement, instead of an if-else statement, but again it will be long!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by LAMER_CODER View Post
    i just need a short, sweet version instead of the longish if statements
    In other words, you want someone else to do your homework for you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Use a table. I.e. you have an array or strings and other stuff. The code then just has a loop that checks for a match.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Help. square and cube a user inputed number
    By Beedee5889 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 01:53 PM
  2. How to see if a user inputed a string
    By herocks in forum C++ Programming
    Replies: 9
    Last Post: 01-23-2007, 02:31 AM
  3. ignoring '/' when inputed by user
    By liquidcourage1 in forum C++ Programming
    Replies: 15
    Last Post: 03-02-2006, 11:22 PM
  4. user inputed struct
    By egomaster69 in forum C Programming
    Replies: 2
    Last Post: 12-15-2004, 06:43 PM
  5. User inputed file name
    By Twiggy in forum C Programming
    Replies: 2
    Last Post: 11-01-2001, 12:35 PM