Thread: How does having a string with numbers affect a file?

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    2

    How does having a string with numbers affect a file?

    My assignment basically consists of reading from a .txt file and displaying information depending on what the user inputs. One way to look up information is with an ID that starts with N, and is followed by 3 integers. I am aware of the fact that, if I put numbers in a string, they are not really integers (right?). My question is: If I use strings to ask for the ID that the user inputs, will it correctly search for it in the text file?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Numbers can be converted to a string, sure. I believe there is a FAQ on it:
    FAQ > Convert an int to a string (char array) (C) - Cprogramming.com

    However since the ID is going to be a string anyway, you may want to think about reading the numbers from the user as a string too. This way making the ID number you will search the file with is a much simpler operation, it is just pasting two strings together.
    Code:
    C:\Users\jk\Desktop>more sandbox.c
    #include <stdio.h>
    #include <string.h>
    int main(void) {
       char actual_id[100] = "N";
       char input[20];
       printf("Enter ID Number (omit N): ");
       scanf("%19s", input);
       strncat(actual_id, input, 19);
       printf("The ID you entered is %s\n", actual_id);
       return 0;
    }
    
    C:\Users\jk\Desktop>gcc -ansi sandbox.c
    
    C:\Users\jk\Desktop>a.exe
    Enter ID Number (omit N): 123
    The ID you entered is N123
    Then you can search with that actual_id string, or check it if there are more requirements than what you stated here.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by Patricio Ramos View Post
    if I put numbers in a string, they are not really integers (right?).[/code]
    Right. But how would you "put numbers in a string"?
    Do remember how the char type is defined though.

    Quote Originally Posted by Patricio Ramos View Post
    My question is: If I use strings to ask for the ID that the user inputs, will it correctly search for it in the text file?
    As long as the user input is a stored in a string then the answer is yes.
    If you choose to store the user input in an integer, then you will need to do a proper conversion to compare.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    2
    That answers my question. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-25-2016, 06:53 AM
  2. EnableMenuItem has no affect
    By Gerread in forum Windows Programming
    Replies: 3
    Last Post: 04-28-2007, 02:47 AM
  3. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  4. will the header file affect the program?
    By Jasonymk in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 07:43 PM

Tags for this Thread