Thread: Code for CS50 functions?

  1. #1
    Registered User VEN0M's Avatar
    Join Date
    Jun 2012
    Posts
    16

    Code for CS50 functions?

    I am working with a computer that currently has no Internet access. I need to be able use use the CS50 library (it has functions like GetInt
    and GetString) for my program. Do any of ya'll now the code behind those functions so I can add and declare the function in my code? Or maybe code that does the same thing that you don't need any other libraries?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Download the library from a computer with Internet access, save it to media that can be accessed from this computer that has no Internet access, and hence work around the lack of Internet access.

    Alternatively, if you know what GetInt and GetString do, it may be feasible to just implement them using the facilities available in the C standard library.
    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 VEN0M's Avatar
    Join Date
    Jun 2012
    Posts
    16
    Quote Originally Posted by laserlight View Post
    Download the library from a computer with Internet access, save it to media that can be accessed from this computer that has no Internet access, and hence work around the lack of Internet access.

    Alternatively, if you know what GetInt and GetString do, it may be feasible to just implement them using the facilities available in the C standard library.
    Ok. So when you say "save it to media" do you mean to download the CS50 folders and them put them in the right places on the other computer?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    While I don't want to be presumptuous this sounds dangerously close to "hey kids, let's not bother learning the C language I/O and use the instructor's crap library to avoid all that ugly stuff." That's all hunky dory until you are asked to write a program in the real world and you can't use your instructor's crap library.

    I hope this is not the case, but it's worth paying attention to what it is that you are being taught.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User VEN0M's Avatar
    Join Date
    Jun 2012
    Posts
    16
    Haha. Don't worry... they are just using this library for the first couple weeks. CS50 is a class from Harvard. So they won't handicap you with custom libraries for long. This is what GetInt is doing... Does anyone know how to write this using the standard library? I looked up the code behind all of the functions in the cs50 library but could not find it. :/


    1. // Input Days
    2. short int days;
    3. while (days<28 || days > 31){
    4. printf("Days in the month: ");
    5. days = GetInt();




  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well I am assuming that GetInt is probably reading an integer from the standard input (i.e. keyboard) and is validating that the input is actually an int. If the validation fails it either throws an error message or sets a flag or returns some invalid value. There is no way to tell what it is doing from your snippet.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User VEN0M's Avatar
    Join Date
    Jun 2012
    Posts
    16
    Oh sorry. Your right. That is what it is doing. Can you do the same thing without using GetInt? I heard that is uses scanf to get a number from the user and store it I a variable.
    Last edited by VEN0M; 07-25-2012 at 10:18 AM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by VEN0M View Post
    Can you do the same thing without using GetInt?
    Of course. GetInt is not some magic that happens in the computer, it's a function. It's a function made up of a bunch of C commands. You could write a function of your own that behaves similarly/identically.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by VEN0M
    Ok. So when you say "save it to media" do you mean to download the CS50 folders and them put them in the right places on the other computer?
    Yes, e.g., using a thumb drive, CD, DVD, portable hard disk drive, etc.

    Quote Originally Posted by VEN0M
    Can you do the same thing without using GetInt?
    Yes, e.g., do the same thing that was done to implement GetInt.
    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

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Took me about 3-5 minutes to find the source code for the c50 library.
    There is also an installation guide.

    Time to improve your researching skills :-)

    Bye, Andreas

  11. #11
    Registered User VEN0M's Avatar
    Join Date
    Jun 2012
    Posts
    16
    Ok. Great. Does anyone know what the code behind GetInt means I heard that it uses scanf to get a number from the user and place it in a variable.

  12. #12
    Registered User VEN0M's Avatar
    Join Date
    Jun 2012
    Posts
    16
    @Andi. I already saw that but as I said earlier I do not have Internet. It would be kind of difficult to run wget without an Internet connection.

  13. #13
    Registered User VEN0M's Avatar
    Join Date
    Jun 2012
    Posts
    16
    @laser do you think I would get the same result by dragging and dropping folders as running this:


    apt-get install gccwget http://mirror.cs50.net/library/c/cs50-library-c-3.1.zipunzip cs50-library-c-3.1.ziprm -f cs50-library-c-3.1.zipcd cs50-library-c-3.1gcc -c -ggdb -std=c99 cs50.c -o cs50.oar rcs libcs50.a cs50.ochmod 0644 cs50.h libcs50.amkdir -p /usr/local/includechmod 0755 /usr/local/includemv -f cs50.h /usr/local/includemkdir -p /usr/local/libchmod 0755 /usr/local/libmv -f libcs50.a /usr/local/libcd ..rm -rf cs50-library-c-3.1

    It looks pretty complicated but that might just be me.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by VENOM
    Does anyone know what the code behind GetInt means I heard that it uses scanf to get a number from the user and place it in a variable.
    Have you already completed the CS50 curriculum? I presume that at some point in the class, you will learn how to implement GetInt. If not, it is time to start searching for new learning material to advance your knowledge. Asking us to explain "the code behind GetInt" when you apparently have not looked at it yet (hence the "I heard") is kind of presumptuous.
    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

  15. #15
    Registered User VEN0M's Avatar
    Join Date
    Jun 2012
    Posts
    16
    I have tried to find the code behind it but it is not posted anywhere. When I said "I heard" I was saying that because one of the teachers said something on the lines if "GetInt basically uses scanf to take and print input from the user." No, I have not finished the course, if I had then you would not be reading this post.
    Last edited by VEN0M; 07-25-2012 at 10:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling C functions from C++ code
    By starshiptrooper in forum C++ Programming
    Replies: 34
    Last Post: 02-17-2012, 11:05 AM
  2. Functions/code help!!
    By Makdaddy in forum C Programming
    Replies: 1
    Last Post: 12-08-2010, 12:11 AM
  3. Plz help me to include cs50.h and cs50.c into Code::Block
    By Huncowboy in forum C Programming
    Replies: 4
    Last Post: 03-09-2010, 10:05 AM
  4. How to code these functions?
    By Cicci in forum C Programming
    Replies: 15
    Last Post: 07-10-2007, 01:52 PM
  5. Loops OR Functions!! HELP WITH THIS CODE!!!!!!!!!!
    By WIshIwasGooD in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2001, 12:49 PM